MeetJoeBlack
MeetJoeBlack

Reputation: 2904

node.js download image from url

My issue is download image with unknown extenstion(it maybe jpg ,or png ,or jpeg, or bmp) from url. So I want to check Content-Length of the image and if it bigger then 0, download it to file,else try download image with another extension and etc.

var fs = require('fs');
var request = require('request');
var xml2js = require('xml2js');
var Q =  require('q');

var baseUrl = 'http://test.com/uploads/catalog_item_image_main/';

Q.nfcall(fs.readFile, "./test.xml", "utf8")
    .then(parseSrting)
    .then(parseProductsAsync)
    .then(processProductsAsync)
;


function parseSrting(data){
    return Q.nfcall(xml2js.parseString,data);
}

function parseProductsAsync(xmljsonresult){
    return xmljsonresult.product_list.product;
}


function processProductsAsync(products){
    products.map(function(product){

        var filename = product.sku + ""; // - where is image name
        filename = filename.replace(/\//g, '_');
        console.log('Processing file ' + filename);

        var imageUrl = baseUrl + filename + '_big.';  // + image extension

        //There I want to check Content-Length of the image and if it bigger then 0, download it to file,
        //else try download image with another extension and etc.

    });
}

I am using Q promises module for Node.js to avoid callbacks hell, but can someone help me with checking image size and save it to file?

Upvotes: 3

Views: 28532

Answers (2)

Aryan
Aryan

Reputation: 3636

Now request module is deprecated so it will not help more

try using url module like bellow

I am downloding or sending image in response from querying details from url

const fs = require('fs');
const url = require('url')

download_image: async (req, res) => {


    let query = url.parse(req.url, true).query;
    let pic = query.image;
    let id = query.id

    let directory_name = "tmp/daily_gasoline_report/" + id + "/" + pic

    let filename = fs.existsSync(directory_name);

    if (filename) {

        //read the image using fs and send the image content back in the response
        fs.readFile(directory_name, function (err, content) {
            if (err) {
                res.writeHead(400, { 'Content-type': 'text/html' })
                console.log(err);
                res.end("No such image");
            } else {
                //specify the content type in the response will be an image
                res.writeHead(200);
                res.end(content);
            }
        });
    } else {
        logger.warn(error.NOT_FOUND)
        return res.status(401).send(error.NOT_FOUND)
    }
  }

Upvotes: 0

Ivy Jha
Ivy Jha

Reputation: 101

You can check the status code of the response. If it was 200, the image was fetched with no problems.

You can use an array of file extensions and a recursive method to try each file extension in sequence. Using the request module you can do it like this:

function processProductsAsync(products){
    products.map(function(product){

        var filename = product.sku + ""; // - where is image name
        filename = filename.replace(/\//g, '_');
        console.log('Processing file ' + filename);

        var imageUrl = baseUrl + filename + '_big.';  // + image extension

        fetchImage(imageUrl, filename, 0);
});

function fetchImage(url, localPath, index) {
    var extensions = ['jpg', 'png', 'jpeg', 'bmp'];

    if (index === extensions.length) {
        console.log('Fetching ' + url + ' failed.');
        return;
    }

    var fullUrl = url + extensions[index];

    request.get(fullUrl, function(response) {
        if (response.statusCode === 200) {
            fs.write(localPath, response.body, function() {
                console.log('Successfully downloaded file ' + url);
            });
        }

        else {
            fetchImage(url, localPath, index + 1);
        }
    });
}

Upvotes: 4

Related Questions