Browse Source

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
pull/4173/head
Szymon Łągiewka 8 months ago
parent
commit
a707330871
Failed to extract signature
  1. 15
      apps/api/src/app/logo/logo.controller.ts
  2. 15
      apps/api/src/app/logo/logo.service.ts

15
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();

15
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
};
}
}

Loading…
Cancel
Save