fs_upload支持redirect字段,添加stream2buffer函数

pull/10/head
undefined 4 years ago
parent 86689e8d4c
commit 327fec1fea

@ -4,18 +4,28 @@ import os from 'os';
import fs from 'fs-extra';
import AdmZip from 'adm-zip';
import xml2js from 'xml2js';
import { ObjectID } from 'mongodb';
import { LocalProblemConfig } from 'hydrooj';
import {
Route, Handler, param, Types,
} from 'hydrooj/dist/service/server';
import { streamToBuffer } from 'hydrooj/dist/utils';
import * as file from 'hydrooj/dist/model/file';
import { PERM } from 'hydrooj/dist/model/builtin';
class FpsProblemImportHandler extends Handler {
async get() {
this.response.template = 'problem_import_fps.html';
@param('ufid', Types.ObjectID, true)
async get(domainId: string, ufid?: ObjectID) {
if (ufid) {
const stream = await file.get(ufid);
const buf = await streamToBuffer(stream);
// @ts-ignore
await this.post({ domainId, input: buf.toString() });
await file.del(ufid);
} else this.response.template = 'problem_import_fps.html';
}
@param('input', Types.String)
@param('input', Types.String, true)
async post(domainId: string, input: string) {
const result = await xml2js.parseStringPromise(input);
const problem = global.Hydro.model.problem;

@ -1,6 +1,6 @@
{
"name": "@hydrooj/fps-importer",
"version": "1.0.4",
"version": "1.0.5",
"description": "Import FPS problems",
"main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git",
@ -10,6 +10,7 @@
"adm-zip": "^0.4.16",
"fs-extra": "^9.0.1",
"hydrooj": "^2.12.22",
"mongodb": "^3.6.1",
"xml2js": "^0.4.23"
},
"devDependencies": {

@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.12.24",
"version": "2.12.25",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",

@ -39,7 +39,8 @@ class FileUploadHandler extends Handler {
}
@param('title', Types.String)
async post(domainId: string, title: string) {
@param('redirect', Types.String, true)
async post(domainId: string, title: string, redirect?: string) {
if (!this.request.files.file) throw new BadRequestError();
const quota = await this.getQuota();
const lfdoc = await fs.promises.stat(this.request.files.file.path);
@ -53,6 +54,11 @@ class FileUploadHandler extends Handler {
}
const fdoc = await file.getMeta(ufid);
this.response.template = 'fs_upload.html';
if (redirect) {
this.response.redirect = redirect.includes('?')
? `${redirect}&ufid=${ufid}`
: `${redirect}?ufid=${ufid}`;
}
this.response.body = {
fdoc, ufid, usage: udoc.usage, quota,
};

@ -170,3 +170,12 @@ export function isClass(obj: any, strict = false) {
}
return false;
}
export function streamToBuffer(stream): Promise<Buffer> {
return new Promise((resolve, reject) => {
const buffers = [];
stream.on('error', reject);
stream.on('data', (data) => buffers.push(data));
stream.on('end', () => resolve(Buffer.concat(buffers)));
});
}

Loading…
Cancel
Save