Skip to content

res.sendfile does not accept file system paths #1906

@benkaiser

Description

@benkaiser

I have come across an issue with a project I am writing to create a music player with nodejs. I came accross a strange error that would throw on some songs. I couldn't work out what was linking them all at the start but then I figured out it was that they all contained the percentage symbol "%" in their file location. I ruled out if the song was playable and if the url was correct by running the example urls directly from my terminal with mpg123 and they played correctly.

Here is there error:

Error: Bad Request
    at SendStream.error (/home/benkaiser/GIT/node-music-player/node_modules/express.io/node_modules/express/node_modules/send/lib/send.js:145:16)
    at SendStream.pipe (/home/benkaiser/GIT/node-music-player/node_modules/express.io/node_modules/express/node_modules/send/lib/send.js:298:31)
    at ServerResponse.res.sendfile (/home/benkaiser/GIT/node-music-player/node_modules/express.io/node_modules/express/lib/response.js:345:8)
    at /home/benkaiser/GIT/node-music-player/routes/index.js:37:11
    at callback (/home/benkaiser/GIT/node-music-player/node_modules/nedb/lib/executor.js:25:17)
    at Datastore._findOne (/home/benkaiser/GIT/node-music-player/node_modules/nedb/lib/datastore.js:436:10)
    at /home/benkaiser/GIT/node-music-player/node_modules/nedb/lib/executor.js:36:13
    at Object.q.process [as _onImmediate] (/home/benkaiser/GIT/node-music-player/node_modules/nedb/node_modules/async/lib/async.js:728:21)
    at processImmediate [as _immediateCallback] (timers.js:330:15)

Here is the function handling the request for the file:

function sendSong(req, res){
  app.db.songs.findOne({_id: req.params.id}, function(err, song){
    if(err || !song){
      res.status(404).send();
    } else {
      console.log(song.location);
      res.sendfile(song.location, {'root': '/'});
    }
  });
}

And here are some example urls of files that throw the error (notice they have % symbols in them):

/home/benkaiser/Music/Various Artists/100% Hits Of The Decade 2000 -/09 Milkshake.mp3
/home/benkaiser/Music/Showtek/Hardstyle - The Very Best Of 2/00 Rockin' Steady (100% Deepack M.mp3

Is there any sort of escaping that needs to happen for the files to be returned correctly? I could not find any documentation for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions