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

This commit is contained in:
Nicolas Meienberger 2023-10-16 22:17:01 +02:00
parent 3d5a041a9d
commit fb9cde68c1
2 changed files with 21 additions and 27 deletions

View file

@ -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 });
}
}

View file

@ -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');
}
}