Authored by Targaryen

up-end

... ... @@ -70,7 +70,11 @@ class FileApi {
username: '' // TODO
});
this._uploadToQiniu(fileList, fileDistPath);
await this._uploadToQiniu(fileList, fileDistPath);
ws.broadcast(BROADCAST_PATH, {
state: '文件上传到七牛处理结束!'
});
} else {
ws.broadcast(BROADCAST_PATH, {
state: '校验文件失败,非法上传!'
... ... @@ -85,31 +89,30 @@ class FileApi {
* @param {待上传的文件夹路径} fileDistPath
*/
async _uploadToQiniu(fileList, fileDistPath) {
let hasError = false;
_.remove(fileList, perFilePath => {
return !perFilePath || /__MACOSX/.test(perFilePath) || !fs.lstatSync(perFilePath).isFile();
});
for(let i = 0; i < fileList.length; i++) {
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} 上传到七牛出错`
});
}
let fileListPromises = _.map(fileList, filepath => {
let fileKey = path.join(QINIU_PREFIX, path.relative(FILE_SAVE_ROOT_PATH, filepath));
return new Promise((resolve, reject) => {
qn.uploadFileAsync(filepath, {key:fileKey}).then(result => {
if (result && result.url) {
ws.broadcast(BROADCAST_PATH, {
state: `${filepath} 上传到七牛成功`
});
} else {
ws.broadcast(BROADCAST_PATH, {
state: `${filepath} 上传到七牛失败`
});
}
resolve(result);
});
});
}
});
return hasError;
return Promise.all(fileListPromises);
}
/**
... ...