2016-06-09 50 views
0

我想下載FTP服務器上的遠程位置到我的本地目錄。我一直在使用jsftp模塊(https://github.com/sergi/jsftp),但我遇到了一個問題。NodeJS下載遠程到本地FTP

基本上我試圖遞歸地下載整個遠程位置,包括所有的子文件夾等。我一直試圖弄清楚自己,但迄今沒有運氣。

到目前爲止,我一直想這樣的:

var worker = { 
    ftp: null, 
    init: function() { 
     this.ftp = new JSFtp({ 
      host: "localhost", 
      port: 21, 
      user: "username", 
      pass: "password" 
     }); 
     this.ftp.auth("username", "password", function (result) { 
      if (typeof result !== "undefined") { 
       console.log("Something went wrong"); 
      } 

      this.handleData(); 
     }.bind(this)); 
    }, 
    handleData: function() { 
     recursive_get_files(this, "/"); 
    } 
}; 
function recursive_get_files(worker, dir) { 
    console.log("Getting directory: " + dir); 
    worker.ftp.ls(dir, function (err, res) { 
     res.forEach(function (file) { 
      if (file.type === 1) { 
       recursive_get_files(worker, dir + "/" + file.name); 
      } else { 
       worker.ftp.get(dir + "/" + file.name, "/downloads" + dir + "/" + file.name, function(err) { 
        if (err) { 
         console.log("Couldn't download file: " + file.name); 
         console.log(err); 
        } 
       }.bind(file.name)); 
      } 
     }); 
    }); 
} 

最大的問題,我認爲,是所有這些功能得到越來越之後對方叫幾乎是瞬間,由於客戶端可能是不允許的撥打這麼多東西,它會打破。

我見過一個名爲ftpsync的模塊(https://www.npmjs.com/package/ftpsync)做了一些遠程同步,從本地到遠程,但我需要繞過這條路。

任何人都可以幫助我嗎?我一直在這一天卡住= /。

回答

0

我能夠做與我的一個朋友來解決這個問題如下:

function recursive_get_files(workerf, dir) { 
    total_pending++; 
    var worker = workerf; 
    var done = function() { 
     //probably callback here if needed 
     console.log("finished downloading ftp"); 
     wrench.chmodSyncRecursive('downloads', 0777); 
     worker.respond(JSON.stringify({type: "success", message: "Finished"})); 
    }; 
    var func_download = function() { 
     //Is the download queue empty? 
     if(download_queue.length === 0) 
     { 
      total_running--; 

      if(total_running === 0 && total_pending === 0) 
      { 
       done(); 
      } 

      return; 
     } 

     //Get the next download in the queue 
     var download = download_queue[0]; 
     download_queue.splice(0, 1); 

     //Get a free FTP connection 
     var curftp; 
     for(curftp = 0; curftp < total; curftp++) { 
      if (worker.ftp[curftp].used === false) { 
       worker.ftp[curftp].used = true; 
       break; 
      } 
     } 

     //Get the file 
     worker.ftp[curftp].ftp.get(download.dir + "/" + download.file.name, "downloads/" + worker.project + download.dir + "/" + download.file.name, function(file, err) { 
      worker.ftp[curftp].used = false; 
      if (err) 
      { 
       console.log("Couldn't download file with FTP(" + curftp + "): " + file); 
       console.log(err); 
       setTimeout(func_download, 0); 
      } 
      else 
      { 
       console.log("Downloaded file with FTP(" + curftp + "): " + file); 
       setTimeout(func_download, 0); 
      } 
     }.bind(null, download.file.name)); 
    }; 

    //Get a list of the current directory (Using the main connection) 
    worker.mainftp.ls(dir, function (err, res) { 
     res.forEach(function (file) { 
      if (file.type === 1) 
      { 
       mkdirp("downloads/" + worker.project + dir + "/" + file.name, function(err) { 
        if (err) console.log(err); 
        recursive_get_files(worker, dir + "/" + file.name); 
       }); 
      } 
      else 
      { 
       download_queue.push({file: file, dir: dir}); 

       if(total_running < total) 
       { 
        total_running++; 
        setTimeout(func_download, 0); 
       } 
      } 
     }); 
    }); 

    total_pending--; 
    if(total_running === 0 && total_pending === 0 && download_queue.length === 0) 
    { 
     done(); 
    } 
} 

它使用wrench.js(遞歸CHMOD)和jsftp。感謝您閱讀^^