Skip to content
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

Open
Seumi opened this issue Jun 18, 2024 · 3 comments
Open

Support for new compressed file formats #3

Seumi opened this issue Jun 18, 2024 · 3 comments
Labels
enhancement New feature or request sweep

Comments

@Seumi
Copy link
Owner

Seumi commented Jun 18, 2024

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

@dosubot dosubot bot added the enhancement New feature or request label Jun 18, 2024
@Seumi
Copy link
Owner Author

Seumi commented Jun 18, 2024

@dosu answer this

Copy link

dosubot bot commented Jun 18, 2024

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.

@Seumi Seumi added the sweep label Jun 19, 2024
Copy link

sweep-ai bot commented Jun 19, 2024

Sweeping

25%

Actions

  • ↻ Restart Sweep

❌ Unable to Complete PR

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 [email protected].

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sweep
Projects
None yet
Development

No branches or pull requests

1 participant