img save as & notification

This commit is contained in:
life
2015-03-20 00:35:19 +08:00
parent c27e92d681
commit 01f59cecac
6 changed files with 143 additions and 9 deletions

70
node_modules/file.js generated vendored
View File

@@ -1,5 +1,6 @@
var db = require('db');
var fs = require('fs');
var needle = require('needle');
var path = require('path');
var Evt = require('evt');
var User = require('user');
@@ -310,12 +311,12 @@ var File = {
if(!srcIsExists) {
return callback(false, 'File Not Exists');
}
console.log(srcPath);
console.log(toPath);
// console.log(srcPath);
// console.log(toPath);
var toIsExists = fs.existsSync(toPath);
function cp() {
Common.copyFile(srcPath, toPath, function(ok) {
callback(ok);
callback && callback(ok);
});
}
if(toIsExists) {
@@ -323,7 +324,7 @@ var File = {
if(!error) {
cp();
} else {
callback(false, 'The Target File Cannot Overwrite');
callback && callback(false, 'The Target File Cannot Overwrite');
}
});
} else {
@@ -331,8 +332,6 @@ var File = {
}
},
// 附件操作
addAttach: function(filePaths, noteId, callback) {
if(!noteId || !filePaths) {
@@ -400,7 +399,64 @@ var File = {
}
}
});
}
},
// 下载图片, 本地的, 或外站的
downloadImg: function(src, callback) {
var me = this;
// 本地的
if(src.indexOf('http://127.0.0.1') != -1) {
var ret = /fileId=([a-zA-Z0-9]{24})/.exec(src);
if(ret && ret.length == 2) {
var fileId = ret[1];
me.getImage(fileId, function(filePath) {
callback(filePath);
});
} else {
callback();
}
} else {
// 远程的图片
needle.get(src, function(err, resp) {
// console.log(resp);
/*
{ 'accept-ranges': 'bytes',
'content-disposition': 'inline; filename="logo.png"',
'content-length': '8583',
'content-type': 'image/png',
date: 'Mon, 19 Jan 2015 15:01:47 GMT',
*/
// log(resp.headers);
if(err || !resp || resp.statusCode == 404) {
callback(false);
} else {
// 当图片没有时候还是执行这一步
var typeStr = resp.headers['content-type'];
var type = 'png';
if(typeStr) {
var typeArr = typeStr.split('/');
if(typeStr.length > 1) {
type = typeArr[1];
}
}
var filename = Common.uuid() + '.' + type;
var imagePath = User.getCurUserImagesPath();
var imagePathAll = imagePath + '/' + filename;
fs.writeFile(imagePathAll, resp.body, function(err) {
if(err) {
callback(false);
} else {
callback(imagePathAll);
}
});
}
});
}
},
};
module.exports = File;