-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.js
67 lines (61 loc) · 1.81 KB
/
middlewares.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Software Name : abcdesktop.io
* Version: 0.2
* SPDX-FileCopyrightText: Copyright (c) 2020-2021 Orange
* SPDX-License-Identifier: GPL-2.0-only
*
* This software is distributed under the GNU General Public License v2.0 only
* see the "license.txt" file for more details.
*
* Author: abcdesktop.io team
* Software description: cloud native desktop service
*/
const { body, query } = require('express-validator');
const { getFinalMiddlewares } = require('oc.user.libraries/middlewares');
const middleWareFileQuery = query('file')
.exists({ checkNull: true })
.withMessage('No file provided')
.bail()
.isString()
.withMessage('file must be a string')
.bail()
.notEmpty()
.withMessage('file must not be empty')
.bail();
const middleWareFileBody = body('file')
.exists({ checkNull: true })
.withMessage('No file provided')
.bail()
.isString()
.withMessage('file must be a string')
.bail()
.notEmpty()
.withMessage('file must not be empty')
.bail();
const middleWareDirectoryQuery = query('directory')
.exists({ checkNull: true })
.withMessage('No directory provided')
.bail()
.isString()
.withMessage('directory must be a string')
.bail()
.notEmpty()
.withMessage('directory must not be empty')
.bail();
function middlewareCheckFile(req, res, next) {
if (req.file === undefined
|| typeof req.file !== 'object'
|| typeof req.file.originalname !== 'string'
|| !(req.file.buffer instanceof Buffer)) {
const ret = { errors: [{ location: 'request', msg: 'No file provided' }] };
res.status(422).send(ret);
return;
}
next();
}
module.exports = {
middleWareFileQuery: getFinalMiddlewares(middleWareFileQuery),
middleWareFileBody: getFinalMiddlewares(middleWareFileBody),
middleWareDirectoryQuery: getFinalMiddlewares(middleWareDirectoryQuery),
middlewareCheckFile,
};