core: handle image download cache

This commit is contained in:
undefined committed 2021-08-17 22:21:27 +08:00
1 parent 33b9b0b048
commit d0471b0de8
17 files changed
+132 -93

No files matched your search

+19 -5
View File
@@ -2,9 +2,12 @@ import moment from 'moment-timezone';
import { ObjectID } from 'mongodb';
import AdmZip from 'adm-zip';
import { Time } from '@hydrooj/utils/lib/utils';
import { lookup } from 'mime-types';
import {
ContestNotLiveError, ValidationError, ProblemNotFoundError,
ContestNotAttendedError, PermissionError, BadRequestError, ContestNotFoundError, InvalidTokenError, RecordNotFoundError,
ContestNotAttendedError, PermissionError, BadRequestError,
ContestNotFoundError, InvalidTokenError, RecordNotFoundError,
NotFoundError,
} from '../error';
import { ProblemDoc, Tdoc, User } from '../interface';
import paginate from '../lib/paginate';
@@ -315,10 +318,21 @@ export class ContestProblemFileDownloadHandler extends ContestProblemHandler {
async get(domainId: string, filename: string, noDisposition = false) {
// @ts-ignore
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
this.response.redirect = await storage.signDownloadLink(
`problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`,
noDisposition ? undefined : filename, false, 'user',
);
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename);
this.response.etag = file.etag;
const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
this.response.body = await storage.get(target);
this.response.type = file['Content-Type'] || type;
if (!noDisposition) this.response.addHeader('Content-Disposition', `attachment; filename=${filename}`);
} else {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
}
}
}
+17 -5
View File
@@ -3,9 +3,10 @@ import moment from 'moment-timezone';
import { ObjectID } from 'mongodb';
import AdmZip from 'adm-zip';
import { Time } from '@hydrooj/utils/lib/utils';
import { lookup } from 'mime-types';
import {
ValidationError, HomeworkNotLiveError, ProblemNotFoundError,
HomeworkNotAttendedError, BadRequestError,
HomeworkNotAttendedError, BadRequestError, NotFoundError,
} from '../error';
import {
PenaltyRules, Tdoc, ProblemDoc, User,
@@ -181,10 +182,21 @@ export class HomeworkProblemFileDownloadHandler extends HomeworkDetailProblemHan
async get(domainId: string, filename: string, noDisposition = false) {
// @ts-ignore
if (typeof this.pdoc.docId === 'string') this.pdoc.docId = this.pdoc.docId.split(':')[1];
this.response.redirect = await storage.signDownloadLink(
`problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`,
noDisposition ? undefined : filename, false, 'user',
);
const target = `problem/${this.pdoc.domainId}/${this.pdoc.docId}/additional_file/${filename}`;
const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename);
this.response.etag = file.etag;
const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
this.response.body = await storage.get(target);
this.response.type = file['Content-Type'] || type;
if (!noDisposition) this.response.addHeader('Content-Disposition', `attachment; filename=${filename}`);
} else {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
}
}
}
+23 -6
View File
@@ -1,6 +1,11 @@
/* eslint-disable camelcase */
import { pick } from 'lodash';
import { BadRequestError, ForbiddenError, ValidationError } from '../error';
import { lookup } from 'mime-types';
import {
BadRequestError, ForbiddenError, NotFoundError,
ValidationError,
} from '../error';
import { sortFiles } from '../utils';
import { PRIV } from '../model/builtin';
import storage from '../model/storage';
import * as system from '../model/system';
@@ -8,7 +13,6 @@ import user from '../model/user';
import {
Route, Handler, Types, param, post,
} from '../service/server';
import { sortFiles } from '../utils';
class CheckInHandler extends Handler {
async prepare() {
@@ -78,7 +82,7 @@ export class FilesHandler extends Handler {
throw new ForbiddenError('File size limit exceeded.');
}
if (!filename) filename = this.request.files.file.name || String.random(16);
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', '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');
await storage.put(`user/${this.user._id}/${filename}`, this.request.files.file.path);
const meta = await storage.getMeta(`user/${this.user._id}/${filename}`);
@@ -104,9 +108,22 @@ export class FSDownloadHandler extends Handler {
@param('filename', Types.Name)
@param('noDisposition', Types.Boolean)
async get(domainId: string, uid: number, filename: string, noDisposition = false) {
this.response.redirect = await storage.signDownloadLink(
`user/${uid}/${filename}`, noDisposition ? undefined : filename, false, 'user',
);
this.response.addHeader('Cache-Control', 'public');
const target = `user/${uid}/${filename}`;
const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename);
this.response.etag = file.etag;
const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
this.response.body = await storage.get(target);
this.response.type = file['Content-Type'] || type;
if (!noDisposition) this.response.addHeader('Content-Disposition', `attachment; filename=${filename}`);
} else {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
}
}
}
+18 -6
View File
@@ -2,10 +2,11 @@ import { isSafeInteger, flatten } from 'lodash';
import { FilterQuery, ObjectID } from 'mongodb';
import AdmZip from 'adm-zip';
import { sortFiles } from '@hydrooj/utils/lib/utils';
import { lookup } from 'mime-types';
import {
NoProblemError, PermissionError, ValidationError,
SolutionNotFoundError, ProblemNotFoundError, BadRequestError,
ForbiddenError,
ForbiddenError, NotFoundError,
} from '../error';
import {
ProblemDoc, User, ProblemStatusDoc,
@@ -373,7 +374,7 @@ export class ProblemFilesHandler extends ProblemDetailHandler {
throw new ForbiddenError('File size limit exceeded.');
}
if (!filename) filename = this.request.files.file.name || String.random(16);
if (filename.includes('/') || filename.includes('..')) throw new ValidationError('filename', '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 (filename.endsWith('.zip')) {
const zip = new AdmZip(this.request.files.file.path);
@@ -414,10 +415,21 @@ export class ProblemFileDownloadHandler extends ProblemDetailHandler {
if (type === 'testdata' && !this.user.own(this.pdoc)) {
if (!this.user.hasPriv(PRIV.PRIV_READ_PROBLEM_DATA)) this.checkPerm(PERM.PERM_READ_PROBLEM_DATA);
}
this.response.redirect = await storage.signDownloadLink(
`problem/${this.pdoc.domainId}/${this.pdoc.docId}/${type}/${filename}`,
noDisposition ? undefined : filename, false, 'user',
);
const target = `problem/${domainId}/${this.pdoc.docId}/${type}/${filename}`;
const file = await storage.getMeta(target);
if (!file) throw new NotFoundError(filename);
this.response.etag = file.etag;
const type = lookup(filename).toString();
const shouldProxy = ['image', 'video', 'audio', 'pdf', 'vnd'].filter((i) => type.includes(i)).length;
if (shouldProxy && file.size! < 32 * 1024 * 1024) {
this.response.body = await storage.get(target);
this.response.type = file['Content-Type'] || type;
if (!noDisposition) this.response.addHeader('Content-Disposition', `attachment; filename=${filename}`);
} else {
this.response.redirect = await storage.signDownloadLink(
target, noDisposition ? undefined : filename, false, 'user',
);
}
}
}
+1 -1
View File
@@ -50,7 +50,7 @@ async function _parseDagJson(domainId: string, _dag: string): Promise<Tdoc['dag'
parsed.push(newNode);
}
} catch (e) {
throw new ValidationError('dag', [e.message]);
throw new ValidationError('dag', null, e.message);
}
return parsed;
}