feat: refine forum settings uploads and transitions
This commit is contained in:
1 parent
e85ced7319
commit
f5d173350b
23 files changed
+985
-54
No files matched your search
+1
-14
@@ -3,24 +3,11 @@
|
||||
import path from 'path';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { sniffImage } from '@/lib/image-type';
|
||||
import { saveObject } from '@/lib/storage';
|
||||
|
||||
const MAX_SIZE = 5 * 1024 * 1024;
|
||||
|
||||
// 从文件头字节嗅探真实图片类型(JPEG/PNG/GIF/WebP),拒绝伪造内容
|
||||
function sniffImage(buf) {
|
||||
if (buf.length < 12) return null;
|
||||
// JPEG: FF D8 FF
|
||||
if (buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return { ext: '.jpg', mime: 'image/jpeg' };
|
||||
// PNG: 89 50 4E 47 0D 0A 1A 0A
|
||||
if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47 && buf[4] === 0x0d && buf[5] === 0x0a && buf[6] === 0x1a && buf[7] === 0x0a) return { ext: '.png', mime: 'image/png' };
|
||||
// GIF: "GIF8"
|
||||
if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x38) return { ext: '.gif', mime: 'image/gif' };
|
||||
// WebP: "RIFF" .... "WEBP"
|
||||
if (buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50) return { ext: '.webp', mime: 'image/webp' };
|
||||
return null;
|
||||
}
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST(req) {
|
||||
|
||||
Reference in new issue
Block a user