Teamlinker/code/server/file/service/file.ts
sx1989827 d4874f0472 init
2023-06-20 14:40:22 +08:00

60 lines
2.0 KiB
TypeScript

import * as fs from "fs-extra";
import Application from "../../common/app/app";
import {Entity} from "../../common/entity/entity";
import {fileModel} from './../../../common/model/file';
import {fileMapper} from './../mapper/file';
import {IServer_Common_Http_Req_File} from "../../common/types/http";
import {emitServiceEvent} from "../../common/event/event";
import path = require("path");
export default class File extends Entity<typeof fileModel,typeof fileMapper> {
constructor(){
super(fileMapper)
}
static async getItemByMd5(md5:string){
let obj = await fileMapper.getItemByMd5(md5);
if(obj) {
let file = new File;
file.setItem(obj);
return file;
} else {
return null;
}
}
async upload(file:IServer_Common_Http_Req_File,meta?:string){
let arr=(new Date).toLocaleDateString().split("/").reverse()
if(arr[2].length==1) {
arr[2]="0"+arr[2]
}
let dirPath = path.join(Application.uploadPath,arr[0]+arr[2]+arr[1])
let filePath=path.join(dirPath,file.md5)+file.fileName.substr(file.fileName.lastIndexOf("."));
let dbPath=filePath.substring(Application.uploadPath.length);
await fs.ensureDir(dirPath)
fs.writeFile(filePath,file.data)
this.item.path=dbPath
await super.create()
if(meta) {
emitServiceEvent("fileUpload",meta,this.item.id)
}
return this.item.id;
}
static async getPaths(ids:string[]){
let arrId=[...ids]
let ret=await fileMapper.getPaths(ids);
let arrRetId=ret.map(item=>{
return item.id
})
for(let i=0;i<arrId.length;i++) {
if(arrId[i] && arrRetId.includes(arrId[i])) {
let obj=ret[arrRetId.indexOf(arrId[i])]
if(obj && obj.path) {
arrId[i]="/file"+obj.path
}
} else {
arrId[i]=null
}
}
return arrId;
}
}