Skip to main content
danf33957308
Participant
December 29, 2015
Answered

Downloading file with NodeJS in CEP extension

  • December 29, 2015
  • 5 replies
  • 5692 views

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!

Correct answer danf33957308

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!

5 replies

Participating Frequently
January 9, 2017

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?

Participant
September 28, 2016

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

danf33957308
danf33957308AuthorCorrect answer
Participant
December 30, 2015

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!

Participant
December 30, 2015

Thx,  I already find solution.

danf33957308
Participant
December 30, 2015

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.

Ann Bens
Community Expert
Community Expert
December 30, 2015

Still not the correct forum.

Moved to

Liam Dilley
Inspiring
December 29, 2015

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

danf33957308
Participant
December 30, 2015

Thanks