diff --git a/node_modules/file.js b/node_modules/file.js index 94ef26ac..beeaf368 100644 --- a/node_modules/file.js +++ b/node_modules/file.js @@ -205,6 +205,63 @@ var File = { }); }, + // 处理用户图片 + getImage: function(fileId, callback) { + var me = this; + if(!fileId) { + return callback(false); + } + + var Api = require('api'); + + // 访问api, 得到图片 + function getImageFromApi() { + log('从远程得到图片 ' + fileId); + Api.getImage(fileId, function(fileLocalPath, filename) { + if(fileLocalPath) { + log('图片保存到本地成功'); + // 保存到本地数据库中 + me.addImageForce(fileId, fileLocalPath, function(doc) { + if(doc) { + log('保存到本地数据库成功'); + } else { + log('保存到数据库失败'); + } + callback(fileLocalPath); + // return me.retImage(fileLocalPath, res); + }); + } else { + // 远程取不到图片, 是没有网络? 还是远程真的没有了 + // TODO + log('取不远程的图片' + fileId); + callback(false); + // return me.e404(res); + } + }); + } + // 先查看本地是否有该文件 + // has表示本地数据库有记录 + me.getImageLocalPath(fileId, function(has, fileLocalPath) { + // 本地有 + log('re img') + console.log(fileLocalPath); + // console.log(fs.exists(fileLocalPath)); + if(has && fileLocalPath) { + fs.exists(fileLocalPath, function(exists) { + if(exists) { + console.log('本地存在'); + callback(fileLocalPath); + // me.retImage(fileLocalPath, res); + } else { + getImageFromApi(); + } + }); + } else { + getImageFromApi(); + } + }); + }, + // 下载, 复制一份文件 download: function(srcPath, toPath, callback) { var srcIsExists = fs.existsSync(srcPath); diff --git a/node_modules/note.js b/node_modules/note.js index 081a8211..e42a9a1b 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -313,9 +313,15 @@ var Note = { // 得到笔记内容 // noteId是本地Id + inSyncContent: {}, // 正在同步中的 getNoteContent: function(noteId, callback) { var me = this; log('getNoteContent------') + // 如果是正在sync的话, 返回 + if(me.inSyncContent[noteId]) { + return; + } + me.inSyncContent[noteId] = true; me.getNote(noteId, function(note) { if(!Common.isOk(note)) { log('not ok'); @@ -333,10 +339,12 @@ var Note = { // 远程获取 me.getServerNoteIdByNoteId(noteId, function(serverNoteId) { if(!serverNoteId) { + me.inSyncContent[noteId] = false; return callback && callback(false); } Api.getNoteContent(serverNoteId, function(noteContent) { + me.inSyncContent[noteId] = false; // 同步到本地 if(Common.isOk(noteContent)) { me.updateNoteContentForce(noteId, noteContent.Content, function(content) { @@ -351,6 +359,7 @@ var Note = { }); } else { + me.inSyncContent[noteId] = false; log('not need'); callback && callback(note); } @@ -441,13 +450,14 @@ var Note = { callback && callback(false); } else { callback && callback(newDoc); + + // 下载内容, 图片, 附件 + me.syncContentAndImagesAndAttachs(newDoc); } }); }); }, - - // sync <- 时 // 更新笔记, 合并之, 内容要重新获取 // note是服务器传过来的, 需要处理下fix @@ -519,7 +529,6 @@ var Note = { // console.log('evernote'); // console.log(everNote); - // 得到本地笔记本Id Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) { note['NotebookId'] = localNotebookId; @@ -537,6 +546,9 @@ var Note = { } else { log('强制更新...'); callback && callback(note); + + // 下载内容, 图片, 附件 + me.syncContentAndImagesAndAttachs(note); } }); }); @@ -607,8 +619,6 @@ var Note = { }); }); - - }, // 服务器上的数据 @@ -1086,6 +1096,62 @@ var Note = { } }, + // 同步内容, 图片, 附件 + // 异步操作 + syncContentAndImagesAndAttachs: function(note) { + var me = this; + // 内容 + console.log("syncContentAndImagesAndAttachs..................." + note.NoteId); + me.getNoteContent(note.NoteId, function(noteAndContent) { + if(noteAndContent) { + console.log('sync content ' + note.NoteId + ' ok'); + var content = noteAndContent.Content; + Web.contentSynced(note.NoteId, note.Content); + // 图片 + if(content) { + me.syncImages(content); + } + } + }); + + // 附件 + var attachs = note.Attachs || []; + for(var i in attachs) { + var attach = attachs[i]; + me.downloadAttachFromServer(note.NoteId, attach.ServerFileId, attach.FileId); + } + }, + + // 同步图片 + inSyncImage: {}, // + syncImages: function(content) { + var me = this; + if(!content) { + return; + } + console.log('syncImages..................'); + console.log(content); + // 得到图片id + var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=(.{24})\"", 'g'); + // var a = 'abdfileId="xxx" alksdjfasdffileId="life"'; + // var reg = /fileId="(.+?)"/g; + var s; + // console.log(reg); + while(s = reg.exec(content)) { + // console.log(s); + if(s && s.length >= 2) { + var fileId = s[1]; + console.log('sync image: ' + fileId); + if(!me.inSyncImage[fileId]) { + me.inSyncImage[fileId] = true; + File.getImage(fileId, function() { + me.inSyncImage[fileId] = false; + }); + } + } + } + }, + /* 1) sync时判断是否有attach, 如果有, 则异步下载之 2) 前端render note时, 判断是否有未Path的attach, 调用该服务 @@ -1095,7 +1161,7 @@ var Note = { downloaded: {}, // 下载完成的 downloadAttachFromServer: function(noteId, serverFileId, fileId) { var me = this; - if(me.inDownload[serverFileId]) { + if(me.inDownload[serverFileId] || me.downloaded[serverFileId]) { return; } if(!Api) { diff --git a/node_modules/server.js b/node_modules/server.js index 7656c4f3..6e1c4d75 100644 --- a/node_modules/server.js +++ b/node_modules/server.js @@ -125,6 +125,23 @@ var Server = { }); }, + getImage: function(req, res) { + var me = this; + // fileId + var fileId = url.parse(req.url, true).query['fileId']; + if(!fileId) { + return me.e404(res); + } + File.getImage(fileId, function(fileLocalPath) { + if(path) { + return me.retImage(fileLocalPath, res); + } else { + return me.e404(res); + } + }) + }, + + /* // 处理用户图片 getImage: function(req, res) { var me = this; @@ -178,5 +195,6 @@ var Server = { } }); } + */ }; module.exports = Server; \ No newline at end of file diff --git a/node_modules/web.js b/node_modules/web.js index 07c7aa29..80c3b186 100644 --- a/node_modules/web.js +++ b/node_modules/web.js @@ -35,6 +35,14 @@ var Web = { me.Tag.deleteTagsNav(deletes); }, + // 内容同步成功 + contentSynced: function(noteId, content) { + var me = this; + if(noteId) { + me.Note.contentSynced(noteId, content); + } + }, + // 通过attach已同步成功 attachSynced: function(attachs, attach, noteId) { var me = this; diff --git a/public/js/app/note.js b/public/js/app/note.js index 2a46e6e3..bc501038 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -1590,6 +1590,24 @@ Note.star = function(noteId) { }); }; +// 内容已同步成功 +Note.contentSynced = function(noteId, content) { + var me = this; + var note = me.getCurNote(); + if(!note) { + return; + } + if(note.InitSync) { + // 重新render内容 + note.InitSync = false; + note.Content = content; + if(me.curNoteId == noteId) { + // 重新渲染 + Note.changeNote(noteId); + } + } +}; + // 这里速度不慢, 很快 Note.getContextNotebooks = function(notebooks) { var moves = []; diff --git a/test.js b/test.js index 97affa7f..e365df25 100755 --- a/test.js +++ b/test.js @@ -20,10 +20,12 @@ Api.addNotebook({ User.userId = '54bdc65599c37b0da9000002'; User.userId = '54d7620d99c37b030600002c'; +/* User.init(function() { Api.getAttach('54d8c8de99c37b02fa000002', function() { }); }); +*/ /* Note.hasNotes('54bdc65599c37b0da9000005', function(doc) { @@ -68,4 +70,13 @@ while((result = reg.exec(content)) != null) { console.log(result); } console.log("??"); -*/ \ No newline at end of file +*/ + +var a = '