Commit 9489de6e authored by xuchentao's avatar xuchentao

feat: increase image upload limit

parent 77b3d936
Pipeline #417 passed with stages
in 17 seconds
......@@ -4,8 +4,8 @@ server {
listen 80;
server_name 101.126.10.129;
# 图片上限为 8MB,JSON 中的 Base64 数据会额外增大请求体。
client_max_body_size 12m;
# 图片上限为 20MB,JSON 中的 Base64 数据会额外增大请求体。
client_max_body_size 28m;
location / {
proxy_pass http://127.0.0.1:8789;
......
const $ = (selector) => document.querySelector(selector);
const API_BASE = "/api/cms";
const MAX_UPLOAD_SIZE = 20 * 1024 * 1024;
let categories = [];
let editingSlug = null;
let editingStatus = "new-unsaved";
......@@ -564,6 +565,10 @@ $("#file").addEventListener("change", async (event) => {
const file = event.target.files?.[0];
event.target.value = "";
if (!file) return;
if (file.size > MAX_UPLOAD_SIZE) {
$("#upload-status").textContent = "图片不能超过 20MB";
return;
}
$("#upload-status").textContent = "上传中…";
try {
const dataUrl = await new Promise((resolve, reject) => {
......
......@@ -470,7 +470,7 @@ export async function saveUpload(dataUrl: unknown): Promise<string> {
const match = String(dataUrl || "").match(/^data:([^;]+);base64,(.+)$/s);
if (!match || !extensions[match[1]]) throw new StoreError("仅支持 PNG、JPG、WEBP 或 GIF 图片");
const buffer = Buffer.from(match[2], "base64");
if (buffer.length > 8 * 1024 * 1024) throw new StoreError("图片不能超过 8MB", 413);
if (buffer.length > 20 * 1024 * 1024) throw new StoreError("图片不能超过 20MB", 413);
const name = `${Date.now()}-${crypto.randomBytes(4).toString("hex")}.${extensions[match[1]]}`;
await writeAtomic(path.join(UPLOADS_DIR, name), buffer);
return `/uploads/${name}`;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment