nodejs 批量下载保存远程图片文件同时更新数据库

需要引入的库文件:

var async = require('async');
var originRequest = require('request');
var mysql = require('mysql');
var fs = require('fs');

核心代码如下:

/*
*@param array list 列表
*@param int id 编号
*/
var updateDetailMainPic = function(list,id){
    console.log(id+"-length:"+list.length);
    var relative_path = conf.uploadDir;//此处需要根据自己的修改 我的这个是 D:/rin_pro/rinblog/public
    var myid = id;
    //============================
    //获取详情展示图
    var imgArr = list;
    imgArr.forEach(function(row){

        var row_orgImg = row.Pic500;
        var row_midImg = row.Pic160;
        var row_smallImg = row.small_pic;
        var row_title = row.Title;
        var row_orgPath = row_orgImg.replace('http://img1.xxx.com','');//原始图
        row_orgPath = doFileUrl(row_orgPath);
        db.query('SELECT * FROM `spi_hotel_pic` WHERE `hotelId`=? and `orgPic`=? and  `isDetail`=1  LIMIT 1', [myid,row_orgPath], function (err, data) {
            if (Array.isArray(data) && data.length >= 1) {
              // 已存在,更新一下
            } else {
              console.dir(row_orgPath);
              // 不存在,添加
              var row_midPath = row_midImg.replace('http://img1.xxx.com','');//中等
              var row_smallPath = row_smallImg.replace('http://img1.xxx.com','');//小图
              row_midPath = doFileUrl(row_midPath);
              row_smallPath = doFileUrl(row_smallPath);
              var row_filedir = relative_path+getFileDir(row_orgPath);
              console.dir(row_filedir);

              if (!fs.existsSync(row_filedir)) {
                  fs.mkdirSync(row_filedir);
              }

              try    {
                  var r = originRequest(row_orgImg);
                                r.on('response', function (resp) {
                                   resp.headers 
                                   resp.statusCode
                                   r.pipe(fs.createWriteStream(relative_path+row_orgPath));
                                });//原始图

              }   catch  (e)   {
                    console.log('err:'+row_orgPath);
                    row_orgPath = null;
              } 
              try    {
                  var r1 = originRequest(row_midImg);
                                r1.on('response', function (resp) {
                                   resp.headers
                                   resp.statusCode
                                   r1.pipe(fs.createWriteStream(relative_path+row_midPath));
                                });//中图

              }   catch  (e)   {
                    console.log('err:'+row_midPath);
                    row_midPath = null;
              } 
              try    {
                  var r2 = originRequest(row_smallImg);
                                r2.on('response', function (resp) {
                                   resp.headers 
                                   resp.statusCode
                                   r2.pipe(fs.createWriteStream(relative_path+row_smallPath));
                                });//小图

              }   catch  (e)   {
                    console.log('err:'+row_smallPath);
                    row_smallPath = null;
              } 

              db.query('INSERT INTO `spi_hotel_pic`(`hotelId`, `title`, `orgPic`, `smallPic`, `midPic`, `isDetail` ) VALUES (?, ?, ?, ?, ?, ?)', 
                      [myid, row_title, row_orgPath, row_smallPath, row_midPath, 1]);
            }
          });
    });
};


var doFileUrl = function(o){
    var filepath = o.replace('hotel_img','Hotel_Images');
    filepath = filepath.replace('uppic/temupload','Hotel_Images');
    return filepath;
};

//获取文件路径
var getFileDir = function(o){
    var pos=o.lastIndexOf("/");
    return o.substring(0,pos+1);  
};