sync attach ok

This commit is contained in:
life
2015-02-09 23:48:47 +08:00
parent b7b5d241f7
commit 22283f22c7
8 changed files with 214 additions and 9 deletions

65
node_modules/api.js generated vendored
View File

@@ -222,8 +222,6 @@ var Api = {
callback(false);
} else {
var typeStr = resp.headers['content-type'];
log(resp.headers);
log(typeStr);
var type = 'png';
if(typeStr) {
var typeArr = typeStr.split('/');
@@ -249,6 +247,69 @@ var Api = {
});
},
// 获取附件
// FileService调用
getAttach: function(serverFileId, callback) {
var me = this;
var url = me.getUrl('file/getAttach', {fileId: serverFileId});
console.log(url);
needle.get(url, function(err, resp) {
if(err) {
return callback && callback(false);
}
// log(resp.body);
/*
{ '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',
'accept-ranges': 'bytes',
'content-disposition': 'attachment; filename="box.js"',
'content-length': '45503',
'content-type': 'application/javascript',
*/
// console.log(resp.headers);
// return;
if(err) {
callback(false);
} else {
// TODO 这里, 要知道文件类型
var typeStr = resp.headers['content-type'];
var contentDisposition = resp.headers['content-disposition'];
var matches = contentDisposition.match(/filename="(.+?)"/);
var filename = matches && matches.length == 2 ? matches[1] : "";
// log(resp.headers);
// log(typeStr);
var type = '';
if(filename) {
type = filename.split('.').pop();
}
if(!filename && typeStr) {
var typeArr = typeStr.split('/');
if(typeStr.length > 1) {
type = typeArr[1];
}
}
var filename = Common.uuid() + '.' + type;
var attachPath = User.getCurUserAttachsPath();
var attachPathAll = attachPath + '/' + filename;
log(attachPathAll);
fs.writeFile(attachPathAll, resp.body, function(err) {
if(err) {
log(err);
log('local save attach failed 本地保存失败');
callback(false);
} else {
callback(true, attachPathAll, filename);
}
});
}
});
},
//------------
// 笔记本操作
//------------