Seumi / PneumaProject

a plagiarism detection system specific to source code based on jplag
MIT License
1 stars 0 forks source link

Support for new compressed file formats #3

Open Seumi opened 3 months ago

Seumi commented 3 months ago

I hope I can support uploading rar or 7z files. How should I modify the code?

Seumi commented 3 months ago

@dosu answer this

dosubot[bot] commented 3 months ago

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:

  1. Modify the 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;
  1. Ensure the 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);
}
  1. Update 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.

sweep-ai[bot] commented 3 months ago
Sweeping

25%

Actions

@Seumi, editing this issue description to include more details will automatically make me relaunch. Please join our community forum for support (tracking_id=5998bdac4f)

Report a bug.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.