fix: use zip.async

This commit is contained in:
monoid 2022-04-28 13:40:16 +09:00
parent 90193e7ac6
commit 0963cdb5c4
3 changed files with 19 additions and 34 deletions

View File

@ -26,7 +26,7 @@ export class ComicReferrer extends createDefaultClass("comic"){
async initDesc():Promise<void>{ async initDesc():Promise<void>{
if(this.desc !== undefined) return; if(this.desc !== undefined) return;
const zip = await readZip(this.path); const zip = await readZip(this.path);
const entries = zip.entries(); const entries = await zip.entries();
this.pagenum = Object.keys(entries).filter(x=>ImageExt.includes(extname(x))).length; this.pagenum = Object.keys(entries).filter(x=>ImageExt.includes(extname(x))).length;
const entry = entries["desc.json"]; const entry = entries["desc.json"];
if(entry === undefined){ if(entry === undefined){

View File

@ -3,17 +3,17 @@ import {
createReadableStreamFromZip, createReadableStreamFromZip,
entriesByNaturalOrder, entriesByNaturalOrder,
readZip, readZip,
ZipAsync,
} from "../util/zipwrap"; } from "../util/zipwrap";
import { since_last_modified } from "./util"; import { since_last_modified } from "./util";
import { ContentContext } from "./context"; import { ContentContext } from "./context";
import Router from "koa-router"; import Router from "koa-router";
import StreamZip from "node-stream-zip";
/** /**
* zip stream cache. * zip stream cache.
*/ */
let ZipStreamCache: { [path: string]: [StreamZip, number] } = {}; let ZipStreamCache: { [path: string]: [ZipAsync, number] } = {};
async function acquireZip(path: string) { async function acquireZip(path: string) {
if (ZipStreamCache[path] === undefined) { if (ZipStreamCache[path] === undefined) {
@ -48,7 +48,7 @@ async function renderZipImage(ctx: Context, path: string, page: number) {
const image_ext = ["gif", "png", "jpeg", "bmp", "webp", "jpg"]; const image_ext = ["gif", "png", "jpeg", "bmp", "webp", "jpg"];
//console.log(`opened ${page}`); //console.log(`opened ${page}`);
let zip = await acquireZip(path); let zip = await acquireZip(path);
const entries = entriesByNaturalOrder(zip).filter((x) => { const entries = (await entriesByNaturalOrder(zip)).filter((x) => {
const ext = x.name.split(".").pop(); const ext = x.name.split(".").pop();
return ext !== undefined && image_ext.includes(ext); return ext !== undefined && image_ext.includes(ext);
}); });

View File

@ -1,41 +1,26 @@
import StreamZip, { ZipEntry } from 'node-stream-zip'; import { ZipEntry } from 'node-stream-zip';
import {orderBy} from 'natural-orderby'; import {orderBy} from 'natural-orderby';
import { ReadStream } from 'fs'; import { ReadStream } from 'fs';
import StreamZip from 'node-stream-zip';
export async function readZip(path : string):Promise<StreamZip>{ export type ZipAsync = InstanceType<typeof StreamZip.async>;
return new Promise((resolve,reject)=>{ export async function readZip(path : string): Promise<ZipAsync>{
let zip = new StreamZip({ return new StreamZip.async({
file:path, file:path,
storeEntries: true storeEntries: true
}); });
zip.on('error',(err)=>{
console.error(`read zip file ${path}`);
reject(err);
});
zip.on('ready',()=>{
resolve(zip);
});
}
);
} }
export function entriesByNaturalOrder(zip: StreamZip){ export async function entriesByNaturalOrder(zip: ZipAsync){
const entries = zip.entries(); const entries = await zip.entries();
const ret = orderBy(Object.values(entries),v=>v.name); const ret = orderBy(Object.values(entries),v=>v.name);
return ret; return ret;
} }
export async function createReadableStreamFromZip(zip:StreamZip,entry: ZipEntry):Promise<NodeJS.ReadableStream>{
return new Promise((resolve,reject)=>{ export async function createReadableStreamFromZip(zip:ZipAsync,entry: ZipEntry):Promise<NodeJS.ReadableStream>{
zip.stream(entry,(err, stream)=>{ return await zip.stream(entry);
if(stream !== undefined){
resolve(stream);
}
else{
reject(err);
}
});}
);
} }
export async function readAllFromZip(zip:StreamZip,entry: ZipEntry):Promise<Buffer>{ export async function readAllFromZip(zip:ZipAsync,entry: ZipEntry):Promise<Buffer>{
const stream = await createReadableStreamFromZip(zip,entry); const stream = await createReadableStreamFromZip(zip,entry);
const chunks:Uint8Array[] = []; const chunks:Uint8Array[] = [];
return new Promise((resolve,reject)=>{ return new Promise((resolve,reject)=>{