local Cbz = require("source.cbz") local Image = require("utils.image") local Paths = require("utils.paths") local Client = require("moonsource_bilimanga.client") local Mapper = require("moonsource_bilimanga.mapper") local _ = require("moonsource_bilimanga.l10n") local Chapter = {} function Chapter.archivePath(identity, chapter) return Paths.bookWorkDir(identity.stable_id, identity.source_id) .. "/" .. chapter.uid .. ".cbz" end function Chapter.existing(identity, chapter) local path = Chapter.archivePath(identity, chapter) return Cbz.ready(path) and path or nil end function Chapter.materializeAsync(identity, chapter, on_progress, cb) local archive_path = Chapter.archivePath(identity, chapter) local state = { cancelled = false, active = nil } if Cbz.ready(archive_path) then require("ui/uimanager"):nextTick(function() if not state.cancelled then cb(archive_path) end end) return { cancel = function() state.cancelled = true end } end local function log(msg) pcall(require("logger").getLogger, "bilimanga") end Client.imageIndexAsync(chapter.uid, function(wire, err) if err then cb(nil, err); return end local urls = Mapper.images(wire) if not urls or #urls == 0 then cb(nil, _("章节图片解析失败")) return end local image_dir = archive_path:gsub("%.cbz$", "") Paths.ensureDir(image_dir) local paths = {} for i, url in ipairs(urls) do paths[i] = image_dir .. "/" .. string.format("%04d%s", i, Image.suffix(url) or ".jpg") end local current = 1 local function nextImage() if state.cancelled then return end local url = urls[current] if not url then Cbz.build(paths, archive_path, state, function(path, cbz_err) if state.cancelled then return end if cbz_err then log("Cbz.build error: " .. tostring(cbz_err)) end cb(path and { title = chapter.title, file = path } or nil, cbz_err or (state.cancelled and nil or _("图片下载失败"))) end) return end local path = paths[current] if Image.ok(path) then if on_progress then on_progress(current, #urls) end current = current + 1 require("ui/uimanager"):nextTick(require("ui.guard").wrap("bilimanga image", nextImage)) return end local downloaded = false local handle = require("http.request").download({ url = url, headers = { ["Referer"] = "https://manga.bilibili.com/", }, timeout = require("http.timeout").net(3), max_bytes = 32 * 1024 * 1024, }, path, function(ok, download_err) downloaded = true state.active = nil if state.cancelled then return end if not ok or not Image.ok(path) then os.remove(path) cb(nil, download_err and tostring(download_err) or _("图片下载失败")) return end if on_progress then on_progress(current, #urls) end current = current + 1 require("ui/uimanager"):nextTick(require("ui.guard").wrap("bilimanga image", nextImage)) end) if not downloaded then state.active = handle end end nextImage() end) return { cancel = function() state.cancelled = true if state.active then state.active.cancel() end end, } end function Chapter.fetcher(source) return function(identity, chapter, cb) local existing = Chapter.existing(identity, chapter) if existing then require("ui/uimanager"):nextTick(function() cb({ title = chapter.title, file = existing }) end) return { cancel = function() end } end local result = Chapter.materializeAsync(identity, chapter, nil, function(path, err) if path then cb({ title = chapter.title, file = path }) else cb(nil, err) end end) return result end end return Chapter