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 ext = names[names.length-1];
var rename = Common.uuid() + "." + ext; var rename = Common.uuid() + "." + ext;
var distPath = User.getCurUserAttachsAppPath() + '/' + rename; var distPath = User.getCurUserAttachsPath() + '/' + rename;
var readable = fs.createReadStream(filePath); var readable = fs.createReadStream(filePath);
// 创建写入流 // 创建写入流
var writable = fs.createWriteStream(distPath); var writable = fs.createWriteStream(distPath);
// 通过管道来传输流 // 通过管道来传输流
readable.pipe(writable); readable.pipe(writable);
var attach = { var attach = {
FileId: Common.objectId, FileId: Common.objectId(),
ServerFileId: '', ServerFileId: '',
Path: distPath, Path: distPath,
NoteId: noteId, NoteId: noteId,
@@ -209,7 +209,7 @@ var File = {
Title: name, Title: name,
Type: ext, Type: ext,
Size: fileStat && fileStat.size, Size: fileStat && fileStat.size,
IsDirty: true, // 本地是新添加的 IsDirty: true, // 本地是新添加的, ServerFileId = 0
CreatedTime: new Date() CreatedTime: new Date()
}; };
Attachs.insert(attach); Attachs.insert(attach);
@@ -217,6 +217,31 @@ var File = {
} }
callback && callback(targets); 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) { getNotes: function(notebookId, callback) {

View File

@@ -1598,15 +1598,9 @@ var Attach = {
var attachId = $(this).closest('li').data("id"); var attachId = $(this).closest('li').data("id");
var t = this; var t = this;
if(confirm("Are you sure to delete it ?")) { if(confirm("Are you sure to delete it ?")) {
$(t).button("loading"); // $(t).button("loading");
ajaxPost("/attach/deleteAttach", {attachId: attachId}, function(re) { self.deleteAttach(attachId);
$(t).button("reset"); // $(t).button("reset");
if(reIsOk(re)) {
self.deleteAttach(attachId);
} else {
alert(re.Msg);
}
});
} }
}); });
// 下载 // 下载
@@ -1679,8 +1673,10 @@ var Attach = {
renderNoteAttachNum: function(noteId, needHide) { renderNoteAttachNum: function(noteId, needHide) {
var self = this; var self = this;
var note = Note.getNote(noteId); var note = Note.getNote(noteId);
if(note.AttachNum) { var attachs = note.Attachs;
self.attachNumO.html("(" + note.AttachNum + ")").show(); var attachNum = attachs ? attachs.length : 0;
if(attachNum) {
self.attachNumO.html("(" + attachNum + ")").show();
self.downloadAllBtnO.show(); self.downloadAllBtnO.show();
self.linkAllBtnO.show(); self.linkAllBtnO.show();
} else { } else {
@@ -1708,6 +1704,7 @@ var Attach = {
*/ */
var html = ""; var html = "";
var attachNum = attachs.length; var attachNum = attachs.length;
console.log(attachs);
for(var i = 0; i < attachNum; ++i) { for(var i = 0; i < attachNum; ++i) {
var each = attachs[i]; var each = attachs[i];
html += '<li class="clearfix" data-id="' + each.FileId + '">' + html += '<li class="clearfix" data-id="' + each.FileId + '">' +
@@ -1732,14 +1729,22 @@ var Attach = {
// 渲染noteId的附件 // 渲染noteId的附件
// 当点击"附件"时加载, // 当点击"附件"时加载,
// TODO 判断是否已loaded // TODO 判断是否已loaded
// note添加一个Attachs
renderAttachs: function(noteId) { renderAttachs: function(noteId) {
var self = this; var self = this;
var note = Note.getNote(noteId);
note.Attachs = note.Attachs || [];
self.loadedNoteAttachs[noteId] = note.Attachs; // 一个对象
self._renderAttachs(note.Attachs);
return;
/*
if(self.loadedNoteAttachs[noteId]) { if(self.loadedNoteAttachs[noteId]) {
self._renderAttachs(self.loadedNoteAttachs[noteId]); self._renderAttachs(self.loadedNoteAttachs[noteId]);
return; return;
} }
// 显示loading // 显示loading
self.attachListO.html('<li class="loading"><img src="/images/loading-24.gif"/></li>'); self.attachListO.html('<li class="loading"><img src="public/images/loading-24.gif"/></li>');
// ajax获取noteAttachs // ajax获取noteAttachs
ajaxGet("/attach/getAttachs", {noteId: noteId}, function(ret) { ajaxGet("/attach/getAttachs", {noteId: noteId}, function(ret) {
var list = []; var list = [];
@@ -1753,20 +1758,27 @@ var Attach = {
self.loadedNoteAttachs[noteId] = list; self.loadedNoteAttachs[noteId] = list;
self._renderAttachs(list); self._renderAttachs(list);
}); });
*/
}, },
// 添加附件, attachment_upload上传调用 // 添加附件, attachment_upload上传调用
addAttach: function(attachInfo) { addAttach: function(attachInfo) {
var self = this; var self = this;
if(!self.loadedNoteAttachs[attachInfo.NoteId]) {
self.loadedNoteAttachs[attachInfo.NoteId] = [];
}
self.loadedNoteAttachs[attachInfo.NoteId].push(attachInfo); self.loadedNoteAttachs[attachInfo.NoteId].push(attachInfo);
self.renderAttachs(attachInfo.NoteId); self.renderAttachs(attachInfo.NoteId);
// TOOD 更新Note表
self.updateAttachToDB(attachInfo.NoteId);
}, },
addAttachs: function(attachInfos) { addAttachs: function(attachInfos) {
var self = this;
var noteId = '';
for(var i in attachInfos) { for(var i in attachInfos) {
this.addAttach(attachInfos[i]); var attachInfo = attachInfos[i];
noteId = attachInfo.NoteId;
self.loadedNoteAttachs[noteId].push(attachInfo);
} }
self.renderAttachs(attachInfo.NoteId);
// TOOD 更新Note表
self.updateAttachToDB(noteId);
}, },
// 删除 // 删除
deleteAttach: function(attachId) { deleteAttach: function(attachId) {
@@ -1774,7 +1786,7 @@ var Attach = {
var noteId = Note.curNoteId; var noteId = Note.curNoteId;
var attachs = self.loadedNoteAttachs[noteId]; var attachs = self.loadedNoteAttachs[noteId];
for(var i = 0; i < attachs.length; ++i) { for(var i = 0; i < attachs.length; ++i) {
if(attachs[i].AttachId == attachId) { if(attachs[i].FileId == attachId) {
// 删除之, 并render之 // 删除之, 并render之
attachs.splice(i, 1); attachs.splice(i, 1);
break; break;
@@ -1782,6 +1794,15 @@ var Attach = {
} }
// self.loadedNoteAttachs[noteId] = attachs; // self.loadedNoteAttachs[noteId] = attachs;
self.renderAttachs(noteId); self.renderAttachs(noteId);
// TODO 更新
self.updateAttachToDB(noteId);
},
// 更新到Note表中
updateAttachToDB: function(noteId) {
var self = this;
var attachs = self.loadedNoteAttachs[noteId]
NoteService.updateAttach(noteId, attachs);
}, },
// 下载 // 下载