From ead51b5dfe0f3f9441effd475d3a617550a4a799 Mon Sep 17 00:00:00 2001 From: monoid Date: Mon, 11 Jan 2021 21:35:48 +0900 Subject: [PATCH] move zip util function --- src/route/manga.ts | 37 +------------------------------------ src/util/zipwrap.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 36 deletions(-) create mode 100644 src/util/zipwrap.ts diff --git a/src/route/manga.ts b/src/route/manga.ts index 610baf2..a109c2a 100644 --- a/src/route/manga.ts +++ b/src/route/manga.ts @@ -1,45 +1,10 @@ import {Context, DefaultContext, DefaultState, Next} from 'koa'; -import StreamZip, { ZipEntry } from 'node-stream-zip'; -import {orderBy} from 'natural-orderby'; +import {readZip,entriesByNaturalOrder,createReadStreamFromZip} from '../util/zipwrap'; import {since_last_modified} from './util'; import {ContentContext} from './context'; import Router from 'koa-router'; -export async function readZip(path : string):Promise{ - 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 function entriesByNaturalOrder(zip: StreamZip){ - const entries = zip.entries(); - const ret = orderBy(Object.values(entries),v=>v.name); - return ret; -} -export async function createReadStreamFromZip(zip:StreamZip,entry: ZipEntry):Promise{ - return new Promise((resolve,reject)=>{ - zip.stream(entry,(err, stream)=>{ - if(stream !== undefined){ - resolve(stream); - } - else{ - reject(err); - } - });} - ); -} - async function renderZipImage(ctx: Context,path : string, page:number){ const image_ext = ['gif', 'png', 'jpeg', 'bmp', 'webp', 'jpg']; let zip = await readZip(path); diff --git a/src/util/zipwrap.ts b/src/util/zipwrap.ts new file mode 100644 index 0000000..505ff81 --- /dev/null +++ b/src/util/zipwrap.ts @@ -0,0 +1,36 @@ +import StreamZip, { ZipEntry } from 'node-stream-zip'; +import {orderBy} from 'natural-orderby'; + +export async function readZip(path : string):Promise{ + 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 function entriesByNaturalOrder(zip: StreamZip){ + const entries = zip.entries(); + const ret = orderBy(Object.values(entries),v=>v.name); + return ret; +} +export async function createReadStreamFromZip(zip:StreamZip,entry: ZipEntry):Promise{ + return new Promise((resolve,reject)=>{ + zip.stream(entry,(err, stream)=>{ + if(stream !== undefined){ + resolve(stream); + } + else{ + reject(err); + } + });} + ); +} \ No newline at end of file