Authored by Targaryen

upload

... ... @@ -62,7 +62,6 @@ class FileApi {
state: '校验文件通过,开始将文件上传到七牛...'
});
let upToQiniuResult = await this._uploadToQiniu(fileList, fileDistPath);
let fileModel = new FileModel();
let insertRes = await fileModel.insert({ // 上传记录写库
date: moment().format('YYYY-MM-DD HH:mm'),
... ... @@ -71,9 +70,7 @@ class FileApi {
username: '' // TODO
});
ws.broadcast(BROADCAST_PATH, {
state: '文件已经全部上传到七牛'
});
this._uploadToQiniu(fileList, fileDistPath);
} else {
ws.broadcast(BROADCAST_PATH, {
state: '校验文件失败,非法上传!'
... ... @@ -90,30 +87,29 @@ class FileApi {
async _uploadToQiniu(fileList, fileDistPath) {
let hasError = false;
fileList = _.map(fileList, perFilePath => {
return fileDistPath + _.split(perFilePath, fileDistPath)[1];
}); // 处理待上传的文件列表
_.remove(fileList, perFilePath => {
return !perFilePath || /__MACOSX/.test(perFilePath) || !fs.lstatSync(perFilePath).isFile();
});
for(let i = 0; i < fileList.length; i++) {
let fileKey = path.relative(FILE_SAVE_ROOT_PATH, fileList[i]);
let result = await qn.uploadFileAsync(fileList[i], {
key: path.join(QINIU_PREFIX, fileKey)
let fileKey = path.join(QINIU_PREFIX, path.relative(FILE_SAVE_ROOT_PATH, fileList[i]));
qn.uploadFileAsync(fileList[i], {
key: fileKey
}).then(result => {
if (result && result.url) {
ws.broadcast(BROADCAST_PATH, {
state: `${fileKey} 已经上传到七牛`
});
} else {
ws.broadcast(BROADCAST_PATH, {
state: `${fileKey} 上传到七牛出错`
});
}
});
if (result && result.url) {
ws.broadcast(BROADCAST_PATH, {
state: `${fileKey} 已经上传到七牛`
});
} else {
ws.broadcast(BROADCAST_PATH, {
state: `${fileKey} 上传到七牛出错`
});
}
}
return hasError;
}
/**
... ...