fix: Profilbild-Upload auf JSON/Base64 umgestellt statt multipart

POST /profile/avatar schlug live mit net::ERR_FAILED fehl (Verbindung
abgebrochen, keine Antwort) - selbst bei einer winzigen Testdatei.
CORS-Preflight lief sauber durch, nur der eigentliche multipart-POST
nicht; passt zum bereits dokumentierten Cloudflare-Verhalten bei
multipart-Uploads (siehe .gitea/workflows/*-image.yml). Umgangen durch
Umstellung auf denselben JSON-Content-Type, der fuer alle anderen
Endpunkte nachweislich zuverlaessig funktioniert: Bild wird
clientseitig als data:-URL kodiert und im JSON-Body gesendet.
Globales JSON-Body-Limit auf 5mb angehoben (deckt 2MB-Bild plus
Base64-Overhead).
This commit is contained in:
Nick Szaunig committed 2026-09-02 08:32:08 +02:00
1 parent abe1f6e8cf
commit 135738fdfc
7 files changed
+81 -49

No files matched your search

+14 -4
View File
@@ -16,6 +16,7 @@ import { PermissionResolverService } from '../auth/permissions/permission-resolv
import { UsersService } from '../users/users.service';
import {
ALLOWED_AVATAR_MIME_TYPES,
MAX_AVATAR_SIZE_BYTES,
mimeTypeForExtension,
} from './avatar-mime-types';
import { UpdateProfileDto } from './dto/update-profile.dto';
@@ -141,19 +142,28 @@ export class ProfileService {
await this.usersService.deactivate(userId, userId);
}
async uploadAvatar(userId: string, file: Express.Multer.File): Promise<void> {
if (!(file.mimetype in ALLOWED_AVATAR_MIME_TYPES)) {
async uploadAvatar(userId: string, dataUrl: string): Promise<void> {
const match = /^data:(image\/(?:jpeg|png|webp));base64,(.+)$/.exec(dataUrl);
if (!match) {
throw new BadRequestException('Ungültiges Bildformat');
}
const [, mimeType, base64] = match;
if (!(mimeType in ALLOWED_AVATAR_MIME_TYPES)) {
throw new BadRequestException('Nicht unterstützter Dateityp');
}
const buffer = Buffer.from(base64, 'base64');
if (buffer.length > MAX_AVATAR_SIZE_BYTES) {
throw new BadRequestException('Datei zu groß (max. 2 MB)');
}
const user = await this.requireUser(userId);
const extension = ALLOWED_AVATAR_MIME_TYPES[file.mimetype];
const extension = ALLOWED_AVATAR_MIME_TYPES[mimeType];
const directory = avatarStorageRoot();
await fs.mkdir(directory, { recursive: true });
const avatarPath = path.join(
directory,
`${crypto.randomUUID()}${extension}`,
);
await fs.writeFile(avatarPath, file.buffer);
await fs.writeFile(avatarPath, buffer);
if (user.avatarPath) {
await fs.unlink(user.avatarPath).catch(() => undefined);
}