Skip to content
Snippets Groups Projects
Commit 97cd0086 authored by Xavier Jouslin de Noray's avatar Xavier Jouslin de Noray
Browse files

Background Content type: add jpg content type

Change-Id: I720cba5ee97932524511efd01d1e1220e894af20
parent 816de5eb
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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';
}
}
}
......@@ -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 {
......
......@@ -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")
......
......@@ -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 () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment