✨ (kmoe): 下载的 EPUB 按页序转成 CBZ
转不了时仍打开原来的 EPUB,已下载的卷不再重新扣额度。
This commit is contained in:
1 parent
6fddb67ab3
commit
fd1cd66cd6
7 files changed
+497
-55
No files matched your search
+174
-7
@@ -1,5 +1,5 @@
|
||||
--[[--
|
||||
moonsource_kmoe.source / chapter 离线用例:书城节点与筛选组、搜索聚合、卷下载(制作中、已有文件、非 EPUB)、缓存统计。
|
||||
moonsource_kmoe.source / chapter 离线用例:书城节点与筛选组、搜索聚合、卷下载(制作中、EPUB 转 CBZ、坏文件、转不了时留 EPUB)、缓存统计。
|
||||
|
||||
@module tests.moonsource_kmoe.source_spec
|
||||
--]]
|
||||
@@ -52,6 +52,122 @@ function fake.downUrlAsync(_self, origin, cb)
|
||||
return { cancel = function() end }
|
||||
end
|
||||
|
||||
local archives = {}
|
||||
local function u16(n) return string.char(n % 256, math.floor(n / 256) % 256) end
|
||||
local function u32(n)
|
||||
return string.char(n % 256, math.floor(n / 256) % 256, math.floor(n / 65536) % 256, math.floor(n / 16777216) % 256)
|
||||
end
|
||||
--- 测试用的 store zip。桩阅读器只认这种头,不跑 libarchive。
|
||||
local function zipStore(files)
|
||||
local parts, central, offset = {}, {}, 0
|
||||
for _, file in ipairs(files) do
|
||||
local name, data = file[1], file[2]
|
||||
local local_hdr = "PK\3\4" .. u16(20) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
||||
.. u32(0) .. u32(#data) .. u32(#data) .. u16(#name) .. u16(0) .. name
|
||||
parts[#parts + 1] = local_hdr .. data
|
||||
central[#central + 1] = "PK\1\2" .. u16(20) .. u16(20) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
||||
.. u32(0) .. u32(#data) .. u32(#data) .. u16(#name) .. u16(0) .. u16(0) .. u16(0) .. u16(0)
|
||||
.. u32(0) .. u32(offset) .. name
|
||||
offset = offset + #local_hdr + #data
|
||||
end
|
||||
local central_blob = table.concat(central)
|
||||
return table.concat(parts) .. central_blob .. "PK\5\6" .. u16(0) .. u16(0) .. u16(#files) .. u16(#files)
|
||||
.. u32(#central_blob) .. u32(offset) .. u16(0)
|
||||
end
|
||||
local function readZip(blob)
|
||||
if #blob < 22 then return nil end
|
||||
local entries, i = {}, 1
|
||||
while i + 3 <= #blob do
|
||||
local sig = blob:sub(i, i + 3)
|
||||
if sig == "PK\1\2" or sig == "PK\5\6" then return entries end
|
||||
if sig ~= "PK\3\4" or i + 29 > #blob then return nil end
|
||||
local name_len = blob:byte(i + 26) + blob:byte(i + 27) * 256
|
||||
local extra_len = blob:byte(i + 28) + blob:byte(i + 29) * 256
|
||||
local size = blob:byte(i + 18) + blob:byte(i + 19) * 256 + blob:byte(i + 20) * 65536 + blob:byte(i + 21) * 16777216
|
||||
local start = i + 30 + name_len + extra_len
|
||||
if start - 1 + size > #blob then return nil end
|
||||
local name = blob:sub(i + 30, i + 29 + name_len)
|
||||
entries[#entries + 1] = {
|
||||
path = name, mode = "file", size = size, index = #entries + 1,
|
||||
data = blob:sub(start, start + size - 1),
|
||||
}
|
||||
i = start + size
|
||||
end
|
||||
return nil
|
||||
end
|
||||
package.preload["ffi/archiver"] = function()
|
||||
local Reader = {}
|
||||
Reader.__index = Reader
|
||||
function Reader:new() return setmetatable({ index = 0, entries = {} }, Reader) end
|
||||
function Reader:open(path)
|
||||
local f = io.open(path, "rb")
|
||||
if not f then self.err = "open"; return nil end
|
||||
local blob = f:read("*a")
|
||||
f:close()
|
||||
local list = readZip(blob)
|
||||
if not list then self.err = "bad"; return nil end
|
||||
self._list, self.entries, self.index = list, {}, 0
|
||||
for _, entry in ipairs(list) do
|
||||
self.entries[entry.path] = entry
|
||||
self.entries[entry.index] = entry
|
||||
end
|
||||
return true
|
||||
end
|
||||
function Reader:next()
|
||||
self.index = self.index + 1
|
||||
return self._list[self.index]
|
||||
end
|
||||
function Reader:iterate()
|
||||
self.index = 0
|
||||
return self.next, self
|
||||
end
|
||||
function Reader:extractToMemory(key)
|
||||
local entry = self.entries[key]
|
||||
return entry and entry.data or nil
|
||||
end
|
||||
function Reader:close() end
|
||||
local Writer = {}
|
||||
Writer.__index = Writer
|
||||
function Writer:new() return setmetatable({ files = {} }, Writer) end
|
||||
function Writer:open(path) self.path = path; self.files = {}; return true end
|
||||
function Writer:setZipCompression(method)
|
||||
self.method = method
|
||||
return method == "store"
|
||||
end
|
||||
function Writer:addFileFromMemory(name, data)
|
||||
self.files[#self.files + 1] = { name = name, data = data }
|
||||
return true
|
||||
end
|
||||
function Writer:close()
|
||||
if not self.path then return end
|
||||
archives[self.path] = { method = self.method, files = self.files }
|
||||
local f = io.open(self.path, "wb")
|
||||
if f then f:write("PK\3\4stub"); f:close() end
|
||||
end
|
||||
return { Reader = Reader, Writer = Writer }
|
||||
end
|
||||
|
||||
local JPG, PNG = "jpeg-page-one", "png-page-two"
|
||||
local function comic(rotate)
|
||||
local mark = rotate and ' kmoetag="rotate:' .. rotate .. '"' or ' kmoetag="rotate:0"'
|
||||
local opf = [[
|
||||
<package><manifest>
|
||||
<item id="Page_2" href="html/b.html" media-type="application/xhtml+xml"/>
|
||||
<item id="Page_1" href="html/a.html" media-type="application/xhtml+xml"/>
|
||||
</manifest><spine>
|
||||
<itemref idref="Page_1" />
|
||||
<itemref idref="Page_2" />
|
||||
</spine></package>
|
||||
]]
|
||||
return zipStore({
|
||||
{ "image/zzz.png", PNG },
|
||||
{ "html/b.html", '<html><body><img src="../image/zzz.png" class="singlePage"/></body></html>' },
|
||||
{ "image/aaa.jpg", JPG },
|
||||
{ "html/a.html", '<html><body><img src="../image/aaa.jpg" class="singlePage"' .. mark .. "/></body></html>" },
|
||||
{ "vol.opf", opf },
|
||||
})
|
||||
end
|
||||
|
||||
local Kmoe = require("moonsource_kmoe.source")
|
||||
local Chapter = require("moonsource_kmoe.chapter")
|
||||
local src = Kmoe.new()
|
||||
@@ -111,7 +227,7 @@ do
|
||||
Assert.len(downloads, 0)
|
||||
end
|
||||
|
||||
-- 下到的不是 zip 就删掉报错;是 EPUB 就交出路径,下次直接复用不再下载。
|
||||
-- 下到的不是 zip 就删掉报错。能打开但不是漫画的 zip 留下 EPUB。
|
||||
do
|
||||
down_body = "<html>額度不足</html>"
|
||||
local path, err
|
||||
@@ -122,18 +238,69 @@ do
|
||||
|
||||
down_body = "PK\3\4epub"
|
||||
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p, e) path, err = p, e end)
|
||||
Assert.eq(path, WORK .. "/de34d9/3086.epub")
|
||||
Assert.eq(downloads[#downloads].url, "https://dl.example/o")
|
||||
Assert.is_nil(path)
|
||||
Assert.matches(err, "EPUB")
|
||||
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "截断的 zip 不留")
|
||||
|
||||
down_body = zipStore({ { "readme.txt", "x" } })
|
||||
Chapter.materializeAsync(fake, identity, { uid = "plain", origin = "o" }, nil, function(p, e) path, err = p, e end)
|
||||
Assert.eq(path, WORK .. "/de34d9/plain.epub", "没有页序就继续用 EPUB")
|
||||
Assert.is_nil(io.open(WORK .. "/de34d9/plain.cbz", "rb"))
|
||||
end
|
||||
|
||||
-- 页序跟 spine,不跟压缩包里的文件名。转完删 EPUB,已有 CBZ 不再下载。
|
||||
do
|
||||
down_body = comic(0)
|
||||
local path, err
|
||||
local phases = {}
|
||||
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, function()
|
||||
phases[#phases + 1] = "convert"
|
||||
end, function(p, e) path, err = p, e end)
|
||||
Assert.is_nil(err)
|
||||
Assert.eq(path, WORK .. "/de34d9/3086.cbz")
|
||||
Assert.eq(phases[1], "convert")
|
||||
Assert.is_nil(io.open(WORK .. "/de34d9/3086.epub", "rb"), "转完不留 EPUB")
|
||||
local packed = archives[WORK .. "/de34d9/3086.cbz.part"]
|
||||
Assert.eq(packed.method, "store")
|
||||
Assert.eq(packed.files[1].name, "0001.jpg")
|
||||
Assert.eq(packed.files[1].data, JPG)
|
||||
Assert.eq(packed.files[2].name, "0002.png")
|
||||
Assert.eq(packed.files[2].data, PNG)
|
||||
local n = #downloads
|
||||
Chapter.materializeAsync(fake, identity, { uid = "3086", origin = "o" }, nil, function(p) path = p end)
|
||||
Stubs.flush()
|
||||
Assert.eq(path, WORK .. "/de34d9/3086.epub")
|
||||
Assert.eq(#downloads, n, "已有文件不再下载")
|
||||
Assert.eq(path, WORK .. "/de34d9/3086.cbz")
|
||||
Assert.eq(#downloads, n, "已有 CBZ 不再下载")
|
||||
end
|
||||
|
||||
-- 磁盘上的旧 EPUB 离线转换,不请求下载地址。非 0 旋转转不了,EPUB 还在。
|
||||
do
|
||||
local n = #downloads
|
||||
local legacy = WORK .. "/de34d9/old.epub"
|
||||
local f = assert(io.open(legacy, "wb"))
|
||||
f:write(comic(0))
|
||||
f:close()
|
||||
local path
|
||||
Chapter.materializeAsync(fake, identity, { uid = "old", origin = "o" }, nil, function(p) path = p end)
|
||||
Stubs.flush()
|
||||
Assert.eq(path, WORK .. "/de34d9/old.cbz")
|
||||
Assert.eq(#downloads, n, "旧 EPUB 不重新下载")
|
||||
Assert.is_nil(io.open(legacy, "rb"))
|
||||
|
||||
local turned = WORK .. "/de34d9/turn.epub"
|
||||
f = assert(io.open(turned, "wb"))
|
||||
f:write(comic(90))
|
||||
f:close()
|
||||
Chapter.materializeAsync(fake, identity, { uid = "turn", origin = "o" }, nil, function(p) path = p end)
|
||||
Stubs.flush()
|
||||
Assert.eq(path, turned, "旋转页仍打开 EPUB")
|
||||
Assert.not_nil(io.open(turned, "rb"))
|
||||
Assert.is_nil(io.open(WORK .. "/de34d9/turn.cbz", "rb"))
|
||||
end
|
||||
|
||||
-- 缓存一段:单卷失败不阻断,统计已缓存 / 失败。
|
||||
do
|
||||
down_body = "PK\3\4epub"
|
||||
down_body = comic(0)
|
||||
local toc = {
|
||||
{ uid = "a", origin = "o" }, { uid = "b", origin = "o", building = true }, { uid = "c", origin = "o" },
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user