0

I am trying to download and later serve images using Node.js. I need to save each image in a directory specified by the url. My code for downloading images gets stuck because there is no directory to save them to. I am trying to create one using mkdirp but keep getting the error [Error: EACCES: permission denied, mkdir '/20110'] errno: -13, code: 'EACCES', syscall: 'mkdir', path: '/20110'. Here is my full code:

controller.serveImages = function(req, res, connection) {
    var file = "/" + req.params.attachmentId + "/" + req.params.attachmentFileName;
    console.log("serve called", file);
    var fs = require('fs'),
        request = require('request');
    var mkdirp = require('mkdirp');
    mkdirp( "/" + req.params.attachmentId, function (err) {
        if (err) console.error(err)
        else console.log('Directory made!')
    });
    fs.readFile(file, function(error, data) {
        console.log("reading");
        if(error){
            if (error.code === 'ENOENT') { //File not downloaded
                var download = function(uri, filename, callback){
                    request.head(uri, function(err, res, body){
                        console.log('content-type:', res.headers['content-type']);
                        console.log('content-length:', res.headers['content-length']);

                        request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
                    });
                };

                download('https://www.google.com/images/srpr/logo3w.png', file, function(){
                    console.log('done');
                });
            }
            else{
                console.log(error)
            }
        }
        else{
            console.log("Found locally", data, file);
            res.sendFile(file);
        }
    });

};

The code is adapted from here. An example request would be /20110/TNCA+PB.png. With that I would want to create a directory /20110 and save TNCA+PB.png there.

taurus
  • 470
  • 7
  • 22
  • 1
    The answer to your solution lies in the error code I believe: `EACCES: permission denied` Your program needs the correct permissions to that directory. – infamoustrey Jun 07 '18 at 18:13
  • 1
    Seems that you're trying to create the folder at File System root level. Have you tried to create the folder under the `application` folder instead? Writing to root level is never a good idea. – David Espino Jun 07 '18 at 18:14
  • 1
    Here are some suggestions to get the app running path https://stackoverflow.com/questions/10265798/determine-project-root-from-a-running-node-js-application... then you can append that to your mkdirp function – David Espino Jun 07 '18 at 18:16
  • @DavidEspino That helped me specify the correct, thank you! Feel free to submit an answer. – taurus Jun 07 '18 at 18:42
  • Done!... Glad to help :) – David Espino Jun 07 '18 at 21:33

1 Answers1

1

Seems that you're trying to create the folder at File System root level. Have you tried to create the folder under the application folder instead? Writing to root level is never a good idea.

Here are some suggestions to get the app running path Determine project root from a running node.js application , then you can append that to your mkdirp function.

Regards

David Espino
  • 2,177
  • 14
  • 21