<commit_before>'use strict'

module.exports = function (mongoose, config, db) {
    const utils = require('../lib/utils')()
        , fs = require('fs')
        , path = require('path');

    function fileResponse(file, _user) {
        let user = _user || file.user;
        return {
            id: file.id,
            name: file.name,
            user_id: user.id,
            user: {
                id: user.id,
                email: user.email,
                name: user.name
            },
            ext: file.ext
        };
    }

    return {
        upload(req, res) {
            if (req.file && req.file.originalname) {
                let ext = path.extname(req.file.originalname).toLowerCase();
                let tmp_path = req.file.path;
                db.Sequence.getNextSequence('files', (err, nextFileId) => {
                    if (err) return utils.error(res, 422, err);
                    let target_path = config.uploadPath + '/' + nextFileId + ext;
                    fs.copy(tmp_path, target_path, err => {
                        if (err) throw err;
                        fs.unlink(tmp_path, function () {
                            if (err) throw err;
                        });
                    });
                    const file = new db.File({
                        id: nextFileId,
                        name: req.file.originalname,
                        user: req.user,
                        ext,
                    });
                    file.save(err => err ?
                        utils.error(res, 422, err.message) :
                        utils.responseData(res, "update success!", fileResponse(file, req.user)));
                });
            } else {
                utils.error(res, 422, "Are you upload some files?");
            }
        },

        delete(req, res) {<commit_msg>解决文件上传失败的问题<commit_after>'use strict'

module.exports = function (mongoose, config, db) {
    const utils = require('../lib/utils')()
        , fs = require('fs')
        , path = require('path');

    function copy(source, target, cb) {
        let cbCalled = false;
        let rd = fs.createReadStream(source);
        rd.on("error", done);

        let wr = fs.createWriteStream(target);
        wr.on("error", done);
        wr.on("close", ex => cb());
        rd.pipe(wr);

        function done(err) {
            if (cbCalled) return;
            cb(err);
            cbCalled = true;
        }
    }

    function fileResponse(file, _user) {
        let user = _user || file.user;
        return {
            id: file.id,
            name: file.name,
            user_id: user.id,
            user: {
                id: user.id,
                email: user.email,
                name: user.name
            },
            ext: file.ext
        };
    }

    return {
        upload(req, res) {
            if (req.file && req.file.originalname) {
                let ext = path.extname(req.file.originalname).toLowerCase();
                let tmp_path = req.file.path;
                db.Sequence.getNextSequence('files', (err, nextFileId) => {
                    if (err) return db.Sequence.SequenceRollback('files', () => utils.error(res, 422, err));
                    let target_path = config.uploadPath + '/' + nextFileId + ext;
                    copy(tmp_path, target_path, err => {
                        if (err) return db.Sequence.SequenceRollback('files', () => utils.error(res, 422, err));
                        fs.unlink(tmp_path, () => {
                            if (err) return db.Sequence.SequenceRollback('files', () => utils.error(res, 422, err));
                            const file = new db.File({
                                id: nextFileId,
                                name: req.file.originalname,
                                user: req.user,
                                ext,
                            });
                            file.save(err => err ?
                                utils.error(res, 422, err) :
                                utils.responseData(res, "update success!", fileResponse(file, req.user)));
                        });
                    });
                });
            } else {
                utils.error(res, 422, "Are you upload some files?");
            }
        },

        delete(req, res) {
{'fraction_non_alphanumeric': 0.12304866850321396, 'fraction_numerical': 0.005509641873278237, 'mean_word_length': 1.1326480665687715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3174798', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1138, 'n_words': 249}