move zip util function
This commit is contained in:
parent
4f69543d78
commit
ead51b5dfe
@ -1,45 +1,10 @@
|
|||||||
|
|
||||||
import {Context, DefaultContext, DefaultState, Next} from 'koa';
|
import {Context, DefaultContext, DefaultState, Next} from 'koa';
|
||||||
import StreamZip, { ZipEntry } from 'node-stream-zip';
|
import {readZip,entriesByNaturalOrder,createReadStreamFromZip} from '../util/zipwrap';
|
||||||
import {orderBy} from 'natural-orderby';
|
|
||||||
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';
|
||||||
|
|
||||||
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){
|
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'];
|
||||||
let zip = await readZip(path);
|
let zip = await readZip(path);
|
||||||
|
36
src/util/zipwrap.ts
Normal file
36
src/util/zipwrap.ts
Normal 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);
|
||||||
|
}
|
||||||
|
});}
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user