Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit cf78317

Browse files
committed
feat: Refactor avatar upload handler to remove local filesystem dependency and support multiple image formats
1 parent 94d899f commit cf78317

8 files changed

Lines changed: 28 additions & 12 deletions

File tree

pages/api/avatar/[userId].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6363

6464
console.log(`[Avatar API] Not found on CDN, trying local fallback`);
6565

66-
// Fallback: recherche locale
67-
const avatarsDir = path.join(process.cwd(), 'uploads/avatars');
66+
// Fallback: recherche locale - utiliser le dossier uploads global si pas dans un environnement worker
67+
const avatarsDir = path.join(process.cwd(), 'uploads', 'avatars');
6868

6969
for (const ext of extensions) {
7070
const localPath = path.join(avatarsDir, `${userId}${ext}`);

pages/api/banners-icons/[hash].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6363

6464
console.log(`[Banners API] Not found on CDN, trying local fallback`);
6565

66-
// Fallback: recherche locale
67-
const bannersDir = path.join(process.cwd(), 'uploads/bannersIcons');
66+
// Fallback: recherche locale - utiliser le dossier uploads global si pas dans un environnement worker
67+
const bannersDir = path.join(process.cwd(), 'uploads', 'bannersIcons');
6868

6969
for (const ext of extensions) {
7070
const localPath = path.join(bannersDir, `${hash}${ext}`);

pages/api/games-icons/[hash].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6464

6565
console.log(`[Game Icons API] Not found on CDN, trying local fallback`);
6666

67-
// Fallback: recherche locale
68-
const gameIconsDir = path.join(process.cwd(), 'uploads/gameIcons');
67+
// Fallback: recherche locale - utiliser le dossier uploads global si pas dans un environnement worker
68+
const gameIconsDir = path.join(process.cwd(), 'uploads', 'gameIcons');
6969

7070
for (const ext of extensions) {
7171
const localPath = path.join(gameIconsDir, `${hash}${ext}`);

pages/api/items-icons/[hash].ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6464

6565
console.log(`[Item Icons API] Not found on CDN, trying local fallback`);
6666

67-
// Fallback: recherche locale
68-
const itemIconsDir = path.join(process.cwd(), 'uploads/itemsIcons');
67+
// Fallback: recherche locale - utiliser le dossier uploads global si pas dans un environnement worker
68+
const itemIconsDir = path.join(process.cwd(), 'uploads', 'itemsIcons');
6969

7070
for (const ext of extensions) {
7171
const localPath = path.join(itemIconsDir, `${hash}${ext}`);

pages/api/upload/avatar.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
7777
console.log(`Avatar uploaded to R2: ${r2Key}`);
7878
} else {
7979
// Fallback: sauvegarder localement si R2 n'est pas disponible
80-
const destPath = path.join(avatarsDir, `${userId}.avif`);
80+
// Utiliser le dossier uploads global si pas dans un environnement worker
81+
const uploadsDir = path.join(process.cwd(), 'uploads', 'avatars');
82+
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
83+
84+
const destPath = path.join(uploadsDir, `${userId}.avif`);
8185
await fs.promises.writeFile(destPath, avifBuffer);
8286
console.log(`Avatar uploaded locally: ${destPath}`);
8387
}

pages/api/upload/banner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6464
console.log(`Banner uploaded to R2: ${r2Key}`);
6565
} else {
6666
// Fallback: sauvegarder localement si R2 n'est pas disponible
67-
const destPath = path.join(bannersDir, `${hash}.avif`);
67+
// Utiliser le dossier uploads global si pas dans un environnement worker
68+
const uploadsDir = path.join(process.cwd(), 'uploads', 'bannersIcons');
69+
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
70+
71+
const destPath = path.join(uploadsDir, `${hash}.avif`);
6872
await fs.promises.writeFile(destPath, avifBuffer);
6973
console.log(`Banner uploaded locally: ${destPath}`);
7074
}

pages/api/upload/game-icon.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6464
console.log(`Game icon uploaded to R2: ${r2Key}`);
6565
} else {
6666
// Fallback: sauvegarder localement si R2 n'est pas disponible
67-
const destPath = path.join(iconsDir, `${hash}.avif`);
67+
// Utiliser le dossier uploads global si pas dans un environnement worker
68+
const uploadsDir = path.join(process.cwd(), 'uploads', 'gameIcons');
69+
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
70+
71+
const destPath = path.join(uploadsDir, `${hash}.avif`);
6872
await fs.promises.writeFile(destPath, avifBuffer);
6973
console.log(`Game icon uploaded locally: ${destPath}`);
7074
}

pages/api/upload/item-icon.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
6464
console.log(`Item icon uploaded to R2: ${r2Key}`);
6565
} else {
6666
// Fallback: sauvegarder localement si R2 n'est pas disponible
67-
const destPath = path.join(itemsIconsDir, `${hash}.avif`);
67+
// Utiliser le dossier uploads global si pas dans un environnement worker
68+
const uploadsDir = path.join(process.cwd(), 'uploads', 'itemsIcons');
69+
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
70+
71+
const destPath = path.join(uploadsDir, `${hash}.avif`);
6872
await fs.promises.writeFile(destPath, avifBuffer);
6973
console.log(`Item icon uploaded locally: ${destPath}`);
7074
}

0 commit comments

Comments
 (0)