@innovastudio/contentbox 1.2.3 → 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/README.md CHANGED
@@ -132,6 +132,8 @@ React and Vue project examples are provided in separate downloads in the user ar
132
132
  - scss/ (Only provided in Source Code package)
133
133
  - docs/
134
134
  - README.md (Documentation)
135
+ - readme.txt (Readme file)
136
+ - readme-sourcecode.txt (Readme file for Source Code package)
135
137
  - ...
136
138
 
137
139
 
@@ -400,47 +402,92 @@ Here we use the **onUploadCoverImage** parameter to specify a custom upload func
400
402
  const builder = new ContentBox({
401
403
  wrapper: '.is-wrapper',
402
404
  onUploadCoverImage: (e) => {
403
- const selectedFile = e.target.files[0];
404
- const filename = selectedFile.name;
405
- const reader = new FileReader();
406
- reader.onload = (e) => {
407
- let base64 = e.target.result;
408
- base64 = base64.replace(/^data:(.*?);base64,/, "");
409
- base64 = base64.replace(/ /g, '+');
410
-
411
- // Upload process
412
- axios.post('/upload', { image: base64, filename: filename }).then((response)=>{
413
-
414
- const uploadedImageUrl = response.data.url; // get saved image url
415
- builder.boxImage(uploadedImageUrl); // change cover image
416
-
417
- }).catch((err)=>{
418
- console.log(err)
419
- });
420
- }
421
- reader.readAsDataURL(selectedFile);
405
+ uploadFile(e, (response)=>{
406
+ const uploadedImageUrl = response.url; // get saved image url
407
+ builder.boxImage(uploadedImageUrl); // change cover image
408
+ });
422
409
  },
423
410
  });
411
+
412
+ function uploadFile(e, callback) {
413
+
414
+ const selectedFile = e.target.files[0];
415
+
416
+ const formData = new FormData();
417
+ formData.append('file', selectedFile);
418
+ fetch('/upload', {
419
+ method: 'POST',
420
+ body: formData,
421
+ })
422
+ .then(response=>response.json())
423
+ .then(response=>{
424
+ console.log(response)
425
+ if(callback) callback(response);
426
+ });
427
+ }
424
428
  ```
425
429
 
426
- Within the function we can use Axios to post an image to the server. Then the server should return the saved image url. Here we use the **boxImage(url)** method to apply the image url as a background cover.
430
+ The example above uses the **boxImage(url)** method to apply the image url as a background cover.
427
431
 
428
- In this example, the image is posted to an endpoint: **/upload**
432
+ In the example, the image is posted to an endpoint: **/upload**
429
433
  If you’re using Node.js, you can implement the endpoint to save the image using:
430
434
 
431
435
  ```javascript
436
+ const express = require('express');
437
+ const fs = require('fs');
438
+ const app = express();
439
+ const path = require('path');
440
+ const cors = require('cors');
441
+ const serveStatic = require('serve-static');
442
+ const formidable = require('formidable-serverless');
443
+ const sharp = require('sharp');
444
+
445
+ //Specify url path
432
446
  var $path = __dirname + '/public/uploads/'; // Physical path
433
447
  var $urlpath = 'uploads/'; // URL path
434
448
 
449
+ app.use(cors());
450
+ app.use(express.urlencoded({
451
+ extended: true
452
+ }));
453
+ app.use(express.json({ limit: '50mb' }));
454
+ app.use(serveStatic(path.join(__dirname, '/public')));
455
+
435
456
  app.post('/upload', (req, res) => {
436
- const base64Data = req.body.image;
437
- const filename = req.body.filename;
438
- require('fs').writeFile($path + filename, base64Data, 'base64', ()=>{
439
- res.status(200).json({
440
- success: true,
441
- url: `${$urlpath}${filename}` // return saved image url
442
- })
457
+
458
+ const form = new formidable.IncomingForm();
459
+ form.parse(req, async (err, fields, files) => {
460
+
461
+ if (err) return res.status(500).json({ ok:true, status: 500, error: 'Something went wrong.' });
462
+
463
+ let extension = path.extname(files.file.name).toLowerCase();
464
+ if(extension !== '.jpeg' && extension !== '.jpg' && extension !== '.png' && extension !== '.gif' && extension !== '.webp' && extension !== '.webm' && extension !== '.mp4') {
465
+ res.status(500).json({ ok:true, status: 500, error: 'File type not allowed.' });
466
+ return;
467
+ }
468
+
469
+ const file = fs.readFileSync(files.file.path);
470
+
471
+ let imageFile = file;
472
+ if(extension === '.jpeg' || extension === '.jpg') {
473
+ imageFile = await sharp(files.file.path).resize(1600, 1600, {
474
+ fit: sharp.fit.inside,
475
+ withoutEnlargement: true,
476
+ })
477
+ .jpeg({ quality: 80, progressive: true, force: false })
478
+ .toBuffer();
479
+ }
480
+
481
+ fs.writeFile($path + files.file.name, imageFile, async (err)=>{
482
+ if (err) {
483
+ res.status(500).json({ ok:true, status: 500, error: 'Something went wrong.' });
484
+ return
485
+ }
486
+ res.status(200).json({ ok:true, status: 200, url: $urlpath + files.file.name});
487
+ });
488
+
443
489
  });
490
+
444
491
  });
445
492
  ```
446
493
 
@@ -513,39 +560,12 @@ const builder = new ContentBox({
513
560
  wrapper: '.is-wrapper',
514
561
 
515
562
  onMediaUpload: (e)=>{
516
- uploadFile(e, (response)=>{
517
- const uploadedImageUrl = response.data.url; // get saved file url
518
- builder.returnUrl(uploadedImageUrl); // appply
519
- });
563
+ ...
520
564
  },
521
565
  onVideoUpload: (e)=>{
522
- uploadFile(e, (response)=>{
523
- const uploadedFileUrl = response.data.url; // get saved file url
524
- builder.returnUrl(uploadedFileUrl); // apply
525
- });
566
+ ...
526
567
  },
527
568
  });
528
-
529
- function uploadFile(e, callback) {
530
- const selectedFile = e.target.files[0];
531
- const filename = selectedFile.name;
532
- const reader = new FileReader();
533
- reader.onload = (e) => {
534
- let base64 = e.target.result;
535
- base64 = base64.replace(/^data:(.*?);base64,/, "");
536
- base64 = base64.replace(/ /g, '+');
537
-
538
- // Upload process
539
- axios.post('/upload', { image: base64, filename: filename }).then((response)=>{
540
-
541
- callback(response);
542
-
543
- }).catch((err)=>{
544
- console.log(err)
545
- });
546
- }
547
- reader.readAsDataURL(selectedFile);
548
- }
549
569
  ```
550
570
 
551
571
  More configuration options can be found in the ContentBuilder.js documentation: https://innovastudio.com/docs/contentbuilder.pdf
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innovastudio/contentbox",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "",
5
5
  "main": "public/contentbox/contentbox.esm.js",
6
6
  "files": [
@@ -39,7 +39,7 @@
39
39
  "webpack-dev-server": "^4.0.0"
40
40
  },
41
41
  "dependencies": {
42
- "@innovastudio/contentbuilder": "^1.1.7",
42
+ "@innovastudio/contentbuilder": "^1.1.8",
43
43
  "axios": "^0.21.4",
44
44
  "cors": "^2.8.5",
45
45
  "express": "^4.17.1",