From c891933d11976a6efd2a6593168d58f1933b519e Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 28 Aug 2020 09:04:05 +0200 Subject: [PATCH] implemented /model/files --- api/model.yaml | 26 ++++++++++++++++++++++++++ src/routes/model.spec.ts | 26 ++++++++++++++++++++++++++ src/routes/model.ts | 9 +++++++++ src/routes/validate/model.ts | 7 +++++++ 4 files changed, 68 insertions(+) diff --git a/api/model.yaml b/api/model.yaml index 0f45f2b..f3999da 100644 --- a/api/model.yaml +++ b/api/model.yaml @@ -73,6 +73,32 @@ 500: $ref: 'api.yaml#/components/responses/500' +/model/files: + get: + summary: list all stored models + description: 'Auth: basic, levels: dev, admin' + tags: + - /model + responses: + 200: + description: model details list + content: + application/json: + schema: + properties: + name: + type: string + example: model_VN_A3WG6_V1 + size: + type: number + example: 4177449 + 401: + $ref: 'api.yaml#/components/responses/401' + 403: + $ref: 'api.yaml#/components/responses/403' + 500: + $ref: 'api.yaml#/components/responses/500' + /model/file/{name}: parameters: - $ref: 'api.yaml#/components/parameters/Name' diff --git a/src/routes/model.spec.ts b/src/routes/model.spec.ts index 50cb88c..5be66b7 100644 --- a/src/routes/model.spec.ts +++ b/src/routes/model.spec.ts @@ -273,6 +273,32 @@ describe('/model', () => { }); }); + describe('GET /model/files', () => { + it('rejects a write user', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/model/files', + auth: {basic: 'janedoe'}, + httpStatus: 403, + }); + }); + it('rejects an API key', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/model/files', + auth: {key: 'admin'}, + httpStatus: 401, + }); + }); + it('rejects an unauthorized request', done => { + TestHelper.request(server, done, { + method: 'get', + url: '/model/files', + httpStatus: 401, + }); + }); + }); + describe('GET /model/file/{name}', (() => { it('returns the binary data', done => { TestHelper.request(server, done, { diff --git a/src/routes/model.ts b/src/routes/model.ts index cfa757e..c771ff9 100644 --- a/src/routes/model.ts +++ b/src/routes/model.ts @@ -95,6 +95,15 @@ router.delete('/model/:group(((?!file)[^\\/]+?))/:name', (req, res, next) => { }); }); +router.get('/model/files', (req, res, next) => { + if (!req.auth(res, ['dev', 'admin'], 'basic')) return; + + ModelFileModel.find().exec((err, data) => { + if (err) return next(err); + res.json(data.map(e => ModelValidate.fileOutput(e))); + }); +}); + router.get('/model/file/:name', (req, res, next) => { if (!req.auth(res, ['dev', 'admin'], 'all')) return; diff --git a/src/routes/validate/model.ts b/src/routes/validate/model.ts index d473872..971cfd3 100644 --- a/src/routes/validate/model.ts +++ b/src/routes/validate/model.ts @@ -32,4 +32,11 @@ export default class ModelValidate { // validate input for model }).validate(data, {stripUnknown: true}); return error !== undefined? null : value; } + + static fileOutput (data) { + return { + name: data.name, + size: data.data.length + } + } } \ No newline at end of file