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);
}
});
}
});
},
//------------
// 笔记本操作
//------------

3
node_modules/evt.js generated vendored
View File

@@ -9,6 +9,9 @@ var Evt = {
getImageLocalUrl: function(fileId) {
return this.localUrl + '/api/file/getImage?fileId=' + fileId;
},
getAttachLocalUrl: function(fileId) {
return this.localUrl + '/api/file/getAttach?fileId=' + fileId;
},
// 项目绝对地址
getBasePath: function() {
return process.cwd();

29
node_modules/file.js generated vendored
View File

@@ -205,6 +205,35 @@ var File = {
});
},
// 下载, 复制一份文件
download: function(srcPath, toPath, callback) {
var srcIsExists = fs.existsSync(srcPath);
if(!srcIsExists) {
return callback(false, 'File Not Exists');
}
console.log(srcPath);
console.log(toPath);
var toIsExists = fs.existsSync(toPath);
function cp() {
Common.copyFile(srcPath, toPath, function(ok) {
callback(ok);
});
}
if(toIsExists) {
fs.unlink(toPath, function(error) {
if(!error) {
cp();
} else {
callback(false, 'The Target File Cannot Overwrite');
}
});
} else {
cp();
}
},
// 附件操作
addAttach: function(filePaths, noteId, callback) {
if(!noteId || !filePaths) {

62
node_modules/note.js generated vendored
View File

@@ -1086,6 +1086,68 @@ var Note = {
}
},
/*
1) sync时判断是否有attach, 如果有, 则异步下载之
2) 前端render note时, 判断是否有未Path的attach, 调用该服务
从服务器端下载文件, 并通过到前端已下载完成
*/
inDownload: {}, // 正在下载的文件 fileId => true
downloaded: {}, // 下载完成的
downloadAttachFromServer: function(noteId, serverFileId, fileId) {
var me = this;
if(me.inDownload[serverFileId]) {
return;
}
if(!Api) {
Api = require('api');
}
me.inDownload[serverFileId] = true;
Api.getAttach(serverFileId, function(ok, toPath, filename) {
me.inDownload[serverFileId] = false;
if(ok) {
me.downloaded[serverFileId] = fileId;
// 更新serverFileId与fileId的映射, 修改的是note
me.syncAttach(noteId, serverFileId, fileId, toPath, filename, function(ok, attachs, attach) {
if(ok) {
// 通知web
Web.attachSynced(attachs, attach, noteId);
}
});
} else {
// 下次再下载 ?
// 或者放到一个队列中 ?
// TODO
}
});
},
// 同步附件, 更新serverFileId
syncAttach: function(noteId, serverFileId, fileId, path, filename, callback) {
var me = this;
me.getNote(noteId, function(note) {
if(!note) {
callback(false);
}
var attachs = note.Attachs;
for(var i in attachs) {
var attach = attachs[i];
if(attach.FileId == fileId) {
attach.ServerFileId = serverFileId;
attach.Path = path;
// attach.Title = filename;
// attach.Filename = filename;
Notes.update({_id: note._id}, {$set: {Attachs: attachs}}, function() {
callback(true, attachs, attach);
});
break;
}
}
callback(false);
});
},
// 根据标签得到笔记数量
countNoteByTag: function(title, callback) {
var userId = User.getCurActiveUserId();

9
node_modules/web.js generated vendored
View File

@@ -11,10 +11,11 @@ var Web = {
Tag: null,
// 注入前端变量
set: function(notebook, note, tag) {
set: function(notebook, note, attach, tag) {
var me = this;
me.Notebook = notebook;
me.Note = note;
me.Attach = attach;
me.Tag = tag;
},
@@ -32,6 +33,12 @@ var Web = {
me.Tag.addTagsNav(adds);
var deletes = tagSyncInfo.deletes;
me.Tag.deleteTagsNav(deletes);
},
// 通过attach已同步成功
attachSynced: function(attachs, attach, noteId) {
var me = this;
me.Attach.attachSynced(attachs, attach, noteId);
}
};
module.exports = Web;