@cocreate/file 1.6.3 → 1.7.1

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/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## [1.7.1](https://github.com/CoCreate-app/CoCreate-file/compare/v1.7.0...v1.7.1) (2023-09-17)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * renamed filter to $filter ([c8e8390](https://github.com/CoCreate-app/CoCreate-file/commit/c8e83901429d2c370b327449a9f7f0081b03e6fd))
7
+ * update broadcast to broadcastSender false ([b93ca93](https://github.com/CoCreate-app/CoCreate-file/commit/b93ca93f6b1bff1b1052c841f63919f4156e551e))
8
+ * update data.filter to data.$filter, supports data.object.$filter to ad filters per object ([7ccb455](https://github.com/CoCreate-app/CoCreate-file/commit/7ccb45521a8af3c67587c756c905d7ed4a557a60))
9
+
10
+ # [1.7.0](https://github.com/CoCreate-app/CoCreate-file/compare/v1.6.3...v1.7.0) (2023-09-07)
11
+
12
+
13
+ ### Features
14
+
15
+ * support match param. An array of files to match with config. if match found upload or save source ([30e6d57](https://github.com/CoCreate-app/CoCreate-file/commit/30e6d57da184f9533a0b75e8acc3a09e68b48629))
16
+
1
17
  ## [1.6.3](https://github.com/CoCreate-app/CoCreate-file/compare/v1.6.2...v1.6.3) (2023-08-21)
2
18
 
3
19
 
@@ -22,4 +22,4 @@ module.exports = {
22
22
  }
23
23
  }
24
24
  ]
25
- }
25
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file",
3
- "version": "1.6.3",
3
+ "version": "1.7.1",
4
4
  "description": "A headless file uploader that uses HTML5 attributes for customization. Allows easy upload of files to server.",
5
5
  "keywords": [
6
6
  "file",
package/src/client.js CHANGED
@@ -15,10 +15,12 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  ********************************************************************************/
17
17
 
18
- // Commercial Licensing Information:
19
- // For commercial use of this software without the copyleft provisions of the AGPLv3,
20
- // you must obtain a commercial license from CoCreate LLC.
21
- // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
18
+ /**
19
+ * Commercial Licensing Information:
20
+ * For commercial use of this software without the copyleft provisions of the AGPLv3,
21
+ * you must obtain a commercial license from CoCreate LLC.
22
+ * For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
23
+ */
22
24
 
23
25
  import Observer from '@cocreate/observer';
24
26
  import Crud from '@cocreate/crud-client';
@@ -389,7 +391,7 @@ async function upload(element, data) {
389
391
  let Data = Elements.getObject(input);
390
392
  if (Data.type) {
391
393
  if (input.getFilter)
392
- Data.filter = input.getFilter()
394
+ Data.$filter = input.getFilter()
393
395
 
394
396
  let files = await getFiles(input)
395
397
 
@@ -406,18 +408,16 @@ async function upload(element, data) {
406
408
  }
407
409
  }
408
410
 
409
- let action = 'update' + Data.type.charAt(0).toUpperCase() + Data.type.slice(1)
410
- if (Crud[action]) {
411
- let response = await Crud[action](Data)({
412
- array,
413
- object,
414
- upsert: true
415
- });
416
-
417
- data.push(response)
418
- if (response && (!object || object !== response.object)) {
419
- Elements.setTypeValue(element, response);
420
- }
411
+ Data.method = 'update.' + Data.type
412
+ let response = await Crud.send(Data)({
413
+ array,
414
+ object,
415
+ upsert: true
416
+ });
417
+
418
+ data.push(response)
419
+ if (response && (!object || object !== response.object)) {
420
+ Elements.setTypeValue(element, response);
421
421
  }
422
422
  }
423
423
  }
@@ -471,7 +471,7 @@ async function Import(element, data) {
471
471
  let Data = Elements.getObject(element[i]);
472
472
  if (Data.type) {
473
473
  if (element[i].getFilter)
474
- Data.filter = element[i].getFilter()
474
+ Data.$filter = element[i].getFilter()
475
475
 
476
476
  if (Data.type === 'key')
477
477
  Data.type = 'object'
@@ -482,10 +482,8 @@ async function Import(element, data) {
482
482
 
483
483
  if (data.length) {
484
484
  for (let i = 0; i < data.length; i++) {
485
- let action = 'create' + data[i].type.charAt(0).toUpperCase() + data[i].type.slice(1)
486
- if (Crud[action]) {
487
- data[i] = await Crud[action](data[i])
488
- }
485
+ data[i].method = 'create.' + data[i].type
486
+ data[i] = await Crud.send(data[i])
489
487
  }
490
488
  }
491
489
 
@@ -523,15 +521,14 @@ async function Export(element, data) {
523
521
  let Data = Elements.getObject(element[i]);
524
522
  if (Data.type) {
525
523
  if (element[i].getFilter)
526
- Data.filter = element[i].getFilter()
524
+ Data.$filter = element[i].getFilter()
527
525
 
528
526
  if (Data.type === 'key')
529
527
  Data.type = 'object'
530
- let action = 'read' + Data.type.charAt(0).toUpperCase() + Data.type.slice(1)
531
- if (Crud[action]) {
532
- Data = await Crud[action](Data)
533
- data.push(...Data[Data.type])
534
- }
528
+ Data.method = 'read.' + Data.type
529
+ Data = await Crud.send(Data)
530
+ data.push(...Data[Data.type])
531
+
535
532
  }
536
533
 
537
534
  let queriedElements = queryElements({ element: element[i], prefix: 'export' })
package/src/index.js CHANGED
@@ -15,10 +15,12 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  ********************************************************************************/
17
17
 
18
- // Commercial Licensing Information:
19
- // For commercial use of this software without the copyleft provisions of the AGPLv3,
20
- // you must obtain a commercial license from CoCreate LLC.
21
- // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
18
+ /**
19
+ * Commercial Licensing Information:
20
+ * For commercial use of this software without the copyleft provisions of the AGPLv3,
21
+ * you must obtain a commercial license from CoCreate LLC.
22
+ * For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
23
+ */
22
24
 
23
25
  (function (root, factory) {
24
26
  if (typeof define === 'function' && define.amd) {
package/src/server.js CHANGED
@@ -77,9 +77,15 @@ const mimeTypes = {
77
77
  ".7z": "application/x-7z-compressed"
78
78
  }
79
79
 
80
- module.exports = async function file(CoCreateConfig, configPath) {
80
+ module.exports = async function file(CoCreateConfig, configPath, match) {
81
81
  let directories = CoCreateConfig.directories
82
82
  let sources = CoCreateConfig.sources
83
+ let configDirectoryPath = path.dirname(configPath)
84
+
85
+ if (match && !Array.isArray(match))
86
+ match = [match]
87
+ else if (!match)
88
+ match = []
83
89
 
84
90
  let config = await Config({
85
91
  organization_id: {
@@ -115,13 +121,13 @@ module.exports = async function file(CoCreateConfig, configPath) {
115
121
 
116
122
 
117
123
  crud.socket.create(config)
118
- config.broadcast = false
124
+ config.broadcastSender = false
119
125
 
120
126
  if (config.email && config.password) {
121
127
  let request = {
122
128
  method: 'signIn',
123
129
  array: 'users',
124
- filter: {
130
+ $filter: {
125
131
  query: [
126
132
  { key: 'email', value: config.email, operator: '$eq' },
127
133
  { key: 'password', value: config.password, operator: '$eq' }
@@ -143,7 +149,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
143
149
 
144
150
  }
145
151
 
146
- console.log('Uploading files...')
152
+ // console.log('Uploading files...')
147
153
 
148
154
  /**
149
155
  * Store files by config directories
@@ -153,14 +159,15 @@ module.exports = async function file(CoCreateConfig, configPath) {
153
159
  async function runDirectories() {
154
160
  for (const directory of directories) {
155
161
  const entry = directory.entry
156
- const exclude = directory.exclude
162
+ const exclude = directory.exclude || []
157
163
  await runFiles(directory, entry, exclude)
158
164
  }
159
165
  return
160
166
  }
161
167
 
162
168
  async function runFiles(directory, entry, exclude, parentDirectory = '') {
163
- let files = fs.readdirSync(entry);
169
+ const entryPath = path.resolve(configDirectoryPath, entry)
170
+ let files = fs.readdirSync(entryPath);
164
171
 
165
172
  for (let file of files) {
166
173
  let skip = false
@@ -172,8 +179,19 @@ module.exports = async function file(CoCreateConfig, configPath) {
172
179
  }
173
180
  if (skip) continue
174
181
 
182
+ for (let i = 0; i < match.length; i++) {
183
+ skip = true
184
+ const filePath = path.resolve(entryPath, file);
185
+ if (filePath.startsWith(match[i])) {
186
+ skip = false
187
+ console.log('Uploaded: ', filePath)
188
+ break;
189
+ }
190
+ }
175
191
 
176
- let isDirectory = fs.existsSync(`${entry}/${file}`) && fs.lstatSync(`${entry}/${file}`).isDirectory();
192
+ if (skip) continue
193
+
194
+ let isDirectory = fs.existsSync(`${entryPath}/${file}`) && fs.lstatSync(`${entryPath}/${file}`).isDirectory();
177
195
  let name = file
178
196
  let source = ''
179
197
  let directoryName = parentDirectory || '';
@@ -203,7 +221,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
203
221
  if (isDirectory)
204
222
  mimeType = "text/directory"
205
223
  else
206
- source = getSource(`${entry}/${file}`, mimeType)
224
+ source = getSource(`${entryPath}/${file}`, mimeType)
207
225
 
208
226
  let values = {
209
227
  '{{name}}': name,
@@ -257,8 +275,8 @@ module.exports = async function file(CoCreateConfig, configPath) {
257
275
  }
258
276
 
259
277
  if (!newObject.object._id)
260
- newObject.filter = {
261
- query: [{ key: 'path', value: pathName, operator: '$eq' }]
278
+ newObject.$filter = {
279
+ query: [{ key: 'path', value: pathName, operator: '$or' }]
262
280
  }
263
281
 
264
282
  response = await runStore(newObject);
@@ -304,6 +322,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
304
322
  let source = { ...sources[i] };
305
323
  let keys = new Map()
306
324
  let response = {};
325
+ let isMatch = false
307
326
 
308
327
  try {
309
328
  if (array) {
@@ -316,7 +335,8 @@ module.exports = async function file(CoCreateConfig, configPath) {
316
335
 
317
336
  let variables = object[key].match(/{{([A-Za-z0-9_.,\[\]\-\/ ]*)}}/g);
318
337
  if (variables) {
319
- keys.set(key, `${object[key]}`)
338
+ let originalValue = object[key]
339
+ keys.set(key, originalValue)
320
340
  let value = ""
321
341
  for (let variable of variables) {
322
342
  let entry = /{{\s*([\w\W]+)\s*}}/g.exec(variable);
@@ -325,6 +345,17 @@ module.exports = async function file(CoCreateConfig, configPath) {
325
345
  if (!fs.existsSync(entry))
326
346
  continue
327
347
 
348
+ if (!isMatch) {
349
+ const filePath = path.resolve(configDirectoryPath, entry);
350
+ for (let i = 0; i < match.length; i++) {
351
+ if (filePath.startsWith(match[i])) {
352
+ console.log('Source saved', sources[i])
353
+ isMatch = true
354
+ break;
355
+ }
356
+ }
357
+ }
358
+
328
359
  let read_type = 'utf8'
329
360
  const fileExtension = path.extname(entry);
330
361
  let mime_type = mimeTypes[fileExtension] || 'text/html'
@@ -347,24 +378,24 @@ module.exports = async function file(CoCreateConfig, configPath) {
347
378
 
348
379
  let data = { array, object }
349
380
  if (!object._id && object.path)
350
- data.filter = {
351
- query: [{ key: 'path', value: object.path, operator: '$eq' }]
381
+ data.$filter = {
382
+ query: [{ key: 'path', value: object.path, operator: '$or' }]
352
383
  }
353
384
 
354
- response = await runStore(data);
385
+ if (match.length && isMatch)
386
+ response = await runStore(data);
355
387
  }
356
388
  } catch (err) {
357
389
  console.log(err)
358
390
  process.exit()
359
391
  }
392
+
360
393
  if (response.object && response.object[0] && response.object[0]._id) {
361
- for (const [key, value] of keys) {
362
- source.object[key] = value
363
- }
364
394
  source.object._id = response.object[0]._id
365
- } else {
366
- console.log('_id could not be found')
367
- process.exit()
395
+ }
396
+
397
+ for (const [key, value] of keys) {
398
+ source.object[key] = value
368
399
  }
369
400
 
370
401
  updatedSources.push(source)
@@ -377,7 +408,7 @@ module.exports = async function file(CoCreateConfig, configPath) {
377
408
  async function runStore(data) {
378
409
  try {
379
410
  let response;
380
- if (!data.object._id && !data.filter) {
411
+ if (!data.object._id && !data.$filter) {
381
412
  response = await crud.send({
382
413
  method: 'create.object',
383
414
  ...config,
@@ -404,10 +435,10 @@ module.exports = async function file(CoCreateConfig, configPath) {
404
435
  if (directories)
405
436
  await runDirectories()
406
437
 
407
- if (sources) {
438
+ if (sources && sources.length) {
408
439
  let sources = await runSources()
409
440
  let newConfig = { ...CoCreateConfig }
410
- if (directories)
441
+ if (directories && directories.length)
411
442
  newConfig.directories = directories
412
443
 
413
444
  newConfig.sources = sources
@@ -427,11 +458,13 @@ module.exports = async function file(CoCreateConfig, configPath) {
427
458
  fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(newConfig, null, 4)};`);
428
459
  }
429
460
 
430
- console.log('upload complete!');
461
+ if (!match.length) {
462
+ console.log('upload complete!');
431
463
 
432
- setTimeout(function () {
433
- process.exit()
434
- }, 2000)
464
+ setTimeout(function () {
465
+ process.exit()
466
+ }, 2000)
467
+ }
435
468
  }
436
469
 
437
470
  run()