diff --git a/src/controllers/plugins.controller.ts b/src/controllers/plugins.controller.ts index 1f0e5a089532e86e7a6b4b10e28e49fd09537f0f..6fa84cc13b08eb5c2bf7389d287610a90b96037c 100644 --- a/src/controllers/plugins.controller.ts +++ b/src/controllers/plugins.controller.ts @@ -291,7 +291,10 @@ export class PluginsController { req.params.id, req.query.arch as string ); - res.status(StatusCodes.OK).send(icon); + res + .setHeader('Content-Type', icon.type) + .status(StatusCodes.OK) + .send(icon.content); } catch (e) { if (e instanceof NotFoundException) { res.status(StatusCodes.NOT_FOUND).send(); @@ -394,7 +397,10 @@ export class PluginsController { req.params.id, req.query.arch as string ); - res.status(StatusCodes.OK).send(background); + res + .setHeader('Content-Type', background.type) + .status(StatusCodes.OK) + .send(background.content); return; } catch (e) { if (e instanceof NotFoundException) { diff --git a/src/services/file_manager.service.ts b/src/services/file_manager.service.ts index 15634921690c5a9c8dcc5288aba60931393889e3..18f11bd4ffed963c0d1c079ed893c6369fad424e 100644 --- a/src/services/file_manager.service.ts +++ b/src/services/file_manager.service.ts @@ -80,4 +80,20 @@ export class FileManagerService { return undefined; } } + + getImageContentType(buffer: Buffer): string { + const fileSignature = buffer.toString('hex', 0, 4); + switch (fileSignature) { + case '89504e47': + return 'image/png'; + case '47494638': + return 'image/gif'; + case 'ffd8ffe0': + case 'ffd8ffe1': + case 'ffd8ffe2': + return 'image/jpeg'; + default: + return 'application/octet-stream'; + } + } } diff --git a/src/services/plugins_manager.service.ts b/src/services/plugins_manager.service.ts index e4e1afd097d894ab48b888d5d598ea7c2a0b268b..656932743a73c6607b4c32d3e06438c031ca80ee 100644 --- a/src/services/plugins_manager.service.ts +++ b/src/services/plugins_manager.service.ts @@ -232,7 +232,7 @@ export class PluginsManager { async getIcon( id: string, arch: string | undefined = undefined - ): Promise<Buffer> { + ): Promise<{content: Buffer; type: string}> { if (this.plugins.length === 0) { await this.setPlugins(); } @@ -251,8 +251,7 @@ export class PluginsManager { ) { throw new NotFoundException('Plugin not found'); } - - return await this.fileManager.readArchive( + const content = await this.fileManager.readArchive( // eslint-disable-next-line __dirname + '/../..' + @@ -266,6 +265,10 @@ export class PluginsManager { '.jpl', 'data/' + plugin.icon ); + return { + content, + type: this.fileManager.getImageContentType(content), + }; } async getVersions(): Promise<Array<{id: string; version: string}>> { @@ -331,7 +334,10 @@ export class PluginsManager { this.plugins = plugins; } - async getPluginBackground(id: string, arch: string): Promise<Buffer> { + async getPluginBackground( + id: string, + arch: string + ): Promise<{content: Buffer; type: string}> { if (this.plugins.length === 0) { await this.setPlugins(); } @@ -348,8 +354,7 @@ export class PluginsManager { ) { throw new NotFoundException('Plugin not found'); } - - return await this.fileManager.readArchive( + const content = await this.fileManager.readArchive( // eslint-disable-next-line __dirname + '/../..' + @@ -363,6 +368,10 @@ export class PluginsManager { '.jpl', 'data/' + plugin.background ); + return { + content, + type: this.fileManager.getImageContentType(content), + }; } private addPluginArch(platforms: string[], arches: string[]): void { diff --git a/tests/plugins.controller.test.ts b/tests/plugins.controller.test.ts index 1e20ad003a4674a4151de70a6d24d9b6f8266243..4e3c0862a8d514793d2c2aee8d2418a150668190 100644 --- a/tests/plugins.controller.test.ts +++ b/tests/plugins.controller.test.ts @@ -177,7 +177,7 @@ describe('Routes', function () { it("should get icon if the given id is valid", (done) => { const expectedIcon = 'test'; - pluginsManagerServiceStub.getIcon.resolves(Buffer.from(expectedIcon)); + pluginsManagerServiceStub.getIcon.resolves({content: Buffer.from(expectedIcon), type: 'image/png'}); request(expressApp) .get("/icons/test?arch=test") diff --git a/tests/plugins.manager.test.ts b/tests/plugins.manager.test.ts index 8d2f54f353e90dcfeb0c3ecc3910b697166c0d8e..defc8658563e31b2338c0865627cb3e64e3821d1 100644 --- a/tests/plugins.manager.test.ts +++ b/tests/plugins.manager.test.ts @@ -285,7 +285,7 @@ describe('Plugins manager service tests', function () { stub(Object.getPrototypeOf(pluginsManagerService), 'getPlatform').returns(['test']); const actual = await pluginsManagerService['getIcon']("test", "test"); - expect(actual).toEqual(Buffer.from(expected)); + expect(actual.content).toEqual(Buffer.from(expected)); }); it('should return an array of plugin versions', async () => {