diff --git a/node_modules/api.js b/node_modules/api.js index ab89eed4..2d36b2a2 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -228,13 +228,21 @@ var Api = { getImage: function(fileId, callback) { var me = this; var url = me.getUrl('file/getImage', {fileId: fileId}); - log(url); + + // console.log('getImage'); + // console.log(url); + needle.get(url, function(err, resp) { me.checkError(err, resp); if(err) { return callback && callback(false); } - // log(resp.body); + else if (resp.statusCode != 200) { + console.log(fileId + ' 图片返回状态错误: ' + resp.statusCode); + return callback && callback(false); + } + + // log(resp); /* { 'accept-ranges': 'bytes', 'content-disposition': 'inline; filename="logo.png"', @@ -243,10 +251,14 @@ var Api = { date: 'Mon, 19 Jan 2015 15:01:47 GMT', */ // log(resp.headers); - if(err) { - callback(false); - } else { - var typeStr = resp.headers['content-type']; + else { + var typeStr = ('' + resp.headers['content-type']).toLowerCase(); + + if (typeStr.indexOf('image') < 0) { + console.log(fileId + ' 不是图片'); + return callback && callback(false); + } + var type = 'png'; if(typeStr) { var typeArr = typeStr.split('/'); @@ -258,13 +270,13 @@ var Api = { var filename = Common.uuid() + '.' + type; var imagePath = User.getCurUserImagesPath(); var imagePathAll = imagePath + '/' + filename; - log(imagePathAll); fs.writeFile(imagePathAll, resp.body, function(err) { if(err) { log(err); log('local save image failed 本地保存失败'); callback(false); } else { + console.log('main save image success'); callback(imagePathAll, filename); } }); diff --git a/node_modules/api_main.js b/node_modules/api_main.js index 3ccbfb9a..597f7448 100644 --- a/node_modules/api_main.js +++ b/node_modules/api_main.js @@ -41,12 +41,20 @@ var Api = { getImage: function(fileId, callback) { var me = this; var url = me.getUrl('file/getImage', {fileId: fileId}); + + // console.log('getImage'); + // console.log(url); + needle.get(url, function(err, resp) { me.checkError(err, resp); if(err) { return callback && callback(false); } - // log(resp.body); + else if (resp.statusCode != 200) { + console.log(fileId + ' 图片返回状态错误: ' + resp.statusCode + ' ' + url); + return callback && callback(false); + } + // log(resp); /* { 'accept-ranges': 'bytes', 'content-disposition': 'inline; filename="logo.png"', @@ -56,7 +64,13 @@ var Api = { */ // log(resp.headers); else { - var typeStr = resp.headers['content-type']; + var typeStr = ('' + resp.headers['content-type']).toLowerCase(); + + if (typeStr.indexOf('image') < 0) { + console.log(fileId + ' 不是图片 ' + typeStr + ' ' + url); + return callback && callback(false); + } + var type = 'png'; if(typeStr) { var typeArr = typeStr.split('/'); diff --git a/node_modules/file.js b/node_modules/file.js index b0396e9e..19c743a8 100644 --- a/node_modules/file.js +++ b/node_modules/file.js @@ -532,7 +532,7 @@ var File = { } else { // 远程取不到图片, 是没有网络? 还是远程真的没有了 // TODO - // console.log("cann't get server's image" + fileId); + console.log("cann't get server's image" + fileId); callback(false); // return me.e404(res); } @@ -547,8 +547,8 @@ var File = { // console.log(fs.exists(fileLocalPath)); if(has && fileLocalPath) { fs.exists(fileLocalPath, function(exists) { - if(exists) { - // console.log('本地存在'); + if(exists) { + // console.log('本地存在 ' + fileId); callback(fileLocalPath); // me.retImage(fileLocalPath, res); } else { diff --git a/node_modules/file_main.js b/node_modules/file_main.js index e3bdae32..8ec22712 100644 --- a/node_modules/file_main.js +++ b/node_modules/file_main.js @@ -109,9 +109,15 @@ var File = { if(has && fileLocalPath) { fs.exists(fileLocalPath, function(exists) { if(exists) { - // console.log('本地存在'); - callback(fileLocalPath); - // me.retImage(fileLocalPath, res); + if (fileLocalPath.indexOf('; charset=utf-8') < 0) { + // console.log('本地存在 ' + fileId); + callback(fileLocalPath); + } + else { + // 存在, 但之前存错了 + console.log('存在, 但之前存错了 ' + fileId); + getImageFromApi(); + } } else { getImageFromApi(); } diff --git a/node_modules/leanote_protocol.js b/node_modules/leanote_protocol.js index 661530ec..bc12458f 100644 --- a/node_modules/leanote_protocol.js +++ b/node_modules/leanote_protocol.js @@ -17,12 +17,14 @@ var leanoteProtocol = { var ret = /fileId=([a-zA-Z0-9]{24})/.exec(url); if (ret && ret[1]) { var fileId = ret[1]; - // console.log(fileId); + // console.log('imageId: ' + fileId); File.getImage(ret[1], function(fileLocalPath) { if(fileLocalPath) { + // console.log(fileId + ' : ' + fileLocalPath); callback({path: fileLocalPath}); } else { + console.log('取不到图片: ' + fileId); callback(); } }); diff --git a/tests/testNeedle.js b/tests/testNeedle.js index 71c186cb..67aeb864 100644 --- a/tests/testNeedle.js +++ b/tests/testNeedle.js @@ -1,8 +1,7 @@ var needle = require('needle'); -var url = 'http://i7.baidu.com/it/u=401605395,2928249378&fm=96&s=5DAEA85217785B88557C00640300B062'; -url = 'http://i7.baidu.com/it/u=401605395,2928249378&fm=96&s=5DAEA85217785B88557C00640300B062'; +var url = 'http://leanote.com/api/file/getImage?fileId=56fe6b53ab64415150001dee'; // url = 'http://leanote.com/images/logo.png'; needle.get(url, function(err, resp) { console.log(resp.statusCode); - console.log(typeof resp.body); + console.log(resp.body); }); \ No newline at end of file