import { TABLEID, TABLENAME } from "../config/enum/dbEnum";
import { randomId } from "../tools/system";


/**
 * 文件上传
 */
const fs = require('fs');
const path = require('path');

/**
 * 上传文件
 * @param files 
 * @param type 
 * @returns 
 */
export async function upFile(files, type) {
    let pngId = randomId(TABLEID.图片存储);
    let upUrl = path.join(__dirname.substring(0,__dirname.indexOf("out")), 'files', 'honor');
    let stats = fs.existsSync(upUrl);
    if (!stats) fs.mkdirSync(upUrl);

    let fileName = `${pngId}${type}`;
   
    fs.renameSync(files.file.path, path.join(upUrl, `${fileName}` ));

    return {isSuccess:true, url:`/honor/${fileName}`, fileName};
}