Browse Source

Let admins upload unlimited size of data

pull/6/head
Menci 8 years ago
parent
commit
ce9640231b
  1. 4
      models/file.js
  2. 10
      models/problem.js
  3. 8
      modules/problem.js

4
models/file.js

@ -56,12 +56,12 @@ class File extends Model {
return syzoj.utils.resolvePath(syzoj.config.upload_dir, type, md5);
}
static async upload(path, type) {
static async upload(path, type, noLimit) {
let fs = Promise.promisifyAll(require('fs-extra'));
let buf = await fs.readFileAsync(path);
if (buf.length > syzoj.config.limit.data_size) throw new ErrorMessage('数据包太大。');
if (!noLimit && buf.length > syzoj.config.limit.data_size) throw new ErrorMessage('数据包太大。');
try {
let AdmZip = require('adm-zip');

10
models/problem.js

@ -318,13 +318,13 @@ class Problem extends Model {
return syzoj.utils.resolvePath(syzoj.config.upload_dir, 'testdata', this.id.toString());
}
async updateTestdata(path) {
async updateTestdata(path, noLimit) {
let AdmZip = require('adm-zip');
let zip = new AdmZip(path);
let unzipSize = 0;
for (let x of zip.getEntries()) unzipSize += x.header.size;
if (unzipSize > syzoj.config.limit.testdata) throw new ErrorMessage('数据包太大。');
if (!noLimit && unzipSize > syzoj.config.limit.testdata) throw new ErrorMessage('数据包太大。');
let dir = this.getTestdataPath();
let fs = Promise.promisifyAll(require('fs-extra'));
@ -346,7 +346,7 @@ class Problem extends Model {
for (let file of list.files) if (file.filename !== filename) oldSize += file.size;
}
if (oldSize + size > syzoj.config.limit.testdata) throw new ErrorMessage('数据包太大。');
if (!noLimit && oldSize + size > syzoj.config.limit.testdata) throw new ErrorMessage('数据包太大。');
await fs.moveAsync(filepath, path.join(dir, filename), { overwrite: true });
await fs.removeAsync(dir + '.zip');
@ -424,8 +424,8 @@ class Problem extends Model {
}
}
async updateFile(path, type) {
let file = await File.upload(path, type);
async updateFile(path, type, noLimit) {
let file = await File.upload(path, type, noLimit);
if (type === 'additional_file') {
this.additional_file_id = file.id;

8
modules/problem.js

@ -436,7 +436,7 @@ app.post('/problem/:id/import', async (req, res) => {
try {
let data = await download(req.body.url + (req.body.url.endsWith('/') ? 'testdata/download' : '/testdata/download'));
await fs.writeFileAsync(tmpFile.path, data);
await problem.updateTestdata(tmpFile.path);
await problem.updateTestdata(tmpFile.path, await res.locals.user.hasPrivilege('manage_problem'));
} catch (e) {
syzoj.log(e);
}
@ -506,11 +506,11 @@ app.post('/problem/:id/manage', app.multer.fields([{ name: 'testdata', maxCount:
if (validateMsg) throw new ErrorMessage('无效的题目数据配置。', null, validateMsg);
if (req.files['testdata']) {
await problem.updateTestdata(req.files['testdata'][0].path);
await problem.updateTestdata(req.files['testdata'][0].path, await res.locals.user.hasPrivilege('manage_problem'));
}
if (req.files['additional_file']) {
await problem.updateFile(req.files['additional_file'][0].path, 'additional_file');
await problem.updateFile(req.files['additional_file'][0].path, 'additional_file', await res.locals.user.hasPrivilege('manage_problem'));
}
await problem.save();
@ -673,7 +673,7 @@ app.post('/problem/:id/testdata/upload', app.multer.array('file'), async (req, r
if (req.files) {
for (let file of req.files) {
await problem.uploadTestdataSingleFile(file.originalname, file.path, file.size);
await problem.uploadTestdataSingleFile(file.originalname, file.path, file.size, await res.locals.user.hasPrivilege('manage_problem'));
}
}

Loading…
Cancel
Save