mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-18 01:36:08 +08:00
core&ui: add error & fix error notification (#464)
This commit is contained in:
1 parent
a2ea3812df
commit
534a634028
14 files changed
+123
-76
No files matched your search
@@ -3,7 +3,8 @@ import { statSync } from 'fs';
|
||||
import { pick } from 'lodash';
|
||||
import { lookup } from 'mime-types';
|
||||
import {
|
||||
BadRequestError, ForbiddenError, NotFoundError, ValidationError,
|
||||
AccessDeniedError, FileExistsError, FileLimitExceededError, FileUploadError, NotFoundError,
|
||||
ValidationError,
|
||||
} from '../error';
|
||||
import { PRIV } from '../model/builtin';
|
||||
import * as oplog from '../model/oplog';
|
||||
@@ -57,22 +58,22 @@ export class FilesHandler extends Handler {
|
||||
async postUploadFile(domainId: string, filename: string) {
|
||||
this.checkPriv(PRIV.PRIV_CREATE_FILE);
|
||||
if ((this.user._files?.length || 0) >= system.get('limit.user_files')) {
|
||||
if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new ForbiddenError('File limit exceeded.');
|
||||
if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new FileLimitExceededError('count');
|
||||
}
|
||||
const file = this.request.files?.file;
|
||||
if (!file) throw new ValidationError('file');
|
||||
const f = statSync(file.filepath);
|
||||
const size = Math.sum((this.user._files || []).map((i) => i.size)) + f.size;
|
||||
if (size >= system.get('limit.user_files_size')) {
|
||||
if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new ForbiddenError('File size limit exceeded.');
|
||||
if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new FileLimitExceededError('size');
|
||||
}
|
||||
if (!filename) filename = file.originalFilename || String.random(16);
|
||||
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 FileExistsError(filename);
|
||||
await storage.put(`user/${this.user._id}/${filename}`, file.filepath, this.user._id);
|
||||
const meta = await storage.getMeta(`user/${this.user._id}/${filename}`);
|
||||
const payload = { name: filename, ...pick(meta, ['size', 'lastModified', 'etag']) };
|
||||
if (!meta) throw new Error('Upload failed');
|
||||
if (!meta) throw new FileUploadError();
|
||||
this.user._files.push({ _id: filename, ...payload });
|
||||
await user.setById(this.user._id, { _files: this.user._files });
|
||||
this.back();
|
||||
@@ -97,7 +98,7 @@ export class FSDownloadHandler extends Handler {
|
||||
async get(domainId: string, uid: number, filename: string, noDisposition = false) {
|
||||
const targetUser = await user.getById('system', uid);
|
||||
if (!targetUser) throw new NotFoundError(uid);
|
||||
if (this.user._id !== uid && !targetUser.hasPriv(PRIV.PRIV_CREATE_FILE)) throw new ForbiddenError('Access denied');
|
||||
if (this.user._id !== uid && !targetUser.hasPriv(PRIV.PRIV_CREATE_FILE)) throw new AccessDeniedError();
|
||||
this.response.addHeader('Cache-Control', 'public');
|
||||
const target = `user/${uid}/${filename}`;
|
||||
const file = await storage.getMeta(target);
|
||||
@@ -120,8 +121,8 @@ export class StorageHandler extends Handler {
|
||||
@param('secret', Types.String)
|
||||
async get(domainId: string, target: string, filename = '', expire: number, secret: string) {
|
||||
const expected = md5(`${target}/${expire}/${builtinConfig.file.secret}`);
|
||||
if (expire < Date.now()) throw new ForbiddenError('Link expired');
|
||||
if (secret !== expected) throw new ForbiddenError('Invalid secret');
|
||||
if (expire < Date.now()) throw new AccessDeniedError();
|
||||
if (secret !== expected) throw new AccessDeniedError();
|
||||
this.response.body = await storage.get(target);
|
||||
this.response.type = (target.endsWith('.out') || target.endsWith('.ans'))
|
||||
? 'text/plain'
|
||||
|
||||
Reference in new issue
Block a user