attach conflict copy ok

This commit is contained in:
life
2015-02-01 21:30:28 +08:00
parent 647ca66fa5
commit 1134336f36
3 changed files with 112 additions and 11 deletions

61
node_modules/common.js generated vendored
View File

@@ -34,6 +34,65 @@ var Common = {
}
return false;
},
// 复制文件
copyFile: function(src, dist, callback) {
if(!src || !dist) {
return callback && callback(false);
}
var readStream = fs.createReadStream(src);
var writeStream = fs.createWriteStream(dist);
readStream.pipe(writeStream);
readStream.on('end', function () {
callback && callback(true);
});
readStream.on('error', function () {
callback && callback(false);
});
},
// 拆分filePath的各个部分
splitFile: function(fullFilePath) {
var ret = {
path: "", // a/b
name: "", // c.js
nameNotExt: "", // a
ext: "", // js
getFullPath: function() {
var me = this;
if(me.path) {
if(me.ext) {
return me.path + '/' + me.nameNotExt + '.' + me.ext;
} else {
return me.path + '/' + me.nameNotExt;
}
} else {
if(me.ext) {
return me.nameNotExt + '.' + me.ext;
} else {
return me.nameNotExt;
}
}
}
}
if(!fullFilePath) {
return ret;
}
var strs = fullFilePath.split('/');
if(strs.length == 1) {
ret.name = strs;
} else {
ret.name = strs[strs.length - 1];
strs.pop();
ret.path = strs.join('/');
}
var names = ret.name.split('.');
if(names.length > 1) {
ret.ext = names[names.length - 1];
names.pop();
ret.nameNotExt = names.join('.');
} else {
ret.nameNotExt = ret.name;
}
return ret;
}
};
module.exports = Common;

60
node_modules/note.js generated vendored
View File

@@ -555,9 +555,15 @@ var Note = {
// 为冲突更新, note已有有NoteId, ServerNoteId, 但NotebookId是服务器端的
updateNoteForceForConflict: function(note, callback) {
var me = this;
note.NoteId = note.ServerNoteId;
me.updateNoteForce(note, callback);
return;
note.IsDirty = false;
note.InitSync = true;
note.LocalIsNew = false;
// 文件操作
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
note['NotebookId'] = localNotebookId;
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
@@ -589,17 +595,53 @@ var Note = {
note.ConflictTime = new Date(); // 发生冲突时间
note.ConflictFixed = false; // 冲突未解决
note.IsDirty = true;
note.LocalIsNew = true;
note.LocalIsNew = true; // 新增加的
note.InitSync = false; // 都是本地的, 相当于新建的笔记
Notes.insert(note, function(err, newNote) {
if(err) {
callback(false);
} else {
callback(newNote);
// 只复制有path的
var attachs = note.Attachs || [];
var newAttachs = [];
console.log('不会吧.............')
console.log(attachs);
async.eachSeries(attachs, function(attach, cb) {
if(!attach.Path) {
return cb();
}
// 新路径
var filePathAttr = Common.splitFile(attach.Path);
filePathAttr.nameNotExt += '_cp_' + attach.FileId; // 另一个
var newPath = filePathAttr.getFullPath();
console.log('复制文件');
console.log(attach);
// 复制之
// try {
Common.copyFile(attach.Path, newPath, function(ret) {
if(ret) {
attach.FileId = Common.objectId();
attach.IsDirty = true;
attach.Path = newPath;
delete attach['ServerFileId'];
newAttachs.push(attach);
}
cb();
});
/*
} catch(e) {
cb();
}
*/
}, function() {
note.Attachs = newAttachs;
console.log('conflict 复制后的');
console.log(note.Attachs);
Notes.insert(note, function(err, newNote) {
if(err) {
callback(false);
} else {
callback(newNote);
}
});
});
});
},
@@ -619,9 +661,9 @@ var Note = {
log(conflictNotes);
// 这里为什么要同步? 因为fixConflicts后要进行send changes, 这些有冲突的不能发送changes
if(conflictNotes) {
async.eachSeries(conflictNotes, function(note, cb) { // note是服务器上最新的
async.eachSeries(conflictNotes, function(note, cb) { // note是服务器上最新的, note.NoteId, ServerNoteId已转换
var noteId = note.NoteId; // 本地noteId
// 复制一份
// 复制一份, 本地的复制一份, 然后服务器上的替换本地的
me.copyNoteForConfict(noteId, function(newNote) {
if(newNote) {
// 更新之前的

View File

@@ -18,7 +18,7 @@ Api.addNotebook({
*/
// Api.uploadImage();
User.userId = '54bdc65599c37b0da9000002';
Note.getNoteByServerNoteId('54ccc53d99c37bf812000001', function(doc) {
Note.getNoteByServerNoteId('54ce29b399c37b1d5c000479', function(doc) {
console.log(doc);
});
/*