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
+11
@@ -127,6 +127,17 @@ export const API = {
|
||||
updateSettings(forum_name) {
|
||||
return apiFetch('/settings', { method: 'PUT', body: JSON.stringify({ forum_name }) });
|
||||
},
|
||||
uploadFavicon(file) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
const headers = {};
|
||||
if (token) headers['Authorization'] = 'Bearer ' + token;
|
||||
return fetch('/api/admin/favicon', { method: 'POST', body: fd, headers }).then(async (res) => {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok || data.error) throw new Error(data.error || '上传失败');
|
||||
return data;
|
||||
});
|
||||
},
|
||||
getLinks() { return apiFetch('/links'); },
|
||||
addLink(title, url) {
|
||||
return apiFetch('/links', { method: 'POST', body: JSON.stringify({ title, url }) });
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
export const DIRECT_IMAGE_LIMIT = 5 * 1024 * 1024;
|
||||
export const MAX_IMAGE_SOURCE_SIZE = 20 * 1024 * 1024;
|
||||
|
||||
function canvasToBlob(canvas, quality) {
|
||||
return new Promise((resolve, reject) => {
|
||||
canvas.toBlob(
|
||||
(blob) => blob ? resolve(blob) : reject(new Error('compression_failed')),
|
||||
'image/webp',
|
||||
quality
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async function decodeImage(file) {
|
||||
if (typeof createImageBitmap === 'function') {
|
||||
const bitmap = await createImageBitmap(file);
|
||||
return {
|
||||
source: bitmap,
|
||||
width: bitmap.width,
|
||||
height: bitmap.height,
|
||||
close: () => bitmap.close(),
|
||||
};
|
||||
}
|
||||
|
||||
const url = URL.createObjectURL(file);
|
||||
const image = new Image();
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
image.onload = resolve;
|
||||
image.onerror = reject;
|
||||
image.src = url;
|
||||
});
|
||||
return {
|
||||
source: image,
|
||||
width: image.naturalWidth,
|
||||
height: image.naturalHeight,
|
||||
close: () => URL.revokeObjectURL(url),
|
||||
};
|
||||
} catch (error) {
|
||||
URL.revokeObjectURL(url);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function compressImage(file, { maxDimension = 4096 } = {}) {
|
||||
if (file.size <= DIRECT_IMAGE_LIMIT) return file;
|
||||
if (file.size > MAX_IMAGE_SOURCE_SIZE) throw new Error('source_too_large');
|
||||
|
||||
const decoded = await decodeImage(file);
|
||||
try {
|
||||
const canvas = document.createElement('canvas');
|
||||
const context = canvas.getContext('2d', { alpha: true });
|
||||
if (!context) throw new Error('compression_failed');
|
||||
|
||||
const maxEdge = Math.max(decoded.width, decoded.height);
|
||||
const initialScale = Math.min(1, maxDimension / maxEdge);
|
||||
const qualities = [0.88, 0.76, 0.64, 0.52, 0.4];
|
||||
|
||||
for (const sizeScale of [1, 0.8, 0.64, 0.5]) {
|
||||
canvas.width = Math.max(1, Math.round(decoded.width * initialScale * sizeScale));
|
||||
canvas.height = Math.max(1, Math.round(decoded.height * initialScale * sizeScale));
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.drawImage(decoded.source, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
for (const quality of qualities) {
|
||||
const blob = await canvasToBlob(canvas, quality);
|
||||
if (blob.size <= DIRECT_IMAGE_LIMIT) {
|
||||
const baseName = file.name.replace(/\.[^.]+$/, '') || 'favicon';
|
||||
return new File([blob], `${baseName}.webp`, { type: 'image/webp' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('compression_failed');
|
||||
} finally {
|
||||
decoded.close();
|
||||
}
|
||||
}
|
||||
|
||||
export function compressFavicon(file) {
|
||||
return compressImage(file, { maxDimension: 1024 });
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
export function applyFavicon(url, version = '') {
|
||||
if (typeof document === 'undefined' || !url) return;
|
||||
|
||||
let href = url;
|
||||
try {
|
||||
const resolved = new URL(url, window.location.href);
|
||||
if (version) resolved.searchParams.set('v', version);
|
||||
href = resolved.href;
|
||||
} catch {
|
||||
href = url;
|
||||
}
|
||||
|
||||
let link = document.querySelector('link[rel="icon"]');
|
||||
if (!link) {
|
||||
link = document.createElement('link');
|
||||
link.rel = 'icon';
|
||||
document.head.appendChild(link);
|
||||
}
|
||||
link.href = href;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export function sniffImage(buffer, { allowIcon = false } = {}) {
|
||||
if (buffer.length < 12) return null;
|
||||
|
||||
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
||||
return { ext: '.jpg', mime: 'image/jpeg' };
|
||||
}
|
||||
if (
|
||||
buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47
|
||||
&& buffer[4] === 0x0d && buffer[5] === 0x0a && buffer[6] === 0x1a && buffer[7] === 0x0a
|
||||
) {
|
||||
return { ext: '.png', mime: 'image/png' };
|
||||
}
|
||||
if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38) {
|
||||
return { ext: '.gif', mime: 'image/gif' };
|
||||
}
|
||||
if (
|
||||
buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46
|
||||
&& buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50
|
||||
) {
|
||||
return { ext: '.webp', mime: 'image/webp' };
|
||||
}
|
||||
if (allowIcon && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0x01 && buffer[3] === 0x00) {
|
||||
return { ext: '.ico', mime: 'image/x-icon' };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user