...
|
...
|
@@ -12,9 +12,10 @@ const rd = require('rd'); |
|
|
const ws = require('../../lib/ws');
|
|
|
const qn = require('../../lib/qiniu');
|
|
|
|
|
|
const QINIU_PREFIX = 'yoho';
|
|
|
const BROADCAST_PATH = '/upload';
|
|
|
const FILE_SAVE_ROOT_PATH = './uploads';
|
|
|
const FILE_SAVE_ROOT_PATH = './uploads'; // 上传到本地服务器的根目录
|
|
|
const QINIU_PREFIX = 'yoho'; // 上传的七牛的根目录
|
|
|
const SUPER_FILE_CHECK_PASS = 'yohowebtech.superfile'; // 存在此文件,直接上传到七牛
|
|
|
const BROADCAST_PATH = '/upload'; // ws 键
|
|
|
|
|
|
class FileApi {
|
|
|
|
...
|
...
|
@@ -28,51 +29,65 @@ class FileApi { |
|
|
let fileNameWithoutType = _.split(fileName, '.')[0];
|
|
|
let fileSavePath = path.join(FILE_SAVE_ROOT_PATH, fileName); // 文件保存到服务器的路径
|
|
|
|
|
|
let readable = await fs.createReadStream(file.path);
|
|
|
let writable = await fs.createWriteStream(fileSavePath);
|
|
|
let readable = fs.createReadStream(file.path);
|
|
|
let writable = fs.createWriteStream(fileSavePath);
|
|
|
|
|
|
await readable.pipe(writable);
|
|
|
readable.pipe(writable);
|
|
|
|
|
|
await readable.on('end', () => {
|
|
|
readable.on('end', async() => {
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
state: '压缩包已经上传到服务器,开始解压...'
|
|
|
});
|
|
|
});
|
|
|
|
|
|
let fileDistPath = path.join(FILE_SAVE_ROOT_PATH, actPath); // 文件解压的路径
|
|
|
let fileDistPath = path.join(FILE_SAVE_ROOT_PATH, actPath); // 文件解压的路径
|
|
|
|
|
|
shelljs.mkdir('-p', fileDistPath);
|
|
|
|
|
|
shelljs.mkdir('-p', fileDistPath);
|
|
|
// 解压
|
|
|
await decompress(path.resolve(fileSavePath), path.resolve(fileDistPath), {
|
|
|
plugins: [
|
|
|
decompressUnzip()
|
|
|
]
|
|
|
});
|
|
|
|
|
|
// 解压
|
|
|
await decompress(path.resolve(fileSavePath), path.resolve(fileDistPath), {
|
|
|
plugins: [
|
|
|
decompressUnzip()
|
|
|
]
|
|
|
}).then(() => {
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
state: '压缩包已经解压成功,开始将文件上传到七牛...'
|
|
|
state: '压缩包已经解压成功,开始校验文件...'
|
|
|
});
|
|
|
});
|
|
|
|
|
|
let upToQiniuResult = await this.uploadToQiniu(fileDistPath);
|
|
|
let fileList = rd.readSync(fileDistPath); // 读取待上传的文件列表
|
|
|
let testRes = this._testFiles(fileList); // 文件校验
|
|
|
|
|
|
let fileModel = new FileModel();
|
|
|
let insertRes = await fileModel.insert({
|
|
|
date: moment().format('YYYY-MM-DD HH:mm'),
|
|
|
fileName: fileName,
|
|
|
actPath: actPath,
|
|
|
username: '' // TODO
|
|
|
});
|
|
|
if (testRes) {
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
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'),
|
|
|
fileName: fileName,
|
|
|
actPath: actPath,
|
|
|
username: '' // TODO
|
|
|
});
|
|
|
|
|
|
return upToQiniuResult;
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
state: '文件已经全部上传到七牛'
|
|
|
});
|
|
|
} else {
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
state: '校验文件失败,非法上传!'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 上传到七牛
|
|
|
* @param {待上传的文件夹} fileDistPath
|
|
|
* @param {待上传的文件列表} fileList
|
|
|
* @param {待上传的文件夹路径} fileDistPath
|
|
|
*/
|
|
|
async uploadToQiniu(fileDistPath) {
|
|
|
let fileList = rd.readSync(fileDistPath); // 读取待上传的文件列表
|
|
|
async _uploadToQiniu(fileList, fileDistPath) {
|
|
|
let hasError = false;
|
|
|
|
|
|
fileList = _.map(fileList, perFilePath => {
|
...
|
...
|
@@ -93,24 +108,59 @@ class FileApi { |
|
|
state: `${fileList[i]} 已经上传到七牛`
|
|
|
});
|
|
|
} else {
|
|
|
hasError = true;
|
|
|
ws.broadcast(BROADCAST_PATH, {
|
|
|
state: `${fileList[i]} 上传到七牛出错`
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
code: 200,
|
|
|
hasError: hasError
|
|
|
}
|
|
|
/**
|
|
|
* 测试是否是可以上传的文件包
|
|
|
* @param {待上传的文件列表} fileDistPath
|
|
|
*/
|
|
|
_testFiles(fileList) {
|
|
|
let isSuperFile = false;
|
|
|
let isExistedHyperesources = false;
|
|
|
let isExistedHypeGenerated = false;
|
|
|
|
|
|
_.forEach(fileList, file => {
|
|
|
if (file.indexOf(SUPER_FILE_CHECK_PASS) > 0) {
|
|
|
isSuperFile = true;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (/.hyperesources/.test(file)) {
|
|
|
isExistedHyperesources = true;
|
|
|
}
|
|
|
|
|
|
if (/_hype_generated_script/.test(file)) {
|
|
|
isExistedHypeGenerated = true;
|
|
|
}
|
|
|
|
|
|
if (isExistedHyperesources && isExistedHypeGenerated) {
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
return isSuperFile || (isExistedHyperesources && isExistedHypeGenerated);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取上传记录
|
|
|
*/
|
|
|
async getAllLogs() {
|
|
|
let fileModel = new FileModel();
|
|
|
|
|
|
return await fileModel.findAll();
|
|
|
async getAllLogs(params) {
|
|
|
let page = _.parseInt(params.page);
|
|
|
let limit = 10;
|
|
|
let skip = page > 0? limit * (page - 1) : 0;
|
|
|
|
|
|
let result = await new FileModel().cfind({}).sort({
|
|
|
date: -1
|
|
|
}).skip(skip).limit(limit).exec();
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
module.exports = FileApi; |
...
|
...
|
|