@gudhub/core 1.0.44 → 1.0.47

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.
@@ -26,29 +26,11 @@ export class FileManager {
26
26
  }
27
27
  }
28
28
 
29
- async uploadFileFromStringApi(
30
- source,
31
- file_name,
32
- app_id,
33
- item_id,
34
- extension,
35
- format,
36
- element_id
37
- ) {
29
+ async uploadFileFromStringApi(fileObject) {
38
30
  try {
39
- const fileObj = {
40
- app_id,
41
- item_id,
42
- extension,
43
- file_name,
44
- format,
45
- element_id,
46
- source,
47
- };
48
-
49
31
  const file = await this.req.post({
50
32
  url: "/file/upload",
51
- form: { file: JSON.stringify(fileObj) },
33
+ form: { file: JSON.stringify(fileObject) },
52
34
  });
53
35
  return file;
54
36
  } catch (err) {
@@ -78,7 +60,7 @@ export class FileManager {
78
60
  }
79
61
  }
80
62
 
81
- async duplicateFile(files) {
63
+ async duplicateFileApi(files) {
82
64
  return this.req.post({
83
65
  url: "/api/new/file/duplicate",
84
66
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
@@ -88,6 +70,31 @@ export class FileManager {
88
70
  });
89
71
  }
90
72
 
73
+ async downloadFileFromString(app_id, file_id) {
74
+ if(await this.isFileExists(app_id, file_id)) {
75
+ let file = await this.getFile(app_id, file_id);
76
+ let data = await this.req.get({
77
+ url: file.url + '?timestamp=' + file.last_update,
78
+ externalResource: true
79
+ });
80
+ return {
81
+ file,
82
+ data,
83
+ type: 'file'
84
+ }
85
+ } else {
86
+ return false;
87
+ }
88
+ }
89
+
90
+ async duplicateFile(files) {
91
+ let duplicatedFiles = await this.duplicateFileApi(files);
92
+ duplicatedFiles.forEach(file => {
93
+ this.addFileToStorage(file.app_id, file);
94
+ });
95
+ return duplicatedFiles;
96
+ }
97
+
91
98
  async deleteFileApi(id) {
92
99
  try {
93
100
  const file = await this.req.get({
@@ -105,6 +112,7 @@ export class FileManager {
105
112
  if (app) {
106
113
  app.file_list.push(file);
107
114
  this.storage.updateApp(app);
115
+ this.pipeService.emit("gh_file_upload", { app_id }, file);
108
116
  }
109
117
  }
110
118
 
@@ -113,15 +121,26 @@ export class FileManager {
113
121
  if (app) {
114
122
  app.file_list.push(...files);
115
123
  this.storage.updateApp(app);
124
+ files.forEach(file => {
125
+ this.pipeService.emit("gh_file_upload", { app_id }, file);
126
+ });
116
127
  }
117
128
  }
118
129
 
119
130
  deleteFileFromStorage(fileId, app_id) {
120
131
  const app = this.storage.getApp(app_id);
121
132
  if (app) {
122
- app.file_list = app.file_list.filter((file) => file.file_id != fileId);
133
+ let deletedFile;
134
+ app.file_list = app.file_list.filter((file) => {
135
+ if(file.file_id != fileId) {
136
+ return true;
137
+ } else {
138
+ deletedFile = file;
139
+ return false;
140
+ }
141
+ });
123
142
  this.storage.updateApp(app);
124
- this.pipeService.emit("gh_file_delete", { file_id: fileId }, file);
143
+ this.pipeService.emit("gh_file_delete", { file_id: fileId }, deletedFile);
125
144
  }
126
145
  }
127
146
 
@@ -137,29 +156,35 @@ export class FileManager {
137
156
  }
138
157
 
139
158
  async getFile(app_id, file_id) {
140
- try {
141
- const app = await this.appProcessor.getApp(app_id);
142
- return app.file_list.find((file) => {
143
- return file.file_id == file_id;
159
+ const app = await this.appProcessor.getApp(app_id);
160
+ return new Promise((resolve,reject) => {
161
+ let findedFile = app.file_list.find((file) => {
162
+ return file.file_id == file_id
144
163
  });
145
- } catch (err) {
146
- console.log(err);
147
- return null;
148
- }
149
- }
164
+ if(findedFile) {
165
+ resolve(findedFile)
166
+ } else {
167
+ resolve('')
168
+ }
169
+ })
170
+ }
150
171
 
151
- async getFiles(app_id, filesId = []) {
152
- try {
153
- const app = await this.appProcessor.getApp(app_id);
154
- return app.file_list.filter((file) => {
172
+ async getFiles(app_id, filesId = []) {
173
+
174
+ const app = await this.appProcessor.getApp(app_id);
175
+ return new Promise((resolve, reject) => {
176
+ let findedFiles = app.file_list.filter((file) => {
155
177
  return filesId.some(id => {
156
178
  return id == file.file_id;
157
- })});
158
- } catch (err) {
159
- console.log(err);
160
- return null;
161
- }
162
- }
179
+ })
180
+ });
181
+ if(findedFiles) {
182
+ resolve(findedFiles);
183
+ } else {
184
+ resolve('')
185
+ }
186
+ })
187
+ }
163
188
 
164
189
  async uploadFile(fileData, app_id, item_id) {
165
190
  const file = await this.uploadFileApi(fileData, app_id, item_id);
@@ -167,25 +192,9 @@ export class FileManager {
167
192
  return file;
168
193
  }
169
194
 
170
- async uploadFileFromString(
171
- source,
172
- file_name,
173
- app_id,
174
- item_id,
175
- extension,
176
- format,
177
- element_id
178
- ) {
179
- const file = await this.uploadFileFromStringApi(
180
- source,
181
- file_name,
182
- app_id,
183
- item_id,
184
- extension,
185
- format,
186
- element_id
187
- );
188
- this.addFileToStorage(app_id, file);
195
+ async uploadFileFromString(fileObject) {
196
+ const file = await this.uploadFileFromStringApi(fileObject);
197
+ this.addFileToStorage(file.app_id, file);
189
198
  return file;
190
199
  }
191
200
 
@@ -201,8 +210,23 @@ export class FileManager {
201
210
  return file;
202
211
  }
203
212
 
204
- deleteFile(app_id, id) {
205
- this.deleteFileApi(id);
213
+ async deleteFile(id, app_id) {
214
+ await this.deleteFileApi(id);
206
215
  this.deleteFileFromStorage(id, app_id);
216
+ return true;
217
+ }
218
+
219
+ async isFileExists(app_id, file_id) {
220
+ let urlPattern = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
221
+ if(Boolean(file_id)) {
222
+ let file = await this.getFile(app_id, file_id);
223
+ if(Boolean(file)) {
224
+ return urlPattern.test(file.url);
225
+ } else {
226
+ return false;
227
+ }
228
+ } else {
229
+ return false;
230
+ }
207
231
  }
208
232
  }
@@ -8,10 +8,27 @@ describe("FILE MANAGER", function () {
8
8
  let file = await gudhub.getFile(16259,809199);
9
9
  file.should.have.property("url","https://gudhub.com/userdata/16259/809199.jpg");
10
10
  });
11
+
11
12
  it("GET FILES URL's", async function () {
12
13
  //-- checking if file exist
13
14
  let files = await gudhub.getFiles(16259,[809199,811768]);
14
15
  files[0].should.have.property("url","https://gudhub.com/userdata/16259/809199.jpg");
15
16
  files[1].should.have.property("url", "https://gudhub.com/userdata/16259/811768.png");
16
17
  });
18
+
19
+ it("CHECK IF FILE EXISTS", async function() {
20
+ let existingFile = await gudhub.fileManager.isFileExists(16259,809199);
21
+ let notExistingFile = await gudhub.fileManager.isFileExists(16259,300);
22
+
23
+ existingFile.should.equals(true);
24
+ notExistingFile.should.equals(false);
25
+ });
26
+
27
+ it("DOWNLOAD FILE", async function() {
28
+ let file = await gudhub.downloadFileFromString(16259,811769);
29
+
30
+ file.file.url.should.equals('https://gudhub.com/userdata/16259/811769.html');
31
+ file.data.should.not.be.empty();
32
+ });
33
+
17
34
  });
@@ -31,13 +31,17 @@ export class GudHubHttpsService {
31
31
  }
32
32
 
33
33
  const promise = new Promise((resolve, reject) => {
34
- const url = this.root + request.url;
34
+ let url;
35
+ if(request.externalResource) {
36
+ url = request.url;
37
+ } else {
38
+ url = this.root + request.url;
39
+ url = `${url}${
40
+ /\?/.test(url) ? "&" : "?"
41
+ }token=${accessToken}${convertObjToUrlParams(request.params)}`;
42
+ }
35
43
 
36
- axios.get(
37
- `${url}${
38
- /\?/.test(url) ? "&" : "?"
39
- }token=${accessToken}${convertObjToUrlParams(request.params)}`
40
- ).then(function (response) {
44
+ axios.get(url).then(function (response) {
41
45
  // handle success
42
46
  resolve(response.data);
43
47
  })
package/GUDHUB/gudhub.js CHANGED
@@ -359,6 +359,10 @@ export class GudHub {
359
359
  return this.fileManager.duplicateFile(files);
360
360
  }
361
361
 
362
+ downloadFileFromString(app_id, file_id) {
363
+ return this.fileManager.downloadFileFromString(app_id, file_id);
364
+ }
365
+
362
366
  createDocument(documentObject) {
363
367
  return this.documentManager.createDocument(documentObject);
364
368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gudhub/core",
3
- "version": "1.0.44",
3
+ "version": "1.0.47",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {