-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for new compressed file formats #3
Comments
@dosu answer this |
To add support for uploading
const Router = require('koa-router');
const koaBody = require('koa-body');
const uploadAPI = require('../api/upload');
const auth = require('../middleware/token/checkToken');
const uploadRouter = new Router();
uploadRouter.post('/upload', auth, koaBody({
multipart: true,
formLimit: 1024 * 1024 * 50,
formidable: {
filter: ({ name, originalFilename, mimetype }) => {
// Allow only .rar and .7z files
return mimetype === 'application/x-rar-compressed' || mimetype === 'application/x-7z-compressed';
}
}
}), uploadAPI.upload);
uploadRouter.post('/uuid', auth, uploadAPI.uuid);
uploadRouter.post('/reupload', auth, koaBody({
multipart: true,
formLimit: 1024 * 1024 * 50,
formidable: {
filter: ({ name, originalFilename, mimetype }) => {
// Allow only .rar and .7z files
return mimetype === 'application/x-rar-compressed' || mimetype === 'application/x-7z-compressed';
}
}
}), uploadAPI.reupload);
module.exports = uploadRouter;
const upload = async (ctx, next) => {
let { fields, files } = ctx.request.body;
//TODO
//表单验证
let uuid = uuidv1();
await mkdir(uuid);
let workset = new workSet({
uuid: uuid,
title: fields.title,
lang: fields.lang,
bc: fields.bc === 'false' ? false : true,
threshold: fields.threshold,
mml: fields.mml,
thtype: fields.thtype,
comment: fields.comment,
status: '[MSG]初始化',
createtime: Date.now(),
log: moment().format('YYYY-MM-DD HH:mm') + ' ' + '已创建'
});
try {
await workset.save();
await fileManger.fileMove(files.file.path, path.join(config.workplace.path, uuid, files.file.name))
} catch (e) {
ctx.body = {
code: 'FILEUPLOADERROR',
msg: e.message
}
return
}
ctx.body = {
code: 'FILEUPLOADSUCCESS',
msg: ''
}
doWork(uuid);
}
const reupload = async (ctx, next) => {
let { fields, files } = ctx.request.body;
let res = await workSetModel.findOne({ uuid: fields.uuid }).exec();
if(!res) {
ctx.body = {
code: 'REUPLOADERR',
msg: '作业集不存在'
}
return
}
if(fs.existsSync(path.join(config.workplace.path, fields.uuid))){
await fileManger.fileDelete(fields.uuid);
}
if(fs.existsSync(path.join(config.workplace.path, 'unzip', fields.uuid))){
await fileManger.fileDelete(path.join('unzip', fields.uuid));
}
try{
await mkdir(fields.uuid);
await fileManger.fileMove(files.file.path, path.join(config.workplace.path, fields.uuid, files.file.name))
} catch(e) {
ctx.body = {
code: 'REUPLOADIOERR',
msg: '文件IO出错' + e
}
return
}
res.status = '[MSG]重上传初始化';
res.log.push(moment().format('YYYY-MM-DD HH:mm') + ' ' + '作业集重上传');
await res.save();
ctx.body = {
code: 'REUPLOADSUCCESS',
msg: '重上传成功'
}
doWork(fields.uuid);
}
const shell = require('./shellManger');
const path = require('path');
const unzip = async (source, dest) => {
let cmdStr;
if (source.endsWith('.rar')) {
cmdStr = `unrar x -o+ ${source} ${dest}`;
} else if (source.endsWith('.7z')) {
cmdStr = `7z x ${source} -o${dest}`;
} else {
cmdStr = `unzip -q -a ${source} -d ${dest}`;
}
try {
var unzipres = await shell(cmdStr);
} catch (e) {
return e.message;
}
return unzipres;
};
const zip = async (source) => {
let cmdStr = `cd ${source} && zip -q -r res.zip res`;
try {
var zipres = await shell(cmdStr);
} catch (e) {
return e.message;
}
return zipres;
};
module.exports = {
unzip,
zip
}; This code adds a filter to the |
Actions
❌ Unable to Complete PRIt looks like an issue has occurred around fetching the files. The exception was status_code: 400, body: {'message': 'invalid request: list of documents must not be empty'}. If this error persists contact [email protected].
Tip To recreate the pull request, edit the issue title or description. This is an automated message generated by Sweep AI. |
I hope I can support uploading rar or 7z files. How should I modify the code?
The text was updated successfully, but these errors were encountered: