diff --git a/node_modules/api.js b/node_modules/api.js index 936952b9..e62e9559 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -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); + } + }); + } + }); + }, + //------------ // 笔记本操作 //------------ diff --git a/node_modules/evt.js b/node_modules/evt.js index 06a46f65..b54cf7cf 100644 --- a/node_modules/evt.js +++ b/node_modules/evt.js @@ -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(); diff --git a/node_modules/file.js b/node_modules/file.js index b3513e54..94ef26ac 100644 --- a/node_modules/file.js +++ b/node_modules/file.js @@ -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) { diff --git a/node_modules/note.js b/node_modules/note.js index 16929e89..081a8211 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -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(); diff --git a/node_modules/web.js b/node_modules/web.js index ad7404d0..07c7aa29 100644 --- a/node_modules/web.js +++ b/node_modules/web.js @@ -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; diff --git a/public/js/app/note.js b/public/js/app/note.js index c7a7b0da..2a46e6e3 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -1725,6 +1725,9 @@ Note.initContextmenu = function() { var Attach = { loadedNoteAttachs: {}, // noteId => [attch1Info, attach2Info...] // 按笔记 attachsMap: {}, // attachId => attachInfo + getAttach: function(attachId) { + return this.attachsMap[attachId]; + }, init: function() { var self = this; var me = this; @@ -1748,10 +1751,12 @@ var Attach = { } }); // 下载 + var curAttachId = ''; self.attachListO.on("click", ".download-attach", function(e) { e.stopPropagation(); var $li = $(this).closest('li'); var attachId = $li.data("id"); + curAttachId = attachId; $('#downloadFileInput').attr('nwsaveas', $li.find('.attach-title').text()).click(); // window.open(UrlPrefix + "/attach/download?attachId=" + attachId); @@ -1759,7 +1764,6 @@ var Attach = { }); // 下载全部 self.downloadAllBtnO.click(function() { - // window.open(UrlPrefix + "/attach/downloadAll?noteId=" + Note.curNoteId); // location.href = "/attach/downloadAll?noteId=" + Note.curNoteId; }); @@ -1767,7 +1771,18 @@ var Attach = { $('#downloadFileInput').change(function(e) { var value = $(this).val(); $(this).val(''); - alert(value); + var curAttach = me.getAttach(curAttachId); + if(curAttach) { + FileService.download(curAttach.Path, value, function(ok, msg) { + if(!ok) { + alert(msg || "error"); + } else { + // TODO 提示下载成功 + } + }); + } else { + alert('error'); + } }); // make link @@ -1775,7 +1790,7 @@ var Attach = { e.stopPropagation(); var attachId = $(this).closest('li').data("id"); var attach = self.attachsMap[attachId]; - var src = UrlPrefix + "/attach/download?attachId=" + attachId; + var src = EvtService.getAttachLocalUrl(attachId); // + "/attach/download?attachId=" + attachId; if(LEA.isMarkdownEditor() && MD) { MD.insertLink(src, attach.Title); } else { @@ -1871,16 +1886,20 @@ var Attach = { var each = attachs[i]; var path = each.Path; // 本地是否有, 没有, 是否是在显示的时候才去从服务器上抓? 不 + var disabled = ''; if(path) { var d = ''; } else { - d = 'no' + d = '...' + disabled = 'disabled'; + // 通过后端去下载 + NoteService.downloadAttachFromServer(Note.curNoteId, each.ServerFileId, each.FileId); } html += '