add tag auto load
This commit is contained in:
parent
cade73da87
commit
cfd3e25952
@ -15,10 +15,7 @@ export interface ContentFile{
|
|||||||
readonly type: string;
|
readonly type: string;
|
||||||
}
|
}
|
||||||
export type ContentConstructOption = {
|
export type ContentConstructOption = {
|
||||||
hash: string,
|
hash: string
|
||||||
tags: string[],
|
|
||||||
title: string,
|
|
||||||
additional: JSONMap
|
|
||||||
}
|
}
|
||||||
type ContentFileConstructor = (new (path:string,option?:ContentConstructOption) => ContentFile)&{content_type:string};
|
type ContentFileConstructor = (new (path:string,option?:ContentConstructOption) => ContentFile)&{content_type:string};
|
||||||
export const createDefaultClass = (type:string):ContentFileConstructor=>{
|
export const createDefaultClass = (type:string):ContentFileConstructor=>{
|
||||||
@ -41,16 +38,13 @@ export const createDefaultClass = (type:string):ContentFileConstructor=>{
|
|||||||
content_type: cons.content_type,
|
content_type: cons.content_type,
|
||||||
filename: base,
|
filename: base,
|
||||||
tags: [],
|
tags: [],
|
||||||
content_hash: await this.getHash(),
|
content_hash: this.hash || await this.getHash(),
|
||||||
} as DocumentBody;
|
} as DocumentBody;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
get type():string{
|
get type():string{
|
||||||
return cons.content_type;
|
return cons.content_type;
|
||||||
}
|
}
|
||||||
async getDesc(): Promise<object|null> {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
async getHash():Promise<string>{
|
async getHash():Promise<string>{
|
||||||
if(this.hash !== undefined) return this.hash;
|
if(this.hash !== undefined) return this.hash;
|
||||||
const stat = await promises.stat(this.path);
|
const stat = await promises.stat(this.path);
|
||||||
|
@ -1,26 +1,65 @@
|
|||||||
import {ContentFile, createDefaultClass,registerContentReferrer, ContentConstructOption} from './file';
|
import {ContentFile, createDefaultClass,registerContentReferrer, ContentConstructOption} from './file';
|
||||||
import {readZip,createReadStreamFromZip, readAllFromZip} from '../util/zipwrap';
|
import {readZip,createReadStreamFromZip, readAllFromZip} from '../util/zipwrap';
|
||||||
|
import { DocumentBody } from '../model/doc';
|
||||||
|
import {extname} from 'path';
|
||||||
|
|
||||||
|
type MangaType = "doujinshi"|"artist cg"|"manga"|"western";
|
||||||
|
interface MangaDesc{
|
||||||
|
title:string,
|
||||||
|
artist?:string[],
|
||||||
|
group?:string[],
|
||||||
|
series?:string[],
|
||||||
|
type:MangaType|[MangaType],
|
||||||
|
character?:string[],
|
||||||
|
tags?:string[]
|
||||||
|
}
|
||||||
|
const ImageExt = ['.gif', '.png', '.jpeg', '.bmp', '.webp', '.jpg'];
|
||||||
export class MangaReferrer extends createDefaultClass("manga"){
|
export class MangaReferrer extends createDefaultClass("manga"){
|
||||||
desc: object|null|undefined;
|
desc: MangaDesc|undefined;
|
||||||
additional: object| undefined;
|
pagenum: number;
|
||||||
|
additional: ContentConstructOption| undefined;
|
||||||
constructor(path:string,option?:ContentConstructOption){
|
constructor(path:string,option?:ContentConstructOption){
|
||||||
super(path);
|
super(path);
|
||||||
this.additional = option;
|
this.additional = option;
|
||||||
|
this.pagenum = 0;
|
||||||
}
|
}
|
||||||
async getDesc(){
|
async initDesc():Promise<void>{
|
||||||
if(this.desc !== undefined){
|
if(this.desc !== undefined) return;
|
||||||
return this.desc;
|
|
||||||
}
|
|
||||||
const zip = await readZip(this.path);
|
const zip = await readZip(this.path);
|
||||||
const entry = zip.entry("desc.json");
|
const entries = zip.entries();
|
||||||
|
this.pagenum = Object.keys(entries).filter(x=>ImageExt.includes(extname(x))).length;
|
||||||
|
const entry = entries["desc.json"];
|
||||||
if(entry === undefined){
|
if(entry === undefined){
|
||||||
this.desc = null;
|
return;
|
||||||
return this.desc;
|
|
||||||
}
|
}
|
||||||
const data = (await readAllFromZip(zip,entry)).toString('utf-8');
|
const data = (await readAllFromZip(zip,entry)).toString('utf-8');
|
||||||
this.desc = JSON.parse(data);
|
this.desc = JSON.parse(data);
|
||||||
if(this.desc === undefined) throw new Error("??? JSON.parse is returning undefined");
|
if(this.desc === undefined)
|
||||||
return this.desc;
|
throw new Error(`??? JSON.parse is returning undefined. ${this.path} desc.json format error`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async createDocumentBody(): Promise<DocumentBody>{
|
||||||
|
await this.initDesc();
|
||||||
|
const basebody = await super.createDocumentBody();
|
||||||
|
this.desc?.title;
|
||||||
|
if(this.desc === undefined){
|
||||||
|
return basebody;
|
||||||
|
}
|
||||||
|
let tags:string[] = this.desc.tags || [];
|
||||||
|
tags = tags.concat(this.desc.artist?.map(x=>`artist:${x}`) || []);
|
||||||
|
tags = tags.concat(this.desc.character?.map(x=>`character:${x}`) || []);
|
||||||
|
tags = tags.concat(this.desc.group?.map(x=>`group:${x}`) || []);
|
||||||
|
tags = tags.concat(this.desc.series?.map(x=>`series:${x}`) || []);
|
||||||
|
const type = this.desc.type instanceof Array ? this.desc.type[0]: this.desc.type;
|
||||||
|
tags.push(`type:${type}`);
|
||||||
|
return {
|
||||||
|
...basebody,
|
||||||
|
title:this.desc.title,
|
||||||
|
additional:{
|
||||||
|
page:this.pagenum
|
||||||
|
},
|
||||||
|
tags:tags
|
||||||
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
registerContentReferrer(MangaReferrer);
|
registerContentReferrer(MangaReferrer);
|
Loading…
Reference in New Issue
Block a user