Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing an image name when adding to Slide #129

Open
tboulis opened this issue Oct 19, 2023 · 0 comments
Open

Allow passing an image name when adding to Slide #129

tboulis opened this issue Oct 19, 2023 · 0 comments

Comments

@tboulis
Copy link

tboulis commented Oct 19, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant