var request = require('request');
var fs = require('fs');
var mysql = require('mysql');
var mydb = mysql.createConnection({
host: 'localhost',
// port: 3306,
user: 'root',
password: 'root',
//charset: 'UTF8_GENERAL_CI',
debug: false
});
mydb.connect(function(err){
if (err) {
console.error(err);
throw err;
}
});
mydb.query('USE `dbname`', function(err){
if (err) {
console.error(err);
throw err;
}
});
mydb.query("SELECT `id`,`image` FROM `fy_scenic` WHERE INSTR(`image`, 'http://') ORDER BY `image` ", function(err, rst) {
if (err) {
console.error(err);
throw err;
}
rst.forEach(function(row) {
console.log(row);
var filepath = 'old/'+row.image.substr(36);
var filedir = filepath.substr(0,7);
if (!fs.existsSync(filedir)) {
fs.mkdirSync(filedir);
}
request(row.image).pipe(fs.createWriteStream(filepath));
var sql = "UPDATE `fy_scenic` SET `image`=? WHERE `id`=? ";
sql = mydb.format(sql, [filepath, row.id]);
console.log(sql);
mydb.query(sql, function(err, rst) {
if (err) {
console.error(err);
throw err;
}
});
});
});