<commit_before>const OSS = require('ali-oss');
const fs = require('fs');
const path = require('path');
const { uuid } = require('../../util/inedx');
const oss_config = require('../../config/ali_oss');
// 初始化Client
const client = new OSS(oss_config);
module.exports = {
    upFileForLocal: async (req, res) => {
        let writeStream = [], url = [], promise = [], max_size = 600 * 1024, up_flag;
        let {type} = reqBody(req);
        if (req.files.length <= 0) {
            res.json({status: 400, msg: '未上传文件'});
            return false;
        }
        req.files.map(item => {
            up_flag = item.size > max_size;
            let file_type = item.originalname.split('.')[1];
            let id = uuid(36);
            //创建真实文件
            let write_file = path.resolve('fileTemp', `${type ? type : id}.${file_type}`);
            // 本地缓存数据
            let local_file = path.resolve('fileTemp', item.filename);
            let origin_stream = fs.createReadStream(local_file);
            let write_stream = fs.createWriteStream(write_file);
            writeStream.push({
                local_file, origin_stream, write_stream
            });
            url.push(`/img/${type ? type : id}.${file_type}`);
        });
        writeStream.map(item => {
            promise.push(new Promise(resolve => {
                item.origin_stream.pipe(item.write_stream);
                item.origin_stream.on('end', () => {
                    resolve(true);
                });
            }))
        });
        const flag = await Promise.all(promise);
        if (!flag.includes('false')) {
            writeStream.map(item => {
                fs.unlinkSync(item.local_file);
            });
            res.json({code: 200, message: 'success', url})
        }
    },
    upFileForOss: (req, res) => {
        let file_arr = [], result_arr = [], promise = [];
        if (req.files.length <= 0) {
            res.json({status: 400, msg: '未上传文件'});
            return false;
        }
        req.files.map(item => {
            let key = 'articleImg/' + item.originalname;
            let or_file = path.resolve("fileTemp", item.filename);
            let stream = fs.createReadStream(or_file);
            promise.push(client.putStream(key, stream));
            file_arr.push(or_file);
        });
        Promise.all(promise).then(data => {
            data.map((item,index) => {
                result_arr.push(item.url);
                fs.unlinkSync(file_arr[index]);
            });<commit_msg>oss上传支持自定义文件名<commit_after>const OSS = require('ali-oss');
const fs = require('fs');
const path = require('path');
const { uuid } = require('../../util/inedx');
const oss_config = require('../../config/ali_oss');
// 初始化Client
const client = new OSS(oss_config);
module.exports = {
    upFileForLocal: async (req, res) => {
        let writeStream = [], url = [], promise = [], max_size = 600 * 1024, up_flag;
        let {type} = reqBody(req);
        if (req.files.length <= 0) {
            res.json({status: 400, msg: '未上传文件'});
            return false;
        }
        req.files.map(item => {
            up_flag = item.size > max_size;
            let file_type = item.originalname.split('.')[1];
            let id = uuid(36);
            //创建真实文件
            let write_file = path.resolve('fileTemp', `${type ? type : id}.${file_type}`);
            // 本地缓存数据
            let local_file = path.resolve('fileTemp', item.filename);
            let origin_stream = fs.createReadStream(local_file);
            let write_stream = fs.createWriteStream(write_file);
            writeStream.push({
                local_file, origin_stream, write_stream
            });
            url.push(`/img/${type ? type : id}.${file_type}`);
        });
        writeStream.map(item => {
            promise.push(new Promise(resolve => {
                item.origin_stream.pipe(item.write_stream);
                item.origin_stream.on('end', () => {
                    resolve(true);
                });
            }))
        });
        const flag = await Promise.all(promise);
        if (!flag.includes('false')) {
            writeStream.map(item => {
                fs.unlinkSync(item.local_file);
            });
            res.json({code: 200, message: 'success', url})
        }
    },
    upFileForOss: (req, res) => {
        let file_arr = [], result_arr = [], promise = [];
        let {type} = reqBody(req);
        if (req.files.length <= 0) {
            res.json({status: 400, msg: '未上传文件'});
            return false;
        }
        req.files.map(item => {
            let file_type = item.originalname.split('.')[1];
            let fileName = type ? `${type}.${file_type}` : item.originalname ;
            let key = `articleImg/${fileName}`;
            let or_file = path.resolve("fileTemp", item.filename);
            let stream = fs.createReadStream(or_file);
            promise.push(client.putStream(key, stream));
            file_arr.push(or_file);
        });
        Promise.all(promise).then(data => {
            data.map((item,index) => {
                result_arr.push(item.url);
                fs.unlinkSync(file_arr[index]);
            });
{'fraction_non_alphanumeric': 0.1581986143187067, 'fraction_numerical': 0.008275596612779062, 'mean_word_length': 1.7910848549946294, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1040953', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1557, 'n_words': 278}