import { Instance,closeConnection,getConnection} from "noomi"; import { EntityManager,getEntityManager } from "relaen"; import { AmFile } from "../../dao/entity/base/amfile"; import { Util } from "./util"; @Instance() export class __AmFileService{ /** * 添加文件 * @param fn 文件名 * @param path 路径 */ public async addFile(fn:string,path:string){ let file:AmFile = new AmFile(); file.fileName = fn; file.downPath = Util.getDownPath(path); file.savePath = Util.getAbsPath(file.downPath.split('/')); file = await file.save(); return { fileId:file.fileId, downPath:file.downPath, fileName:fn } } /** * 删除文件 * @param id */ public async deleteFile(id:number){ let file:AmFile = await AmFile.find(id); if(file){ await AmFile.delete(file); //从文件系统删除 try{ require('fs').unlinkSync(file.savePath,true); }catch(e){ } } } //获取文件列表 public async getFileList(page: number, rows: number) { const conn = await getConnection(); const em:EntityManager = await getEntityManager(); let query = await em.createQuery(File.name); let files = await query .select(['*']) .getResultList((page-1)*rows, rows); let count = await AmFile.getCount(); await em.close(); await closeConnection(); return { total: count, rows: files}; } }