Question

I'm writing a script to download files from urls in a list. The problem I'm having is that the urls don't just point to static files, like file.jpg, they tend to point to servlets that return a file.

What I want to do is download the file for each url and save it with a generic name, then read its headers and rename it with the appropriate extension. (Unless there's a better way)

How could I do that?

I've tried using mime-magic, but it tells me that the extension-less files are directories.

Was it helpful?

Solution

It should work using mime-magic. Are you sure the path is correct and the path is not pointing to a directory?

Otherwise you could use the command line tool file --mime /path/to/file

Here is how to detect an extension of a file using mime-magic:

mime('/path/to/foo.pdf', function (err, type) {
    if (err) {
        console.error(err.message);
        // ERROR: cannot open `/path/to/foo.pdf' (No such file or directory)
    } else {
        console.log('Detected mime type: %s', type);
        // application/pdf
    }
});

Note: Added sled's comment as an answer under community-wiki.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top