Open Seumi opened 3 months ago
@dosu answer this
To add support for uploading .rar
or .7z
files, you need to modify the uploadAPI.upload
function to handle these file types. Here's how you can do it:
koaBody
middleware to accept .rar
and .7z
files: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;
uploadAPI.upload
and uploadAPI.reupload
functions can process .rar
and .7z
files. The existing file handling logic should be sufficient: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);
}
zipManger.js
to handle .rar
and .7z
files: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 koaBody
middleware to only accept files with MIME types application/x-rar-compressed
and application/x-7z-compressed
, which correspond to .rar
and .7z
files, respectively. The file handling logic in upload
and reupload
functions will then process these files as needed. The zipManger.js
is also updated to handle the extraction of .rar
and .7z
files using appropriate commands [1][2][3].
To continue talking to Dosu, mention @dosu.
It 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 team@sweep.dev.
@Seumi, editing this issue description to include more details will automatically make me relaunch. Please join our community forum for support (tracking_id=5998bdac4f)
[!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?