feat: image optimization — client-side resize + server-side thumbnails
Avatars/banners now resize to max 512px/1920px and convert to WebP before upload (zero server cost). Chat image uploads generate an 800px-wide WebP thumbnail via Sharp; the feed shows the thumbnail, click opens the full-res original. Adds lazy loading to avatars. Federation-compatible: remote instances without this feature fall back gracefully.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { config } from '../config.js';
|
||||
import { thumbFilename } from './thumbnail.js';
|
||||
|
||||
/**
|
||||
* Delete a single uploaded file by its stored filename.
|
||||
* Uses path.basename() to prevent directory traversal attacks.
|
||||
* Tolerates ENOENT (file already gone) but logs other errors.
|
||||
* Also attempts to delete the corresponding thumbnail if one exists.
|
||||
*/
|
||||
export function deleteUploadFile(filename: string): void {
|
||||
const safeName = path.basename(filename);
|
||||
@@ -17,6 +19,15 @@ export function deleteUploadFile(filename: string): void {
|
||||
console.error(`Failed to delete upload file ${safeName}:`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Also attempt to delete the thumbnail variant
|
||||
const thumbName = thumbFilename(safeName);
|
||||
const thumbPath = path.join(config.uploadDir, thumbName);
|
||||
try {
|
||||
fs.unlinkSync(thumbPath);
|
||||
} catch {
|
||||
// Thumbnail may not exist — that's fine
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user