From 190e669322b4c6135bf631f0c7cf622d60cdddd5 Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 12 Jan 2021 03:12:50 +0900 Subject: [PATCH] rename File and getDesc --- src/content/{referrer.ts => file.ts} | 17 ++++++++-------- src/content/manga.ts | 29 +++++++++++++++++++--------- src/content/mod.ts | 2 +- src/content/video.ts | 4 ++-- src/util/zipwrap.ts | 9 +++++++++ 5 files changed, 41 insertions(+), 20 deletions(-) rename src/content/{referrer.ts => file.ts} (81%) diff --git a/src/content/referrer.ts b/src/content/file.ts similarity index 81% rename from src/content/referrer.ts rename to src/content/file.ts index 4284a0d..894cbce 100644 --- a/src/content/referrer.ts +++ b/src/content/file.ts @@ -8,9 +8,9 @@ import {extname} from 'path'; */ export interface ContentFile{ getHash():Promise; + getDesc():Promise; readonly path: string; readonly type: string; - desc: object|undefined; } type ContentFileConstructor = (new (path:string,desc?:object) => ContentFile)&{content_type:string}; export const createDefaultClass = (type:string):ContentFileConstructor=>{ @@ -18,11 +18,12 @@ export const createDefaultClass = (type:string):ContentFileConstructor=>{ readonly path: string; type = type; static content_type = type; - - desc: object|undefined; - constructor(path:string,desc?:object){ + + constructor(path:string,option?:object){ this.path = path; - this.desc = desc; + } + async getDesc(): Promise { + return null; } async getHash():Promise{ const stat = await promises.stat(this.path); @@ -42,15 +43,15 @@ export function registerContentReferrer(s: ContentFileConstructor){ console.log(`registered content type: ${s.content_type}`) ContstructorTable[s.content_type] = s; } -export function createContentReferrer(type:string,path:string,desc?:object){ +export function createContentFile(type:string,path:string,option?:object){ const constructorMethod = ContstructorTable[type]; if(constructorMethod === undefined){ console.log(type); throw new Error("undefined"); } - return new constructorMethod(path,desc); + return new constructorMethod(path,option); } -export function getContentRefererConstructor(type:string): ContentFileConstructor|undefined{ +export function getContentFileConstructor(type:string): ContentFileConstructor|undefined{ const ret = ContstructorTable[type]; return ret; } \ No newline at end of file diff --git a/src/content/manga.ts b/src/content/manga.ts index 66f54f3..3e4e433 100644 --- a/src/content/manga.ts +++ b/src/content/manga.ts @@ -1,14 +1,25 @@ -import {ContentFile} from './referrer'; -import {createDefaultClass,registerContentReferrer} from './referrer'; -import {readZip} from '../util/zipwrap'; +import {ContentFile} from './file'; +import {createDefaultClass,registerContentReferrer} from './file'; +import {readZip,createReadStreamFromZip, readAllFromZip} from '../util/zipwrap'; export class MangaReferrer extends createDefaultClass("manga"){ - constructor(path:string){ + desc: object|null|undefined; + constructor(path:string,option?:object|undefined){ super(path); - /*(async ()=>{ - const zip = await readZip(path); - const entry = zip.entries(); - - })*/ + } + async getDesc(){ + if(this.desc !== undefined){ + return this.desc; + } + const zip = await readZip(this.path); + const entry = zip.entry("desc.json"); + if(entry === undefined){ + this.desc = null; + return this.desc; + } + const data = (await readAllFromZip(zip,entry)).toString('utf-8'); + this.desc = JSON.parse(data); + if(this.desc === undefined) throw new Error("??? JSON.parse is returning undefined"); + return this.desc; } }; registerContentReferrer(MangaReferrer); \ No newline at end of file diff --git a/src/content/mod.ts b/src/content/mod.ts index 9bef609..df6fe69 100644 --- a/src/content/mod.ts +++ b/src/content/mod.ts @@ -1,3 +1,3 @@ import './manga'; import './video'; -export {ContentFile as ContentReferrer,createContentReferrer} from './referrer'; \ No newline at end of file +export {ContentFile, createContentFile} from './file'; \ No newline at end of file diff --git a/src/content/video.ts b/src/content/video.ts index 06a206c..6d2ca2b 100644 --- a/src/content/video.ts +++ b/src/content/video.ts @@ -1,5 +1,5 @@ -import {ContentFile, registerContentReferrer} from './referrer'; -import {createDefaultClass} from './referrer'; +import {ContentFile, registerContentReferrer} from './file'; +import {createDefaultClass} from './file'; export class VideoReferrer extends createDefaultClass("video"){ constructor(path:string,desc?:object){ diff --git a/src/util/zipwrap.ts b/src/util/zipwrap.ts index 505ff81..ee7e3bc 100644 --- a/src/util/zipwrap.ts +++ b/src/util/zipwrap.ts @@ -33,4 +33,13 @@ export async function createReadStreamFromZip(zip:StreamZip,entry: ZipEntry):Pro } });} ); +} +export async function readAllFromZip(zip:StreamZip,entry: ZipEntry):Promise{ + const stream = await createReadStreamFromZip(zip,entry); + const chunks:Uint8Array[] = []; + return new Promise((resolve,reject)=>{ + stream.on('data',(data)=>{chunks.push(data)}); + stream.on('error', (err)=>reject(err)); + stream.on('end',()=>resolve(Buffer.concat(chunks))); + }); } \ No newline at end of file