syncContent, images, attach [ok]

This commit is contained in:
life
2015-02-10 00:59:53 +08:00
parent 22283f22c7
commit 5d9fb0c1eb
6 changed files with 185 additions and 7 deletions

57
node_modules/file.js generated vendored
View File

@@ -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);

78
node_modules/note.js generated vendored
View File

@@ -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) {

18
node_modules/server.js generated vendored
View File

@@ -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;

8
node_modules/web.js generated vendored
View File

@@ -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;

View File

@@ -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 = [];

13
test.js
View File

@@ -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("??");
*/
*/
var a = '<img src="http://127.0.0.1:8008/api/file/getImage?fileId=34232323234iji3"';
// var reg = /fileId=(.+?)"/g;
var reg = new RegExp("http://127.0.0.1:8008/api/file/getImage\\?fileId=(.{10})", 'g');
console.log(reg);
while(s = reg.exec(a)) {
console.log(s);
}