mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix formidable types
This commit is contained in:
1 parent
67ec4cea0c
commit
1e286b069f
7 files changed
+21
-20
No files matched your search
@@ -104,15 +104,15 @@ class FpsProblemImportHandler extends Handler {
|
|||||||
if (!this.request.files.file) throw new ValidationError('file');
|
if (!this.request.files.file) throw new ValidationError('file');
|
||||||
const tasks = [];
|
const tasks = [];
|
||||||
try {
|
try {
|
||||||
const file = await fs.stat(this.request.files.file.path);
|
const file = await fs.stat(this.request.files.file.filepath);
|
||||||
if (file.size > 64 * 1024 * 1024) throw new FileTooLargeError('64m');
|
if (file.size > 64 * 1024 * 1024) throw new FileTooLargeError('64m');
|
||||||
const content = fs.readFileSync(this.request.files.file.path, 'utf-8');
|
const content = fs.readFileSync(this.request.files.file.filepath, 'utf-8');
|
||||||
const result = await xml2js.parseStringPromise(content);
|
const result = await xml2js.parseStringPromise(content);
|
||||||
tasks.push(result);
|
tasks.push(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof FileTooLargeError) throw e;
|
if (e instanceof FileTooLargeError) throw e;
|
||||||
console.log(e);
|
console.log(e);
|
||||||
const zip = new AdmZip(this.request.files.file.path);
|
const zip = new AdmZip(this.request.files.file.filepath);
|
||||||
for (const entry of zip.getEntries()) {
|
for (const entry of zip.getEntries()) {
|
||||||
try {
|
try {
|
||||||
const buf = entry.getData();
|
const buf = entry.getData();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/fps-importer",
|
"name": "@hydrooj/fps-importer",
|
||||||
"version": "1.3.3",
|
"version": "1.3.4",
|
||||||
"description": "Import FPS problems",
|
"description": "Import FPS problems",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class ProblemImportHydroHandler extends Handler {
|
|||||||
if (keepUser) this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
if (keepUser) this.checkPriv(PRIV.PRIV_EDIT_SYSTEM);
|
||||||
if (!this.request.files.file) throw new ValidationError('file');
|
if (!this.request.files.file) throw new ValidationError('file');
|
||||||
const tmpdir = path.join(os.tmpdir(), 'hydro', `${Math.random()}.import`);
|
const tmpdir = path.join(os.tmpdir(), 'hydro', `${Math.random()}.import`);
|
||||||
const zip = new AdmZip(this.request.files.file.path);
|
const zip = new AdmZip(this.request.files.file.filepath);
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
zip.extractAllToAsync(tmpdir, true, (err) => {
|
zip.extractAllToAsync(tmpdir, true, (err) => {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
|
|||||||
@@ -49,16 +49,17 @@ export class FilesHandler extends Handler {
|
|||||||
if ((this.user._files?.length || 0) >= system.get('limit.user_files')) {
|
if ((this.user._files?.length || 0) >= system.get('limit.user_files')) {
|
||||||
throw new ForbiddenError('File limit exceeded.');
|
throw new ForbiddenError('File limit exceeded.');
|
||||||
}
|
}
|
||||||
if (!this.request.files.file) throw new ValidationError('file');
|
const file = this.request.files?.file;
|
||||||
const f = statSync(this.request.files.file.path);
|
if (!file) throw new ValidationError('file');
|
||||||
|
const f = statSync(file.filepath);
|
||||||
const size = Math.sum((this.user._files || []).map((i) => i.size)) + f.size;
|
const size = Math.sum((this.user._files || []).map((i) => i.size)) + f.size;
|
||||||
if (size >= system.get('limit.user_files_size')) {
|
if (size >= system.get('limit.user_files_size')) {
|
||||||
throw new ForbiddenError('File size limit exceeded.');
|
throw new ForbiddenError('File size limit exceeded.');
|
||||||
}
|
}
|
||||||
if (!filename) filename = this.request.files.file.name || String.random(16);
|
if (!filename) filename = file.originalFilename || String.random(16);
|
||||||
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', null, 'Bad filename');
|
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', null, 'Bad filename');
|
||||||
if (this.user._files.filter((i) => i.name === filename).length) throw new BadRequestError('file exists');
|
if (this.user._files.filter((i) => i.name === filename).length) throw new BadRequestError('file exists');
|
||||||
await storage.put(`user/${this.user._id}/${filename}`, this.request.files.file.path, this.user._id);
|
await storage.put(`user/${this.user._id}/${filename}`, file.filepath, this.user._id);
|
||||||
const meta = await storage.getMeta(`user/${this.user._id}/${filename}`);
|
const meta = await storage.getMeta(`user/${this.user._id}/${filename}`);
|
||||||
const payload = { name: filename, ...pick(meta, ['size', 'lastModified', 'etag']) };
|
const payload = { name: filename, ...pick(meta, ['size', 'lastModified', 'etag']) };
|
||||||
if (!meta) throw new Error('Upload failed');
|
if (!meta) throw new Error('Upload failed');
|
||||||
|
|||||||
@@ -493,13 +493,13 @@ export class ProblemSubmitHandler extends ProblemDetailHandler {
|
|||||||
if (typeof config !== 'object' || config === null) throw new BadRequestError('Incorrect problem config');
|
if (typeof config !== 'object' || config === null) throw new BadRequestError('Incorrect problem config');
|
||||||
const sizeLimit = config.type === 'submit_answer' ? 128 * 1024 * 1024 : 65535;
|
const sizeLimit = config.type === 'submit_answer' ? 128 * 1024 * 1024 : 65535;
|
||||||
if (file.size > sizeLimit) throw new ValidationError('file');
|
if (file.size > sizeLimit) throw new ValidationError('file');
|
||||||
if (file.size > 65535 || await isBinaryFile(file.path, file.size)) {
|
if (file.size > 65535 || await isBinaryFile(file.filepath, file.size)) {
|
||||||
const id = nanoid();
|
const id = nanoid();
|
||||||
await storage.put(`submission/${this.user._id}/${id}`, file.path, this.user._id);
|
await storage.put(`submission/${this.user._id}/${id}`, file.filepath, this.user._id);
|
||||||
code = `@@hydro_submission_file@@${this.user._id}/${id}#${file.name}`;
|
code = `@@hydro_submission_file@@${this.user._id}/${id}#${file.originalFilename}`;
|
||||||
} else {
|
} else {
|
||||||
// TODO auto detect & convert encoding
|
// TODO auto detect & convert encoding
|
||||||
code = await readFile(file.path, 'utf-8');
|
code = await readFile(file.filepath, 'utf-8');
|
||||||
}
|
}
|
||||||
} else if (code.startsWith('@@hydro_submission_file@@')) throw new ValidationError('code');
|
} else if (code.startsWith('@@hydro_submission_file@@')) throw new ValidationError('code');
|
||||||
await this.limitRate('add_record', 60, system.get('limit.submission'));
|
await this.limitRate('add_record', 60, system.get('limit.submission'));
|
||||||
@@ -633,12 +633,12 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
|
|||||||
async postUploadFile(domainId: string, filename: string, type = 'testdata') {
|
async postUploadFile(domainId: string, filename: string, type = 'testdata') {
|
||||||
if (this.pdoc.reference) throw new ForbiddenError('Cannot edit files of a referenced problem.');
|
if (this.pdoc.reference) throw new ForbiddenError('Cannot edit files of a referenced problem.');
|
||||||
if (!this.request.files.file) throw new ValidationError('file');
|
if (!this.request.files.file) throw new ValidationError('file');
|
||||||
if (!filename) filename = this.request.files.file.name || String.random(16);
|
if (!filename) filename = this.request.files.file.originalFilename || String.random(16);
|
||||||
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', null, 'Bad filename');
|
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', null, 'Bad filename');
|
||||||
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
|
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
|
||||||
const files = [];
|
const files = [];
|
||||||
if (filename.endsWith('.zip')) {
|
if (filename.endsWith('.zip')) {
|
||||||
const zip = new AdmZip(this.request.files.file.path);
|
const zip = new AdmZip(this.request.files.file.filepath);
|
||||||
const entries = zip.getEntries();
|
const entries = zip.getEntries();
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
if (!entry.name) continue;
|
if (!entry.name) continue;
|
||||||
@@ -653,8 +653,8 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
|
|||||||
files.push({
|
files.push({
|
||||||
type,
|
type,
|
||||||
name: filename,
|
name: filename,
|
||||||
size: statSync(this.request.files.file.path).size,
|
size: statSync(this.request.files.file.filepath).size,
|
||||||
data: () => this.request.files.file.path,
|
data: () => this.request.files.file.filepath,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!this.user.hasPriv(PRIV.PRIV_EDIT_SYSTEM)) {
|
if (!this.user.hasPriv(PRIV.PRIV_EDIT_SYSTEM)) {
|
||||||
|
|||||||
@@ -119,9 +119,9 @@ class ImportQduojHandler extends Handler {
|
|||||||
|
|
||||||
async post({ domainId }) {
|
async post({ domainId }) {
|
||||||
if (!this.request.files.file) throw new ValidationError('file');
|
if (!this.request.files.file) throw new ValidationError('file');
|
||||||
const stat = await fs.stat(this.request.files.file.path);
|
const stat = await fs.stat(this.request.files.file.filepath);
|
||||||
if (stat.size > 128 * 1024 * 1024) throw new ValidationError('file', 'File too large');
|
if (stat.size > 128 * 1024 * 1024) throw new ValidationError('file', 'File too large');
|
||||||
await this.fromFile(domainId, this.request.files.file.path);
|
await this.fromFile(domainId, this.request.files.file.filepath);
|
||||||
this.response.redirect = this.url('problem_main');
|
this.response.redirect = this.url('problem_main');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hydrooj/import-qduoj",
|
"name": "@hydrooj/import-qduoj",
|
||||||
"version": "1.3.3",
|
"version": "1.3.4",
|
||||||
"description": "Import QDUOJ problem export",
|
"description": "Import QDUOJ problem export",
|
||||||
"main": "package.json",
|
"main": "package.json",
|
||||||
"repository": "https://github.com/hydro-dev/Hydro.git",
|
"repository": "https://github.com/hydro-dev/Hydro.git",
|
||||||
|
|||||||
Reference in new issue
Block a user