import { getPolicyBGImgId } from "../tools/system";

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

/**
 * 上传文件
 * @param files 
 * @param type 
 * @returns 
 */
export async function upFile(files, type) {
    let pngId = getPolicyBGImgId();
    let upUrl = path.join(__dirname.substring(0,__dirname.indexOf("out")), 'files', 'policy');
    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:`/policy/${fileName}`, fileName};
}