feat(F-052): completed feature

This commit is contained in:
chattie
2026-08-19 10:47:17 +02:00
parent 30c131a809
commit adf1d20c4c
19 changed files with 424 additions and 135 deletions

View File

@@ -1,8 +1,28 @@
import { randomUUID } from 'node:crypto';
import { mkdir, writeFile } from 'node:fs/promises';
import { copyFile, mkdir, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { NextRequest, NextResponse } from 'next/server';
/**
* Peer upload directories (frontend and storefront) that must mirror the
* admin's own `public/uploads/`. After every successful upload, the file
* is copied to each peer so that product pages render images regardless
* of which app the URL is fetched from.
*/
const PEER_UPLOAD_DIRS = [
path.join(process.cwd(), '..', '..', 'frontend', 'public', 'uploads'),
path.join(process.cwd(), '..', '..', 'storefront', 'public', 'uploads'),
];
async function mirrorToPeers(filePath: string): Promise<void> {
await Promise.allSettled(
PEER_UPLOAD_DIRS.map(async (dir) => {
await mkdir(dir, { recursive: true });
await copyFile(filePath, path.join(dir, path.basename(filePath)));
}),
);
}
const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://127.0.0.1:3000';
const MAX_SIZE = 10 * 1024 * 1024;
const EXTENSION_BY_TYPE: Readonly<Record<string, string>> = {
@@ -49,6 +69,7 @@ export async function POST(request: NextRequest) {
await mkdir(uploadDir, { recursive: true });
await writeFile(filePath, buffer, { flag: 'wx' });
await mirrorToPeers(filePath);
return NextResponse.json({
url: `/uploads/${filename}`,