8 Replies Latest reply on Jan 9, 2017 12:58 PM by jar0we

    Downloading file with NodeJS in CEP extension

    danf33957308 Level 1

      I'm trying to get file downloading with NodeJS working in my CEP extension for Premiere Pro CC 2015. I have the following script:

       

      var http = require('http');
      var fs = require('fs');
      var file = fs.createWriteStream("file.jpg");
      var request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
          response.pipe(file);
      });
      
      
      

       

      The script works perfectly well if I run it on it's own. For example, if I put it in a file called test.js, and run node test.js in terminal, it successfully saves the image to the same directory the script is in.

       

      When I include it in my CEP extension, however, nothing happens. The script executes without errors, but no file is downloaded anywhere. Even if I try:

      try {
           response.pipe(file);
           alert("File downloaded");
      } catch(e) {
           alert(e);
      }
      

       

      The "File downloaded" alert pops up. So the lack-of-download doesn't seem to be from an error in the code, but perhaps in the way CEP works itself? I'm at a loss. Any suggestions?

       

      Thanks!

        • 1. Re: Downloading file with NodeJS in CEP extension
          Liam Dilley Adobe Community Professional & MVP

          Moved to right forum, you were in the Business Catalyst development forum.

          • 3. Re: Downloading file with NodeJS in CEP extension
            danf33957308 Level 1

            Update:

            Looks like it might be a permissions issue. I altered the createWriteStream path to write to a file on my desktop, and the download worked from the extension.

             

            To clarify, this extension will be used to download audio files from a website our company hosts, and import them directly into Premiere Pro. Therefore, the files need to be downloaded somewhere consistent on the client machine. That's why I'm trying to download to the actual extension folder itself.

             

            If this isn't possible, can anyone suggest a way to reliably download the file to a consistent location on the client machine, so that I can then immediately import it into Premiere?

             

            Thanks.

            • 4. Re: Downloading file with NodeJS in CEP extension
              Ann Bens Adobe Community Professional & MVP

              Still not the correct forum.

              Moved to Premiere Pro SDK

              • 5. Re: Downloading file with NodeJS in CEP extension
                danf33957308 Level 1

                For anyone interested, I came up with a solution.

                 

                Instead of saving to the directory of the extension, you can save a downloaded file to the path of the project, using app.project.path. Here's my commented code:

                 

                In hostscript.jsx

                function returnPath() {
                     var projectPath = app.project.path;
                     // split the path into an array
                     var parsed = projectPath.split("/");
                     // remove the last element (which is the name of the Premiere project file)
                     parsed.pop();
                     // rejoin the array, giving the parent directory of the Premiere project file
                     var joined = parsed.join("/");
                     return(joined);
                }
                

                 

                In main.js

                function downloadAndImport(url, songName) {
                     // call the evalScript we made in the jsx file
                     csInterface.evalScript('returnPath()', function(result) {
                          var https = require('https');
                          var fs = require('fs');
                          var mkdirp = require('mkdirp');
                          // create a Downloads directory in the project path if it doesn't exist already
                          var downloadDirectory = result + '/Downloads';
                          mkdirp(downloadDirectory, function(err){
                               if (err) { // handle error }
                          });
                          var fullPath = downloadDirectory + "/" + songName;
                          var file = fs.createWriteStream(fullPath);
                          var request = https.get(url, function(response) {
                               response.pipe(file);
                               // ensure file is complete before importing
                               response.on('end', function() {
                                    csInterface.evalScript("app.project.importFiles(['" + fullPath + "'])");
                               });
                         });
                     });
                }
                

                 

                This works for my needs. Hopefully it will help someone else!

                • 6. Re: Downloading file with NodeJS in CEP extension
                  akjj69643200 Level 1

                  Thx,  I already find solution.

                  • 7. Re: Downloading file with NodeJS in CEP extension
                    SirGatlin

                    Amazing, Thank you for coming back and sharing your solution. This worked like a charm. saved me.

                    • 8. Re: Downloading file with NodeJS in CEP extension
                      jar0we

                      Curious if anything here needs to be changed for PC. for some reason it seems as if my .jsx file is not  being read on my PC test computer at all. anyone have an idea?