rename File and getDesc

This commit is contained in:
monoid 2021-01-12 03:12:50 +09:00
parent 61a7a4b651
commit 190e669322
5 changed files with 41 additions and 20 deletions

View File

@ -8,9 +8,9 @@ import {extname} from 'path';
*/
export interface ContentFile{
getHash():Promise<string>;
getDesc():Promise<object|null>;
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=>{
@ -19,10 +19,11 @@ export const createDefaultClass = (type:string):ContentFileConstructor=>{
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<object|null> {
return null;
}
async getHash():Promise<string>{
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;
}

View File

@ -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);

View File

@ -1,3 +1,3 @@
import './manga';
import './video';
export {ContentFile as ContentReferrer,createContentReferrer} from './referrer';
export {ContentFile, createContentFile} from './file';

View File

@ -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){

View File

@ -34,3 +34,12 @@ export async function createReadStreamFromZip(zip:StreamZip,entry: ZipEntry):Pro
});}
);
}
export async function readAllFromZip(zip:StreamZip,entry: ZipEntry):Promise<Buffer>{
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)));
});
}