mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: better mime detect
This commit is contained in:
5 files changed
+14
-9
No files matched your search
@@ -422,7 +422,7 @@ class HomeAvatarHandler extends Handler {
|
||||
} else if (this.request.files.file) {
|
||||
const file = this.request.files.file;
|
||||
if (file.size > 8 * 1024 * 1024) throw new ValidationError('file');
|
||||
const ext = path.extname(file.originalFilename);
|
||||
const ext = path.extname(file.originalFilename).toLowerCase();
|
||||
if (!['.jpg', '.jpeg', '.png'].includes(ext)) throw new ValidationError('file');
|
||||
await storage.put(`user/${this.user._id}/.avatar${ext}`, file.filepath, this.user._id);
|
||||
// TODO: cached avatar
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { lookup } from 'mime-types';
|
||||
|
||||
export default function mime(file: string) {
|
||||
const identifer = file.toLowerCase();
|
||||
return (['.in', '.out', '.ans'].some((i) => identifer.endsWith(i)))
|
||||
? 'text/plain'
|
||||
: lookup(file) || 'application/octet-stream';
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { extname } from 'path';
|
||||
import { escapeRegExp } from 'lodash';
|
||||
import { lookup } from 'mime-types';
|
||||
import moment from 'moment-timezone';
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { Readable } from 'stream';
|
||||
import mime from '../lib/mime';
|
||||
import * as bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
import storage from '../service/storage';
|
||||
@@ -20,9 +20,7 @@ export class StorageModel {
|
||||
static async put(path: string, file: string | Buffer | Readable, owner?: number) {
|
||||
const meta = {};
|
||||
await StorageModel.del([path]);
|
||||
meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out'))
|
||||
? 'text/plain'
|
||||
: lookup(path) || 'application/octet-stream';
|
||||
meta['Content-Type'] = mime(path);
|
||||
let _id = StorageModel.generateId(extname(path));
|
||||
// Make sure id is not used
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
@@ -103,9 +101,7 @@ export class StorageModel {
|
||||
);
|
||||
const meta = {};
|
||||
await StorageModel.del([dst]);
|
||||
meta['Content-Type'] = (dst.endsWith('.ans') || dst.endsWith('.out'))
|
||||
? 'text/plain'
|
||||
: lookup(dst) || 'application/octet-stream';
|
||||
meta['Content-Type'] = mime(dst);
|
||||
let _id = StorageModel.generateId(extname(dst));
|
||||
// Make sure id is not used
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
|
||||
@@ -37,6 +37,7 @@ export { postJudge } from './handler/judge';
|
||||
export { Collections } from './service/db';
|
||||
export { Service, Context } from './context';
|
||||
export { buildContent } from './lib/content';
|
||||
export { default as mime } from './lib/mime';
|
||||
export { default as rating } from './lib/rating';
|
||||
export { default as avatar } from './lib/avatar';
|
||||
export { default as rank } from './lib/rank';
|
||||
|
||||
Reference in new issue
Block a user