Files
my-links/app/favicons/controllers/favicons_controller.ts
2024-11-15 14:22:21 +01:00

35 lines
1.0 KiB
TypeScript

import { CacheService } from '#favicons/services/cache_service';
import { FaviconService } from '#favicons/services/favicons_service';
import { Favicon } from '#favicons/types/favicon_type';
import { inject } from '@adonisjs/core';
import type { HttpContext } from '@adonisjs/core/http';
@inject()
export default class FaviconsController {
private faviconService: FaviconService;
private cacheService: CacheService;
constructor(faviconService: FaviconService, cacheService: CacheService) {
this.faviconService = faviconService;
this.cacheService = cacheService;
}
async index(ctx: HttpContext) {
const url = ctx.request.qs()?.url;
if (!url) {
throw new Error('Missing URL');
}
const favicon = await this.cacheService.getOrSetFavicon(url, () =>
this.faviconService.getFavicon(url)
);
return this.sendImage(ctx, favicon);
}
private sendImage(ctx: HttpContext, { buffer, type, size }: Favicon) {
ctx.response.header('Content-Type', type);
ctx.response.header('Content-Length', size.toString());
ctx.response.send(buffer, true);
}
}