heavysixer / node-pptx

Generate PPTX files on the server-side with JavaScript.
MIT License
162 stars 45 forks source link

Allow passing an image name when adding to Slide #129

Open tboulis opened 11 months ago

tboulis commented 11 months ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch nodejs-pptx@1.2.4 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/nodejs-pptx/lib/factories/index.js b/node_modules/nodejs-pptx/lib/factories/index.js
index 7809e88..15b7b82 100644
--- a/node_modules/nodejs-pptx/lib/factories/index.js
+++ b/node_modules/nodejs-pptx/lib/factories/index.js
@@ -199,11 +199,11 @@ class PowerPointFactory {
         }
     }

-    addImage(slide, image) {
-        image.setContent(this.pptFactory.addImage(slide, image));
+    addImage(slide, image, name) {
+        image.setContent(this.pptFactory.addImage(slide, image, name));
     }

-    async addImageFromRemoteUrl(slide, image) {
+    async addImageFromRemoteUrl(slide, image, name) {
         image.source = await new Promise(function(resolve, reject) {
             request.get(image.downloadUrl, { timeout: 30000 }, function(err, res, buffer) {
                 if (err) reject(err);
@@ -211,7 +211,7 @@ class PowerPointFactory {
             });
         });

-        return this.addImage(slide, image);
+        return this.addImage(slide, image, name);
     }

     addText(slide, textBox) {
diff --git a/node_modules/nodejs-pptx/lib/factories/ppt/index.js b/node_modules/nodejs-pptx/lib/factories/ppt/index.js
index 9652f45..4796637 100644
--- a/node_modules/nodejs-pptx/lib/factories/ppt/index.js
+++ b/node_modules/nodejs-pptx/lib/factories/ppt/index.js
@@ -74,21 +74,21 @@ class PptFactory {
         this.slideFactory.moveSlide(sourceSlideNum, destinationSlideNum);
     }

-    addImage(slide, image) {
+    addImage(slide, image, name = uuidv4()) {
         let mediaName = '';
         let source = image.source;

         if (image.sourceType === 'file') {
-            mediaName = `image-${uuidv4()}${path.extname(source)}`;
+            mediaName = `${name}${path.extname(source)}`;
         } else if (image.sourceType === 'base64') {
             let imageExt = 'png'; // assume png unless otherwise specified

             if (source && /image\/(\w+);/.exec(source) && /image\/(\w+);/.exec(source).length > 0) imageExt = /image\/(\w+);/.exec(source)[1];
             if (source.indexOf(';') > -1) source = source.split(';').pop();

-            mediaName = `image-${uuidv4()}.${imageExt}`;
+            mediaName = `${name}.${imageExt}`;
         } else if (image.sourceType === 'url') {
-            mediaName = `image-${uuidv4()}${path.extname(image.downloadUrl)}`;
+            mediaName = `${name}${path.extname(image.downloadUrl)}`;
         } else {
             throw new Error('Invalid "sourceType" specified in PptFactory.addImage(). Possible values: "base64," "file," or "binary."');
         }
diff --git a/node_modules/nodejs-pptx/lib/slide.js b/node_modules/nodejs-pptx/lib/slide.js
index b5c93f0..98870e5 100644
--- a/node_modules/nodejs-pptx/lib/slide.js
+++ b/node_modules/nodejs-pptx/lib/slide.js
@@ -56,7 +56,7 @@ class Slide {
         }
     }

-    async addImage(config) {
+    async addImage(config, name) {
         let image = new Image();

         try {
@@ -67,9 +67,9 @@ class Slide {

         try {
             if (image.sourceType === 'file' || image.sourceType === 'base64') {
-                this.powerPointFactory.addImage(this, image);
+                this.powerPointFactory.addImage(this, image, name);
             } else if (image.sourceType === 'url') {
-                await this.powerPointFactory.addImageFromRemoteUrl(this, image);
+                await this.powerPointFactory.addImageFromRemoteUrl(this, image, name);
             }

             this.elements.push(image);

This issue body was partially generated by patch-package.