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) {
|
} else if (this.request.files.file) {
|
||||||
const file = this.request.files.file;
|
const file = this.request.files.file;
|
||||||
if (file.size > 8 * 1024 * 1024) throw new ValidationError('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');
|
if (!['.jpg', '.jpeg', '.png'].includes(ext)) throw new ValidationError('file');
|
||||||
await storage.put(`user/${this.user._id}/.avatar${ext}`, file.filepath, this.user._id);
|
await storage.put(`user/${this.user._id}/.avatar${ext}`, file.filepath, this.user._id);
|
||||||
// TODO: cached avatar
|
// 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 { extname } from 'path';
|
||||||
import { escapeRegExp } from 'lodash';
|
import { escapeRegExp } from 'lodash';
|
||||||
import { lookup } from 'mime-types';
|
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
|
import mime from '../lib/mime';
|
||||||
import * as bus from '../service/bus';
|
import * as bus from '../service/bus';
|
||||||
import db from '../service/db';
|
import db from '../service/db';
|
||||||
import storage from '../service/storage';
|
import storage from '../service/storage';
|
||||||
@@ -20,9 +20,7 @@ export class StorageModel {
|
|||||||
static async put(path: string, file: string | Buffer | Readable, owner?: number) {
|
static async put(path: string, file: string | Buffer | Readable, owner?: number) {
|
||||||
const meta = {};
|
const meta = {};
|
||||||
await StorageModel.del([path]);
|
await StorageModel.del([path]);
|
||||||
meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out'))
|
meta['Content-Type'] = mime(path);
|
||||||
? 'text/plain'
|
|
||||||
: lookup(path) || 'application/octet-stream';
|
|
||||||
let _id = StorageModel.generateId(extname(path));
|
let _id = StorageModel.generateId(extname(path));
|
||||||
// Make sure id is not used
|
// Make sure id is not used
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
@@ -103,9 +101,7 @@ export class StorageModel {
|
|||||||
);
|
);
|
||||||
const meta = {};
|
const meta = {};
|
||||||
await StorageModel.del([dst]);
|
await StorageModel.del([dst]);
|
||||||
meta['Content-Type'] = (dst.endsWith('.ans') || dst.endsWith('.out'))
|
meta['Content-Type'] = mime(dst);
|
||||||
? 'text/plain'
|
|
||||||
: lookup(dst) || 'application/octet-stream';
|
|
||||||
let _id = StorageModel.generateId(extname(dst));
|
let _id = StorageModel.generateId(extname(dst));
|
||||||
// Make sure id is not used
|
// Make sure id is not used
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export { postJudge } from './handler/judge';
|
|||||||
export { Collections } from './service/db';
|
export { Collections } from './service/db';
|
||||||
export { Service, Context } from './context';
|
export { Service, Context } from './context';
|
||||||
export { buildContent } from './lib/content';
|
export { buildContent } from './lib/content';
|
||||||
|
export { default as mime } from './lib/mime';
|
||||||
export { default as rating } from './lib/rating';
|
export { default as rating } from './lib/rating';
|
||||||
export { default as avatar } from './lib/avatar';
|
export { default as avatar } from './lib/avatar';
|
||||||
export { default as rank } from './lib/rank';
|
export { default as rank } from './lib/rank';
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ export async function previewFile(ev?, type = '') {
|
|||||||
const link = $(ev.currentTarget).find('a').attr('href');
|
const link = $(ev.currentTarget).find('a').attr('href');
|
||||||
if (!link) return null;
|
if (!link) return null;
|
||||||
type ||= ev.currentTarget.getAttribute('data-preview');
|
type ||= ev.currentTarget.getAttribute('data-preview');
|
||||||
const ext = filename.split('.').pop();
|
const ext = filename.split('.').pop().toLowerCase();
|
||||||
if (['zip', 'rar', '7z'].includes(ext) || filesize > 8 * 1024 * 1024) {
|
if (['zip', 'rar', '7z'].includes(ext) || filesize > 8 * 1024 * 1024) {
|
||||||
const action = await new ActionDialog({
|
const action = await new ActionDialog({
|
||||||
$body: tpl.typoMsg(i18n('Cannot preview this file. Download now?')),
|
$body: tpl.typoMsg(i18n('Cannot preview this file. Download now?')),
|
||||||
|
|||||||
Reference in new issue
Block a user