- 实现面向对象的模板管理系统 - 添加N8n模板抽象类和具体实现 - 创建老照片修复上色模板 - 创建修人物表情闭眼模板 - 集成BowongAI服务 - 添加完整的Docker配置(多阶段构建) - 添加docker-compose配置 - 完善项目文档和API设计
71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { Injectable } from "@nestjs/common";
|
|
|
|
// 基础模板抽象类
|
|
export abstract class Template {
|
|
abstract readonly code: string; // 模板唯一标识码
|
|
abstract readonly name: string; // 模板名称
|
|
abstract readonly description: string; // 模板详细描述
|
|
abstract readonly creditCost: number; // 积分消耗
|
|
abstract readonly version: string; // 版本号
|
|
abstract readonly input: string; // 原始图片
|
|
abstract readonly output: string; // 输出图片
|
|
abstract readonly tags: string[]; // 标签数组
|
|
}
|
|
|
|
export type VideoGenerateResultUrl = string;
|
|
|
|
// 图片生成模板抽象类
|
|
export abstract class ImageGenerateTemplate extends Template {
|
|
abstract execute(imageUrl: string): Promise<string>;
|
|
}
|
|
|
|
// 视频生成模板抽象类
|
|
export abstract class VideoGenerateTemplate extends Template {
|
|
abstract execute(imageUrl: string): Promise<string>;
|
|
}
|
|
|
|
// 模板管理器类
|
|
@Injectable()
|
|
export class TemplateManager {
|
|
private readonly templates = new Map<string, Template>();
|
|
|
|
// 注册模板
|
|
registerTemplate(template: Template): void {
|
|
this.templates.set(template.code, template);
|
|
console.log(`模板 ${template.code} 注册成功`);
|
|
}
|
|
|
|
// 批量注册模板
|
|
registerTemplates(templates: Template[]): void {
|
|
templates.forEach(template => this.registerTemplate(template));
|
|
}
|
|
|
|
// 获取模板
|
|
getTemplate(templateCode: string): Template | null {
|
|
return this.templates.get(templateCode) || null;
|
|
}
|
|
|
|
// 获取所有模板
|
|
getAllTemplates(): Template[] {
|
|
return Array.from(this.templates.values());
|
|
}
|
|
|
|
// 执行模板
|
|
async executeTemplate(templateCode: string, input: string): Promise<any> {
|
|
const template = this.getTemplate(templateCode);
|
|
|
|
if (!template) {
|
|
throw new Error(`模板 ${templateCode} 不存在`);
|
|
}
|
|
|
|
if (template instanceof ImageGenerateTemplate) {
|
|
return await template.execute(input);
|
|
}
|
|
|
|
if (template instanceof VideoGenerateTemplate) {
|
|
return await template.execute(input);
|
|
}
|
|
|
|
throw new Error(`未知的模板类型: ${templateCode}`);
|
|
}
|
|
} |