mango-cms 0.2.12 → 0.2.13

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.
@@ -333,6 +333,72 @@ Mango.endpoints = Object.keys(endpoints).reduce((a, c) => {
333
333
 
334
334
  }, {})
335
335
 
336
+ Mango.upload = async (file) => {
337
+
338
+ return new Promise((resolve, reject) => {
339
+ const formData = new FormData()
340
+
341
+ let uploading = true
342
+ let filename = file.name
343
+ let progress = 0
344
+ let url
345
+ let error
346
+
347
+ // // Compress the image
348
+ // if (file.type.includes('image')) {
349
+ // let results = await compress.compress([file], {
350
+ // quality: .75, // the quality of the image, max is 1,
351
+ // maxWidth: 1920, // the max width of the output image, defaults to 1920px
352
+ // maxHeight: 1920, // the max height of the output image, defaults to 1920px
353
+ // resize: true, // defaults to true, set false if you do not want to resize the image width and height
354
+ // rotate: false, // See the rotation section below
355
+ // })
356
+ // const img1 = results[0]
357
+ // const base64str = img1.data
358
+ // const imgExt = img1.ext
359
+ // const filename = file.name
360
+ // file = Compress.convertBase64ToFile(base64str, imgExt)
361
+ // file = new File([file], filename, { type: file.type });
362
+ // console.log('file', file)
363
+ // }
364
+
365
+ formData.append('file', file)
366
+
367
+ const xhr = new XMLHttpRequest()
368
+
369
+ xhr.open('POST', `${api}/upload`, true)
370
+
371
+ xhr.upload.onprogress = (event) => {
372
+ if (event.lengthComputable) {
373
+ progress = (event.loaded / event.total) * 100
374
+ }
375
+ }
376
+
377
+ xhr.onload = () => {
378
+ if (xhr.status === 200) {
379
+ const json = JSON.parse(xhr.response)
380
+ const path = json.paths[0]
381
+ const url = api + path
382
+ uploading = false
383
+ progress = 0
384
+ resolve(url)
385
+ } else {
386
+ error = 'Error while uploading file'
387
+ uploading = false
388
+ reject(error)
389
+ }
390
+ }
391
+
392
+ xhr.onerror = () => {
393
+ error = 'Error while uploading file'
394
+ uploading = false
395
+ reject(error)
396
+ }
397
+
398
+ xhr.send(formData)
399
+ })
400
+ }
401
+
336
402
  Mango.collections = collections
337
403
  Mango.ws = ws
338
404
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mango-cms",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "main": "./index.js",
5
5
  "exports": {
6
6
  ".": "./index.js",