mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: files: 添加文件列表缓存
This commit is contained in:
1 parent
9e92891fb2
commit
3c4256be84
12 files changed
+64
-96
No files matched your search
@@ -15,7 +15,6 @@ import { PERM } from '../model/builtin';
|
||||
import {
|
||||
Route, Handler, Types, post,
|
||||
} from '../service/server';
|
||||
import storage from '../service/storage';
|
||||
import { isPid } from '../lib/validator';
|
||||
import download from '../lib/download';
|
||||
|
||||
@@ -208,7 +207,7 @@ class ProblemImportSYZOJHandler extends Handler {
|
||||
const p = new PassThrough();
|
||||
superagent.get(aurls[f.filename]).pipe(p);
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await storage.put(`problem/${domainId}/${docId}/additional_file/${f.filename}`, p);
|
||||
await problem.addAdditionalFile(domainId, docId, f.filename, p);
|
||||
}
|
||||
};
|
||||
if (wait) await syncFiles();
|
||||
|
||||
@@ -1,57 +1,14 @@
|
||||
/* eslint-disable camelcase */
|
||||
import fs from 'fs';
|
||||
import { BadRequestError } from '../error';
|
||||
import { PRIV } from '../model/builtin';
|
||||
import * as system from '../model/system';
|
||||
import * as user from '../model/user';
|
||||
import db from '../service/db';
|
||||
import {
|
||||
Route, Handler, Types, param,
|
||||
} from '../service/server';
|
||||
import storage from '../service/storage';
|
||||
|
||||
const coll = db.collection('status');
|
||||
|
||||
class FileUploadHandler extends Handler {
|
||||
async getQuota() {
|
||||
let quota = system.get('user.quota');
|
||||
if (this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) {
|
||||
quota = 2 ** 63 - 1;
|
||||
}
|
||||
return quota;
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'fs_upload.html';
|
||||
this.response.body = { fdoc: null, usage: this.user.usage, quota: await this.getQuota() };
|
||||
}
|
||||
|
||||
@param('title', Types.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);
|
||||
const ufid = `user/${this.user._id}/${title}`;
|
||||
const udoc = await user.inc(this.user._id, 'usage', lfdoc.size);
|
||||
try {
|
||||
await storage.put(ufid, this.request.files.file.path);
|
||||
} catch (e) {
|
||||
await user.inc(this.user._id, 'usage', -lfdoc.size);
|
||||
throw e;
|
||||
}
|
||||
this.response.template = 'fs_upload.html';
|
||||
if (redirect) {
|
||||
this.response.redirect = redirect.includes('?')
|
||||
? `${redirect}&ufid=${ufid}`
|
||||
: `${redirect}?ufid=${ufid}`;
|
||||
}
|
||||
this.response.body = {
|
||||
ufid, usage: udoc.usage, quota,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class StatusHandler extends Handler {
|
||||
async get() {
|
||||
const stats = await coll.find().sort({ type: 1, updateAt: -1 }).toArray();
|
||||
@@ -125,7 +82,6 @@ class SwitchLanguageHandler extends Handler {
|
||||
}
|
||||
|
||||
export async function apply() {
|
||||
Route('file_upload', '/upload', FileUploadHandler, PRIV.PRIV_CREATE_FILE);
|
||||
Route('status', '/status', StatusHandler);
|
||||
Route('status_update', '/status/update', StatusUpdateHandler);
|
||||
Route('check_in', '/checkin', CheckInHandler, PRIV.PRIV_USER_PROFILE);
|
||||
|
||||
@@ -225,7 +225,7 @@ export class ProblemExportHandler extends ProblemDetailHandler {
|
||||
const pdoc = pick(this.pdoc, ['pid', 'acMsg', 'content', 'config', 'title', 'html', 'tag', 'category']);
|
||||
const zip = new AdmZip();
|
||||
if (hasPerm) {
|
||||
const files = await storage.list(`problem/${this.domainId}/${this.pdoc.docId}/`, true);
|
||||
const files = await storage.list(`problem/${this.domainId}/${this.pdoc.docId}/`);
|
||||
for (const file of files) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const buf = await streamToBuffer(await storage.get(`${file.prefix}${file.name}`));
|
||||
@@ -427,16 +427,8 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
|
||||
@param('additional_file', Types.Boolean)
|
||||
async get(domainId: string, getTestdata = true, getAdditionalFile = true) {
|
||||
const canReadData = this.user._id === this.pdoc.owner || this.user.hasPerm(PERM.PERM_READ_PROBLEM_DATA);
|
||||
const [testdata, additional_file] = await Promise.all([
|
||||
getTestdata && canReadData
|
||||
? storage.list(`problem/${this.pdoc.domainId}/${this.pdoc.docId}/testdata/`, true)
|
||||
: [],
|
||||
getAdditionalFile
|
||||
? storage.list(`problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/`, true)
|
||||
: [],
|
||||
]);
|
||||
this.response.body.testdata = testdata;
|
||||
this.response.body.additional_file = additional_file;
|
||||
this.response.body.testdata = (getTestdata && canReadData) ? this.pdoc.data : [];
|
||||
this.response.body.additional_file = (getAdditionalFile ? this.pdoc.additional_file : []);
|
||||
this.response.template = 'problem_files.html';
|
||||
}
|
||||
|
||||
@@ -474,13 +466,13 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
|
||||
await problem.addTestdata(domainId, this.pdoc.docId, entry.name, entry.getData());
|
||||
} else {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await storage.put(`problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${entry.name}`, entry.getData());
|
||||
await problem.addAdditionalFile(domainId, this.pdoc.docId, entry.name, entry.getData());
|
||||
}
|
||||
}
|
||||
} else if (type === 'testdata') {
|
||||
await problem.addTestdata(domainId, this.pdoc.docId, filename, this.request.files.file.path);
|
||||
} else {
|
||||
await storage.put(`problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`, this.request.files.file.path);
|
||||
await problem.addAdditionalFile(domainId, this.pdoc.docId, filename, this.request.files.file.path);
|
||||
}
|
||||
this.back();
|
||||
}
|
||||
@@ -676,13 +668,15 @@ export class ProblemImportHandler extends Handler {
|
||||
const pid = await problem.add(domainId, pdoc.pid, pdoc.title, pdoc.content, this.user._id, pdoc.tags, pdoc.category);
|
||||
const entries = zip.getEntries();
|
||||
for (const entry of entries) {
|
||||
if (entry.name.startsWith('testdata/') || entry.name.startsWith('additional_file')) {
|
||||
if (entry.name.startsWith('testdata/')) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await storage.put(`problem/${domainId}/${pid}/${entry.name}`, entry.getData());
|
||||
await problem.addTestdata(domainId, pid, entry.name.split('testdata/')[1], entry.getData());
|
||||
} else if (entry.name.startsWith('additional_file/')) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await problem.addAdditionalFile(domainId, pid, entry.name.split('testdata/')[1], entry.getData());
|
||||
}
|
||||
}
|
||||
const data = await storage.list(`problem/${domainId}/${pid}/testdata/`, true);
|
||||
await problem.edit(domainId, pid, { html: pdoc.html, data });
|
||||
await problem.edit(domainId, pid, { html: pdoc.html });
|
||||
this.response.redirect = this.url('problem_detail', { pid });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user