• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Downloading file with NodeJS in CEP extension

Community Beginner ,
Dec 29, 2015 Dec 29, 2015

Copy link to clipboard

Copied

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!

TOPICS
SDK

Views

5.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Dec 30, 2015 Dec 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, gi

...

Votes

Translate

Translate
LEGEND ,
Dec 29, 2015 Dec 29, 2015

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

Still not the correct forum.

Moved to

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 30, 2015 Dec 30, 2015

Copy link to clipboard

Copied

Thx,  I already find solution.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

LATEST

Hello danf33957308,

I am trying to download an image file from internet via index.html file from my illustrator extension ,

tried to use your piece of code also but nothing happens,

Could you help or give your working set so that i could proceed.  

thnaks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 28, 2016 Sep 28, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 09, 2017 Jan 09, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines