Enhance image upload and background management: Update Docker configuration to create a dedicated backgrounds directory for uploaded images, modify API routes to handle background images specifically, and improve README documentation to reflect these changes. Additionally, refactor components to utilize the new Avatar component for consistent avatar rendering across the application.
Some checks failed
Deploy with Docker Compose / deploy (push) Failing after 33s
Some checks failed
Deploy with Docker Compose / deploy (push) Failing after 33s
This commit is contained in:
@@ -15,25 +15,18 @@ export async function GET() {
|
||||
|
||||
const images: string[] = [];
|
||||
|
||||
// Lister les images dans public/
|
||||
// Lister uniquement les images dans public/uploads/backgrounds/
|
||||
const publicDir = join(process.cwd(), "public");
|
||||
if (existsSync(publicDir)) {
|
||||
const files = await readdir(publicDir);
|
||||
const uploadsDir = join(publicDir, "uploads");
|
||||
const backgroundsDir = join(uploadsDir, "backgrounds");
|
||||
|
||||
if (existsSync(backgroundsDir)) {
|
||||
const files = await readdir(backgroundsDir);
|
||||
const imageFiles = files.filter(
|
||||
(file) =>
|
||||
file.match(/\.(jpg|jpeg|png|gif|webp|svg)$/i) && !file.startsWith(".")
|
||||
);
|
||||
images.push(...imageFiles.map((file) => `/${file}`));
|
||||
}
|
||||
|
||||
// Lister les images dans public/uploads/
|
||||
const uploadsDir = join(publicDir, "uploads");
|
||||
if (existsSync(uploadsDir)) {
|
||||
const uploadFiles = await readdir(uploadsDir);
|
||||
const imageFiles = uploadFiles.filter((file) =>
|
||||
file.match(/\.(jpg|jpeg|png|gif|webp|svg)$/i)
|
||||
);
|
||||
images.push(...imageFiles.map((file) => `/uploads/${file}`));
|
||||
images.push(...imageFiles.map((file) => `/uploads/backgrounds/${file}`));
|
||||
}
|
||||
|
||||
return NextResponse.json({ images });
|
||||
|
||||
@@ -31,16 +31,17 @@ export async function POST(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
// Créer le dossier uploads s'il n'existe pas
|
||||
// Créer le dossier uploads/backgrounds s'il n'existe pas
|
||||
const uploadsDir = join(process.cwd(), "public", "uploads");
|
||||
if (!existsSync(uploadsDir)) {
|
||||
await mkdir(uploadsDir, { recursive: true });
|
||||
const backgroundsDir = join(uploadsDir, "backgrounds");
|
||||
if (!existsSync(backgroundsDir)) {
|
||||
await mkdir(backgroundsDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Générer un nom de fichier unique
|
||||
const timestamp = Date.now();
|
||||
const filename = `${timestamp}-${file.name}`;
|
||||
const filepath = join(uploadsDir, filename);
|
||||
const filepath = join(backgroundsDir, filename);
|
||||
|
||||
// Convertir le fichier en buffer et l'écrire
|
||||
const bytes = await file.arrayBuffer();
|
||||
@@ -48,7 +49,7 @@ export async function POST(request: Request) {
|
||||
await writeFile(filepath, buffer);
|
||||
|
||||
// Retourner l'URL de l'image
|
||||
const imageUrl = `/uploads/${filename}`;
|
||||
const imageUrl = `/uploads/backgrounds/${filename}`;
|
||||
return NextResponse.json({ url: imageUrl });
|
||||
} catch (error) {
|
||||
console.error("Error uploading image:", error);
|
||||
|
||||
Reference in New Issue
Block a user