@modular-rest/server 1.2.1 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-rest/server",
3
- "version": "1.2.1",
3
+ "version": "1.2.4",
4
4
  "description": "a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -80,7 +80,7 @@ async function createRest(options) {
80
80
  /**
81
81
  * Run before hook
82
82
  */
83
- if (options.onBeforeInit) onBeforeInit(app);
83
+ if (options.onBeforeInit) options.onBeforeInit(app);
84
84
 
85
85
  /**
86
86
  * Setup default services
@@ -162,7 +162,7 @@ async function createRest(options) {
162
162
 
163
163
 
164
164
  // on after init
165
- if (options.onAfterInit) onAfterInit(app);
165
+ if (options.onAfterInit) options.onAfterInit(app);
166
166
 
167
167
  //done
168
168
  done({ app, server });
@@ -7,5 +7,8 @@ module.exports = {
7
7
  fileName: String,
8
8
  owner: String,
9
9
  format: String,
10
+ // Tag being used as the parent dir for files
11
+ // uploaddir/$format/$tag/timestamp.format
12
+ tag: String,
10
13
  })
11
14
  }
@@ -9,7 +9,7 @@ let connections = {};
9
9
  let collections = {};
10
10
  let permissionDefinitions = {};
11
11
 
12
- let triggers = require('./../../class/trigger_operator');
12
+ let triggers = require('../../class/trigger_operator');
13
13
  let TypeCasters = require('./typeCasters');
14
14
 
15
15
  /**
@@ -16,7 +16,17 @@ fileRouter.use('/', middleware.auth, async (ctx, next) => {
16
16
 
17
17
  fileRouter.post('/', async (ctx) => {
18
18
 
19
- let body = ctx.body;
19
+ let body = ctx.request.body;
20
+
21
+ // validate result
22
+ let bodyValidate = validateObject(body, 'tag');
23
+
24
+ // fields validation
25
+ if (!bodyValidate.isValid) {
26
+ ctx.status = 412;
27
+ ctx.body = reply('e', { 'e': bodyValidate.requires });
28
+ return;
29
+ }
20
30
 
21
31
  // Access validation
22
32
  let hasAccess = DataService.checkAccess('cms', 'file', AccessTypes.write, body, ctx.state.user);
@@ -37,7 +47,8 @@ fileRouter.post('/', async (ctx) => {
37
47
  else {
38
48
  await service.storeFile({
39
49
  file: file,
40
- ownerId: ctx.state.user.id
50
+ ownerId: ctx.state.user.id,
51
+ tag: body.tag
41
52
  })
42
53
  .then((file) => {
43
54
  result = reply('s', { file });
@@ -74,7 +85,6 @@ fileRouter.delete('/', async (ctx) => {
74
85
  });
75
86
  }
76
87
 
77
- console.log('remove', result);
78
88
  ctx.body = result;
79
89
  });
80
90
 
@@ -22,12 +22,12 @@ class FileService {
22
22
  * @returns storedFile.fullPath
23
23
  * @returns storedFile.fileFormat
24
24
  */
25
- createStoredDetail(fileType) {
25
+ createStoredDetail(fileType, tag) {
26
26
 
27
27
  let time = new Date().getTime();
28
28
  let fileFormat = fileType.split('/')[1];
29
29
  let fileName = `${time}.${fileFormat}`;
30
- let fullPath = pathModule.join(this.directory, fileFormat, fileName);
30
+ let fullPath = pathModule.join(this.directory, fileFormat, tag, fileName);
31
31
 
32
32
  return { fileName, fullPath, fileFormat };
33
33
  }
@@ -39,7 +39,7 @@ class FileService {
39
39
  * @param {file} args.file file object
40
40
  * @param {string} args.ownerId file object
41
41
  */
42
- storeFile({ file, ownerId }) {
42
+ storeFile({ file, ownerId, tag }) {
43
43
 
44
44
  if (!this.directory)
45
45
  throw 'upload directory has not been set.'
@@ -51,7 +51,7 @@ class FileService {
51
51
  * Store file and remove temp file
52
52
  */ {
53
53
 
54
- storedFile = this.createStoredDetail(file.type);
54
+ storedFile = this.createStoredDetail(file.type, tag);
55
55
 
56
56
  fs.copyFile(file.path, storedFile.fullPath, {
57
57
  done: (err) => {
@@ -77,6 +77,7 @@ class FileService {
77
77
  fileName: storedFile.fileName,
78
78
  originalName: file.name,
79
79
  format: storedFile.fileFormat,
80
+ tag,
80
81
  });
81
82
 
82
83
  return doc.save().then(() => doc);
@@ -117,7 +118,7 @@ class FileService {
117
118
  .then(() => {
118
119
 
119
120
  // create file path
120
- let filePath = pathModule.join(this.directory, fileDoc.format, fileDoc.fileName);
121
+ let filePath = pathModule.join(this.directory, fileDoc.format, fileDoc.tag, fileDoc.fileName);
121
122
 
122
123
  // Remove file from disc
123
124
  return this.removeFromDisc(filePath)