Sails: How to get file name instead of file full system path after uploaded?

Created on 26 Dec 2014  Â·  13Comments  Â·  Source: balderdashy/sails

The callback after upload success is just an empty array. Did I miss something or it's what it should be?

Most helpful comment

I use this, which I find pretty neat (http://nodejs.org/api/path.html#path_path_basename_p_ext):

var path = require('path');
...
file.filename = path.basename(file.fd);

This should work on both windows and unix I'd guess as part of the Node.js core.

All 13 comments

See my solution in #35.

@johntom thank you, I will take a loot at that. But I wonder is it true that Skipper it self has not capable to doing this thing?

I will look at my solution later and send code snip later. Works really well.

J

From: Andi N. Dirgantara [mailto:[email protected]]
Sent: Friday, December 26, 2014 9:37 AM
To: balderdashy/skipper
Cc: John R. Tomaselli
Subject: Re: [skipper] How to get filename after uploaded? (#57)

@johntom https://github.com/johntom thank you, I will take a loot at that. But I wonder is it true that Skipper it self has not capable to doing this thing?

—
Reply to this email directly or view it on GitHub https://github.com/balderdashy/skipper/issues/57#issuecomment-68143268 . https://github.com/notifications/beacon/ABylgNbKnQ8KKvbbN4Fp9TKywQZrV1scks5nbWn_gaJpZM4DMSc6.gif


No virus found in this message.
Checked by AVG - www.avg.com
Version: 2015.0.5577 / Virus Database: 4257/8785 - Release Date: 12/22/14

@johntom Hello sir, I have look at your solution. But it's a way to know some of my file attributes before it's uploaded. What about when it's uploaded?

Based on Skipper's docs: "By default, Skipper decides an "at-rest" filename for your uploaded files (called the fd) by generating a UUID and combining it with the file's original file extension when it was uploaded ("e.g. 24d5f444-38b4-4dc3-b9c3-74cb7fbbc932.jpg")." How to get that name and returned in JSON response?

Sorry, I have found that. It exist in the callback, but It's full path, not the filename itself. I use this kind of approach at my callback:

function onUploadComplete (err, uploaded){
    if (err) { return res.serverError(err); }
    res.json({
      status : true,
      path: 'uploads/media/', //my own path relative to sails app location
      filename: uploaded[0].fd.split('/').reverse()[0] //my approach
    });
  });

That is also can be done using regex, but it's so ugly and useless I think, since "why we need to print out our full system path?".

You could use Path to get all the path/file details you need:

http://nodejs.org/api/path.html

Specifically for the filename: http://nodejs.org/api/path.html#path_path_basename_p_ext

Also, you can define the filename yourself by using the saveAs option for upload. You can either use a string (not recommended, since all files will have the same name, so you'd need to make sure the string is unique) or a function

req.file('avatar').upload({
  saveAs: function (__newFileStream,cb) { 
                        //do something to generate a unique name
                        myUniqueName;
                        cb(null, myUniqueName); 
                }
}, ...);

@cburatto thank you. I forget to change the question. I'm currently able to get file name from file path location in the fd value at callback. But I think it should return filename instead of file full system path. The fd key has a value like /home/user/.../.../uniquenamefile.jpg, I think it's better to be uniquenamefile.jpg only.

Thanks for clarifying. I personally prefer to have the full path and then decide how to break it down if needed. As it is, I can simply write some code to manipulate the file instead of rebuilding the path to get access to the file. But each case is different.

@cburatto I see. Is Skipper it self will stay on this way? I think an API that return a filename is needed. But if it's doesn't make sense, it's ok since it's retrievable via full path.
I'm also afraid it will broke some existing user if the API changed :laughing:

If I understand correctly what you're asking, you can take the full path stored in fd, use the split method with '/' on it, and the last element will always be the filename with the extension.

@stavrosf4 yes it's true, I already mentioned in my comment above. I use:

function onUploadComplete (err, uploaded){
    if (err) { return res.serverError(err); }
    res.json({
      status : true,
      path: 'uploads/media/', //my own path relative to sails app location
      filename: uploaded[0].fd.split('/').reverse()[0] //my approach
    });
  });

But it's very inconvenient, because the path is depends on environment/ OS that we use. For example in Windows, it will result nothing. Because Windows use "\" not "/".

I use this, which I find pretty neat (http://nodejs.org/api/path.html#path_path_basename_p_ext):

var path = require('path');
...
file.filename = path.basename(file.fd);

This should work on both windows and unix I'd guess as part of the Node.js core.

@albertpeiro woah very nice, thank you very much. It looks like the core dev still focus on Sails core. I'll close this issue.

Was this page helpful?
0 / 5 - 0 ratings