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>{
if(this.desc !== undefined) return;
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;
const entry = entries["desc.json"];
if(entry === undefined){

View File

@ -3,17 +3,17 @@ import {
createReadableStreamFromZip,
entriesByNaturalOrder,
readZip,
ZipAsync,
} from "../util/zipwrap";
import { since_last_modified } from "./util";
import { ContentContext } from "./context";
import Router from "koa-router";
import StreamZip from "node-stream-zip";
/**
* zip stream cache.
*/
let ZipStreamCache: { [path: string]: [StreamZip, number] } = {};
let ZipStreamCache: { [path: string]: [ZipAsync, number] } = {};
async function acquireZip(path: string) {
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"];
//console.log(`opened ${page}`);
let zip = await acquireZip(path);
const entries = entriesByNaturalOrder(zip).filter((x) => {
const entries = (await entriesByNaturalOrder(zip)).filter((x) => {
const ext = x.name.split(".").pop();
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 { ReadStream } from 'fs';
import StreamZip from 'node-stream-zip';
export async function readZip(path : string):Promise<StreamZip>{
return new Promise((resolve,reject)=>{
let zip = new StreamZip({
file:path,
storeEntries: true
});
zip.on('error',(err)=>{
console.error(`read zip file ${path}`);
reject(err);
});
zip.on('ready',()=>{
resolve(zip);
});
}
);
export type ZipAsync = InstanceType<typeof StreamZip.async>;
export async function readZip(path : string): Promise<ZipAsync>{
return new StreamZip.async({
file:path,
storeEntries: true
});
}
export function entriesByNaturalOrder(zip: StreamZip){
const entries = zip.entries();
export async function entriesByNaturalOrder(zip: ZipAsync){
const entries = await zip.entries();
const ret = orderBy(Object.values(entries),v=>v.name);
return ret;
}
export async function createReadableStreamFromZip(zip:StreamZip,entry: ZipEntry):Promise<NodeJS.ReadableStream>{
return new Promise((resolve,reject)=>{
zip.stream(entry,(err, stream)=>{
if(stream !== undefined){
resolve(stream);
}
else{
reject(err);
}
});}
);
export async function createReadableStreamFromZip(zip:ZipAsync,entry: ZipEntry):Promise<NodeJS.ReadableStream>{
return await zip.stream(entry);
}
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 chunks:Uint8Array[] = [];
return new Promise((resolve,reject)=>{