move zip util function

This commit is contained in:
monoid 2021-01-11 21:35:48 +09:00
parent 4f69543d78
commit ead51b5dfe
2 changed files with 37 additions and 36 deletions

View File

@ -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<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 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<NodeJS.ReadableStream>{
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);

36
src/util/zipwrap.ts Normal file
View File

@ -0,0 +1,36 @@
import StreamZip, { ZipEntry } from 'node-stream-zip';
import {orderBy} from 'natural-orderby';
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 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<NodeJS.ReadableStream>{
return new Promise((resolve,reject)=>{
zip.stream(entry,(err, stream)=>{
if(stream !== undefined){
resolve(stream);
}
else{
reject(err);
}
});}
);
}