Ver Fonte

refactor(api): move app-image endpoint to app directory

Nicolas Meienberger há 1 ano atrás
pai
commit
fb9cde68c1
2 ficheiros alterados com 21 adições e 27 exclusões
  1. 21 0
      src/app/api/app-image/route.ts
  2. 0 27
      src/pages/api/app-image.ts

+ 21 - 0
src/app/api/app-image/route.ts

@@ -0,0 +1,21 @@
+import { getConfig } from '@/server/core/TipiConfig/TipiConfig';
+import fs from 'fs-extra';
+import path from 'path';
+
+export async function GET(request: Request) {
+  try {
+    const { searchParams } = new URL(request.url);
+    const id = searchParams.get('id');
+
+    if (typeof id !== 'string') {
+      return new Response('Not found', { status: 404 });
+    }
+
+    const filePath = path.join(getConfig().rootFolder, 'repos', getConfig().appsRepoId, 'apps', id, 'metadata', 'logo.jpg');
+    const file = fs.readFileSync(filePath);
+
+    return new Response(file, { headers: { 'content-type': 'image/jpeg' } });
+  } catch (error) {
+    return new Response('Error', { status: 500 });
+  }
+}

+ 0 - 27
src/pages/api/app-image.ts

@@ -1,27 +0,0 @@
-import fs from 'fs';
-import { getConfig } from '@/server/core/TipiConfig/TipiConfig';
-import { NextApiRequest, NextApiResponse } from 'next';
-import path from 'path';
-
-/**
- * API endpoint to get the image of an app
- *
- * @param {NextApiRequest} req - The request
- * @param {NextApiResponse} res - The response
- */
-export default function handler(req: NextApiRequest, res: NextApiResponse) {
-  if (typeof req.query.id !== 'string') {
-    res.status(404).send('Not found');
-    return;
-  }
-
-  try {
-    const filePath = path.join(getConfig().rootFolder, 'repos', getConfig().appsRepoId, 'apps', req.query.id, 'metadata', 'logo.jpg');
-    const file = fs.readFileSync(filePath);
-
-    res.setHeader('Content-Type', 'image/jpeg');
-    res.send(file);
-  } catch (e) {
-    res.status(404).send('Not found');
-  }
-}