From a7073308713023157926751c404b0322bfd94828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20=C5=81=C4=85giewka?= Date: Wed, 1 Jan 2025 16:55:05 +0100 Subject: [PATCH] Feature/get logo contentType from blob The property `type` contains the MIME type[^1] which can be forwarded to the caller. [^1]:https://developer.mozilla.org/en-US/docs/Web/API/Blob/type Fixes #4172 --- apps/api/src/app/logo/logo.controller.ts | 15 ++++++++------- apps/api/src/app/logo/logo.service.ts | 15 +++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/apps/api/src/app/logo/logo.controller.ts b/apps/api/src/app/logo/logo.controller.ts index 0982a793f..fdbe430c9 100644 --- a/apps/api/src/app/logo/logo.controller.ts +++ b/apps/api/src/app/logo/logo.controller.ts @@ -26,12 +26,13 @@ export class LogoController { @Res() response: Response ) { try { - const buffer = await this.logoService.getLogoByDataSourceAndSymbol({ - dataSource, - symbol - }); + const { buffer, type } = + await this.logoService.getLogoByDataSourceAndSymbol({ + dataSource, + symbol + }); - response.contentType('image/png'); + response.contentType(type); response.send(buffer); } catch { response.status(HttpStatus.NOT_FOUND).send(); @@ -44,9 +45,9 @@ export class LogoController { @Res() response: Response ) { try { - const buffer = await this.logoService.getLogoByUrl(url); + const { buffer, type } = await this.logoService.getLogoByUrl(url); - response.contentType('image/png'); + response.contentType(type); response.send(buffer); } catch { response.status(HttpStatus.NOT_FOUND).send(); diff --git a/apps/api/src/app/logo/logo.service.ts b/apps/api/src/app/logo/logo.service.ts index 584f50cab..4b3446367 100644 --- a/apps/api/src/app/logo/logo.service.ts +++ b/apps/api/src/app/logo/logo.service.ts @@ -38,12 +38,12 @@ export class LogoService { return this.getBuffer(assetProfile.url); } - public async getLogoByUrl(aUrl: string) { + public getLogoByUrl(aUrl: string) { return this.getBuffer(aUrl); } - private getBuffer(aUrl: string) { - return fetch( + private async getBuffer(aUrl: string) { + const blob = await fetch( `https://t0.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${aUrl}&size=64`, { headers: { 'User-Agent': 'request' }, @@ -51,8 +51,11 @@ export class LogoService { this.configurationService.get('REQUEST_TIMEOUT') ) } - ) - .then((res) => res.arrayBuffer()) - .then((buffer) => Buffer.from(buffer)); + ).then((res) => res.blob()); + + return { + buffer: await blob.arrayBuffer().then((ab) => Buffer.from(ab)), + type: blob.type + }; } }