attach 添加/删除 attach

This commit is contained in:
life
2015-01-30 11:31:42 +08:00
parent 4a78441cad
commit c309427fed
3 changed files with 74 additions and 21 deletions

31
node_modules/file.js generated vendored
View File

@@ -193,14 +193,14 @@ var File = {
var ext = names[names.length-1];
var rename = Common.uuid() + "." + ext;
var distPath = User.getCurUserAttachsAppPath() + '/' + rename;
var distPath = User.getCurUserAttachsPath() + '/' + rename;
var readable = fs.createReadStream(filePath);
// 创建写入流
var writable = fs.createWriteStream(distPath);
// 通过管道来传输流
readable.pipe(writable);
var attach = {
FileId: Common.objectId,
FileId: Common.objectId(),
ServerFileId: '',
Path: distPath,
NoteId: noteId,
@@ -209,7 +209,7 @@ var File = {
Title: name,
Type: ext,
Size: fileStat && fileStat.size,
IsDirty: true, // 本地是新添加的
IsDirty: true, // 本地是新添加的, ServerFileId = 0
CreatedTime: new Date()
};
Attachs.insert(attach);
@@ -217,6 +217,31 @@ var File = {
}
callback && callback(targets);
},
// 删除不存在的attachs
deleteNotExistsAttach: function(noteId, attachs) {
var me = this;
console.log('--');
Attachs.find({NoteId: noteId}, function(err, everAttachs) {
if(err) {
return;
}
var nowMap = {};
for(var i in attachs) {
nowMap[attachs[i].FileId] = attachs[i];
}
var fileBasePath = User.getCurUserAttachsPath();
for(var i in everAttachs) {
var attach = everAttachs[i];
if(!nowMap[attach.FileId]) { // 如果不在, 则删除之
Attachs.remove({FileId: attach.FileId});
// 删除源文件, 别删错了啊
if(attach.Path.indexOf(fileBasePath) >= 0) {
fs.unlink(attach.Path);
}
}
}
});
}
};

9
node_modules/note.js generated vendored
View File

@@ -105,7 +105,14 @@ var Note = {
}
},
// 更新笔记的附件
updateAttach: function(noteId, attachs) {
var me = this;
Notes.update({NoteId: noteId}, {$set: {Attachs: attachs, IsDirty: true}});
// Files, 删除修改了的
File.deleteNotExistsAttach(noteId, attachs);
},
// 获取笔记列表
getNotes: function(notebookId, callback) {