diff --git a/src/controllers/plugins.controller.ts b/src/controllers/plugins.controller.ts index 2f08a04ff5f60b73bb61ba14e904b673a6ec2f2b..262269c2702597d18478fcc1839fe5fc2118526b 100644 --- a/src/controllers/plugins.controller.ts +++ b/src/controllers/plugins.controller.ts @@ -338,6 +338,56 @@ export class PluginsController { res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(); } }); + /** + * @swagger + * + * /backgrounds/{id}: + * get: + * summary: Get the background for a plugin with a given ID and architecture + * description: Returns the background for a plugin with a given ID and architecture. + * tags: [Example, Time] + * parameters: + * - name: arch + * in: query + * description: The architecture the given plugin. + * required: true + * schema: + * type: string + * responses: + * '200': + * description: Successful response with the plugin's background. + * content: + * application/json: + * schema: + * type: array + * items: + * type: string + * '400': + * description: Bad request - When the 'arch' parameter is missing. + * '404': + * description: Not found - When no plugin's background is available for the specified plugin. + * '500': + * description: Internal server error - Something went wrong on the server. + */ + // eslint-disable-next-line @typescript-eslint/no-misused-promises + this.router.get('/backgrounds/:id', async (req: Request, res: Response) => { + try { + if (req.query.arch === undefined) { + res.status(StatusCodes.BAD_REQUEST).send(); + return; + } + const background = await this.pluginsManager.getPluginBackground( + req.params.id, + req.query.arch as string + ); + res + .status(background === '' ? StatusCodes.NOT_FOUND : StatusCodes.OK) + .send(background === '' ? undefined : background); + return; + } catch (e) { + res.status(StatusCodes.INTERNAL_SERVER_ERROR).send(); + } + }); } } diff --git a/src/interfaces/plugins.ts b/src/interfaces/plugins.ts index f5cdaf7bfc10abd01b0d1056251e340e60ce3d2b..eb5ab5ff61115ac7dcfe53bd59af28c025726011 100644 --- a/src/interfaces/plugins.ts +++ b/src/interfaces/plugins.ts @@ -19,7 +19,8 @@ export interface Plugins { name: string; version: string; description: string; - icon: string; + icon: string | undefined; + background: string | undefined; arches: string[]; timestamp: string; author: string; diff --git a/src/services/plugins_manager.service.ts b/src/services/plugins_manager.service.ts index 39308dd991f89c470d51119abbca9c24b1a1895a..c896359ea7d8a45df86bf218578a0a26647e28a5 100644 --- a/src/services/plugins_manager.service.ts +++ b/src/services/plugins_manager.service.ts @@ -61,6 +61,7 @@ export class PluginsManager { version: plugin.version, description: plugin.description, icon: plugin.icon, + background: plugin.background, timestamp: plugin.timestamp, author: plugin.author, }; @@ -73,7 +74,8 @@ export class PluginsManager { name: string; version: string; description: string; - icon: string; + icon: string | undefined; + background: string | undefined; author: string; timestamp: string; } @@ -91,6 +93,7 @@ export class PluginsManager { version: plugin.version, description: plugin.description, icon: plugin.icon, + background: plugin.background, author: plugin.author, timestamp: plugin.timestamp, }; @@ -145,6 +148,7 @@ export class PluginsManager { arches, timestamp, author: issuer.CN, + background: plugin.background, }; } catch (e) { console.error(e); @@ -170,6 +174,7 @@ export class PluginsManager { version: string; description: string; icon: string; + background: string; }> { const manifest = JSON.parse( await this.fileManager.readArchive(path, 'manifest.json') @@ -180,6 +185,7 @@ export class PluginsManager { version: manifest.version, description: manifest.description, icon: manifest.iconPath, + background: manifest.backgroundPath, }; } @@ -196,7 +202,11 @@ export class PluginsManager { plugin.arches.includes(arch as string) && arch !== undefined ); - if (plugin === undefined || process.env.DATA_DIRECTORY === undefined) { + if ( + plugin === undefined || + process.env.DATA_DIRECTORY === undefined || + plugin.icon === undefined + ) { return ''; } @@ -277,4 +287,43 @@ export class PluginsManager { } this.plugins = plugins; } + + async getPluginBackground(id: string, arch: string): Promise<string> { + if (this.plugins.length === 0) { + await this.setPlugins(); + } + const plugin = this.plugins.find( + (plugin: Plugins) => + plugin.name === id && plugin.arches.includes(arch) && arch !== undefined + ); + if ( + plugin === undefined || + process.env.DATA_DIRECTORY === undefined || + plugin.background === undefined + ) { + return ''; + } + + return await this.fileManager + .readArchive( + // eslint-disable-next-line + __dirname + + '/../..' + + process.env.DATA_DIRECTORY + + '/' + + plugin.name + + '/' + + arch + + '/' + + plugin.name + + '.jpl', + 'data/' + plugin.background + ) + .then(data => { + return data; + }) + .catch(e => { + return ''; + }); + } } diff --git a/tests/plugins.controller.test.ts b/tests/plugins.controller.test.ts index 19f403a0a47960087d63811c4d746556790948d1..ac70f94243769cecb66d50d60477806c61aef396 100644 --- a/tests/plugins.controller.test.ts +++ b/tests/plugins.controller.test.ts @@ -51,6 +51,7 @@ describe('Routes', function () { version: '', description: '', icon: '', + background: '', arches: ['test'], timestamp: '', author: '' @@ -87,7 +88,7 @@ describe('Routes', function () { }); it("should fetch plugin details", (done) => { - const expectedPlugin = { name: 'test', version: '', description: '', icon: '', author: '', timestamp: '' }; + const expectedPlugin = { name: 'test', version: '', description: '', icon: '', background: '', author: '', timestamp: '' }; pluginsManagerServiceStub.getPlugin.resolves(expectedPlugin); request(expressApp) @@ -130,7 +131,7 @@ describe('Routes', function () { it("should download a plugin", (done) => { pluginsManagerServiceStub.getPluginPath.resolves('test'); - const expectedPlugin = { name: 'test', version: '', description: '', icon: '', author: '', timestamp: '' }; + const expectedPlugin = { name: 'test', version: '', description: '', icon: '', author: '', timestamp: '', background: '' }; pluginsManagerServiceStub.getPlugin.resolves(expectedPlugin); fs.writeFileSync('test', 'test'); diff --git a/tests/plugins.manager.test.ts b/tests/plugins.manager.test.ts index 7ecac6a29b618252da0fd9b8051ccb5ca36c9fcc..79a59ff6b41818aca555d9bdad2806e75bfda763 100644 --- a/tests/plugins.manager.test.ts +++ b/tests/plugins.manager.test.ts @@ -48,7 +48,8 @@ describe('Plugins manager service tests', function () { icon: '', arches: ['test'], timestamp: '', - author: '' + author: '', + background: '' }]; const expectedPlugin = [ @@ -57,6 +58,7 @@ describe('Plugins manager service tests', function () { version: '', description: '', icon: '', + background: '', timestamp: '', author: '' }]; @@ -74,6 +76,7 @@ describe('Plugins manager service tests', function () { version: '1.0.0', description: 'test', icon: 'test', + background: 'test', timestamp: 'test', author: 'test', } @@ -101,6 +104,7 @@ describe('Plugins manager service tests', function () { version: '1.0.0', description: 'test', icon: 'test', + background: 'test', timestamp: 'test', author: 'test', }; @@ -195,6 +199,7 @@ describe('Plugins manager service tests', function () { version: '1.0.0', description: 'test', icon: 'test', + background: 'test', timestamp: 'test', author: 'test', }; @@ -248,6 +253,7 @@ describe('Plugins manager service tests', function () { version: '1.0.0', description: 'test', icon: 'test', + background: 'test', timestamp: 'test', author: 'test', }; @@ -283,6 +289,7 @@ describe('Plugins manager service tests', function () { version: '1.0.0', description: 'Plugin 1', icon: 'icon1', + background: 'background1', arches: ['x64'], timestamp: 'timestamp1', author: 'author1', @@ -292,6 +299,7 @@ describe('Plugins manager service tests', function () { version: '2.0.0', description: 'Plugin 2', icon: 'icon2', + background: 'background2', arches: ['x86', 'x64'], timestamp: 'timestamp2', author: 'author2', @@ -301,6 +309,7 @@ describe('Plugins manager service tests', function () { version: '3.0.0', description: 'Plugin 3', icon: 'icon3', + background: 'background3', arches: ['x64'], timestamp: 'timestamp3', author: 'author3',