Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #20 from yor-mozilla-com/fix995037
Browse files Browse the repository at this point in the history
Bug 995037 - Migration logic to convert Notes (master) data format to No...
  • Loading branch information
rvandermeulen committed Apr 18, 2014
2 parents 77e9220 + 760d720 commit ced0f7e
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 65 deletions.
81 changes: 66 additions & 15 deletions js/common.js
Expand Up @@ -273,6 +273,10 @@ var App = new function() {
return user;
};

this.updateMigrationStatus = function(data, c, e) {
user.set(data, c, e);
};

this.updateUserData = function(data, c, e) {
user.set(data, c, e);
Settings.update();
Expand Down Expand Up @@ -337,13 +341,13 @@ var App = new function() {
return notes;
};

this.addQueue = function addQueue(type, obj) {
this.addQueue = function addQueue(type, obj, cbSuccess) {
new Models.Queue({
rel : type,
rel_id : obj.data_id || obj.id,
rel_guid : obj.data_guid || obj.guid,
expunge : obj.expunge || false
}).set(onAddQueue);
}).set(cbSuccess || onAddQueue);
};

this.getQueues = function getQueues(cbSuccess, cbError) {
Expand Down Expand Up @@ -443,6 +447,7 @@ var App = new function() {
key += String("0123456789abcdef".substr((resource.data.bodyHash[i] >> 4) & 0x0F,1)) + "0123456789abcdef".substr(resource.data.bodyHash[i] & 0x0F,1);
}
App.resizeImage(resource.data.body, resource.width, resource.height, resource.mime, key);
App.processImageResizeQueue();
};

this.noteResourceNotLoaded = function(resource) {
Expand All @@ -458,9 +463,6 @@ var App = new function() {
"type" : type,
"hash" : hash
});
if (resizeInProgress == 0) {
self.processImageResizeQueue();
}
return null;
} else {
var blobURL = window.URL.createObjectURL(ArrayBufferHelper.getBlob(data, type));
Expand Down Expand Up @@ -891,12 +893,12 @@ var App = new function() {

this.show = function(note, notebook) {
elWarning.style.display = "none";
el.classList.add(CLASS_WHEN_READONLY);
noteContent = "Loading...";

var noteContent = note.getContent(true, true),
noteName = note.getName();
if (noteContent == null) {
el.classList.add(CLASS_WHEN_READONLY);
noteContent = "Loading...";
} else {
if (noteContent != null) {
if (!note.isMissingResourceData()) {
el.classList.remove(CLASS_WHEN_READONLY);
elWarning.style.display = "none";
Expand Down Expand Up @@ -932,11 +934,20 @@ var App = new function() {
};

this.updateContent = function(note) {
currentNote.updateContent(note.content);
if (note.guid === currentNote.getGuid()) {
DB.getNotes({"guid": note.guid}, function(notes) {
self.show(notes[0], currentNotebook);
});
if (typeof note.guid !== "undefined") {
currentNote.updateContent(note.content);
if (note.guid === currentNote.getGuid()) {
DB.getNoteByIndex("guid", note.guid, function(note) {
self.show(note, currentNotebook);
});
}
} else {
currentNote.updateContent(note.data_content);
if (note.getId() === currentNote.getId()) {
DB.getNoteByKey(note.getId(), function(note) {
self.show(note, currentNotebook);
});
}
}
};

Expand Down Expand Up @@ -1482,7 +1493,12 @@ var App = new function() {
function getNoteElement(note) {
var el = document.createElement("li");

var content = note.getContent(true, false);
var content;
if (typeof note.data_text !== "undefined" && note.data_text != null) {
content = note.data_text;
} else {
content = note.getContent(true, false);
}
var contentBody = content.match(/<body[^>]*>([\w\W]*)<\/body>/);
if (contentBody && contentBody.length > 1) {
content = contentBody[1];
Expand Down Expand Up @@ -2079,12 +2095,47 @@ function $(s) { return document.getElementById(s); }
function $$(s) { return document.querySelector(s); }
function html(el, s) { el.innerHTML = (s || "").replace(/</g, '&lt;'); }

function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
62
: nChr === 47 ?
63
:
0;
}

var ArrayBufferHelper = {
getBlob : function(arraybuffer, type) {
return new Blob([arraybuffer], {type: type});
},

b64ToArrayBuffer : function(sBase64, nBlocksSize) {
var
sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);

for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;

}
}
return taBytes;
}
};


window.onload = function() {
navigator.mozL10n.ready(App.init);
$('search').addEventListener('submit', function(e) {
Expand Down
138 changes: 136 additions & 2 deletions js/evernote.js
Expand Up @@ -39,6 +39,9 @@ var Evernote = new function() {
lastChunkUSN = 0,
lastUSN = 0,

migratedCount = 0,
totalToMigrate = 0,

queueList = {
notebooks : [],
notes : []
Expand Down Expand Up @@ -278,8 +281,11 @@ var Evernote = new function() {
self.getSyncChunk(last_update_count, syncMaxEntries, false, self.processSyncChunk);
};
this.startFullSync = function() {
firstUSN = 0;
self.getSyncChunk(0, syncMaxEntries, true, self.processSyncChunk);
App.startSync();
self.migrateAllData(function() {
firstUSN = 0;
self.getSyncChunk(0, syncMaxEntries, true, self.processSyncChunk);
});
};

this.getSyncChunk = function(usn, max, full, c) {
Expand Down Expand Up @@ -941,6 +947,10 @@ var Evernote = new function() {
};

this.enml2html = function(note, loadResources) {
if (typeof note.data_text !== "undefined" && note.data_text != null && loadResources) {
this.migrateNoteData(note, this.noteContentLoaded);
return null;
}
if (note.data_content == null && loadResources) {
// Delay fetching of note contents
noteStore.getNote(oauth_token, note.getGuid(), true, false, false, false, this.noteContentLoaded);
Expand Down Expand Up @@ -986,6 +996,7 @@ var Evernote = new function() {
// }
}
}
App.processImageResizeQueue();
return enml.HTMLOfENML(note.getContent(false, false), hashMap);
};

Expand All @@ -1005,6 +1016,129 @@ var Evernote = new function() {
};
};

this.migrationComplete = function(callback) {
App.updateMigrationStatus({
migration_complete : true
}, callback);
};

this.migrationInProgress = function() {
return App.getUser().migration_complete != true;
};

this.migrateAllData = function(callback) {
if (self.migrationInProgress()) {
DB.getNotebooks({}, function(notebooks) {
self.migrateNotebooks(notebooks, callback);
});
} else {
callback && callback();
}
};

this.migrateNotebooks = function(notebooks, callback) {
if (notebooks.length > 0) {
var notebook = notebooks.pop();
App.addQueue("Notebook", notebook, function() {
self.migrateNotebooks(notebooks, callback);
});
} else {
self.migrateAllNotes(callback);
}
};

this.migrateAllNotes = function(callback) {
if (self.migrationInProgress()) {
DB.getNotes({"content":undefined}, function(notes) {
totalToMigrate = notes.length;
self.migrateNotes(notes, callback);
});
} else {
callback && callback();
}
};

this.migrateNotes = function(notes, callback) {
if (notes.length > 0) {
var n = notes.pop();
migratedCount++;
self.updateProgressBar((migratedCount*100)/totalToMigrate);
self.migrateNoteData(n, function() {
self.migrateNotes(notes, callback);
});
} else {
self.migrationComplete(callback);
}
};

this.migrateNoteData = function(note, callback) {
// Get resource objects and
// 1. convert to ArrayBuffer and compute hash
// 2. create <img> element with hash only
// 3. Replace images from obj.text with new <img>
DB.getNoteResources({"noteId":note.data_id}, function(resources) {
var content = note.data_text.replace("\n", "<br/>");
var newResources = [];
for (var i = 0, len = resources.length; i < len; i++) {
var noteResource = self.createNoteResource(resources[i]);
newResources.push(noteResource);
content = content.concat(self.createImageTag(noteResource));
}

var newObj = {
id: note.data_id,
title: note.data_title || (content || "").split(/<br[^>]*>/i)[0],
content: content,
text: null,
resources: newResources,
country: note.data_country,
city: note.data_city,
date_created: note.data_date_created,
date_updated: note.data_date_updated,
trashed: note.data_trashed,
active: true,
notebook_id: note.data_notebook_id,
metadata: note.data_metadata
};

note.set(newObj, function(note) {
App.addQueue("Note", note, function() {
callback && callback(note);
});
});
});
};

this.createImageTag = function(resource) {
return '<br/><img hash="'+resource.data.bodyHash+'" type="'+resource.mime+'">';
};

this.createNoteResource = function(image) {
var regex = /;base64,/;
var res = regex.exec(image.getSrc());
var mimetype = image.getSrc().substring(5, res.index);
var data = image.getSrc().substring(res.index+8);

var mimeRegex = /\//;
res = mimeRegex.exec(mimetype);
var filetype = mimetype.substring(res.index+1);

var array = ArrayBufferHelper.b64ToArrayBuffer(data);
var filename = image.getName()+'-'+image.getId()+'.'+filetype;
return {
noteGuid: null,
mime: mimetype,
data: {
body: array.buffer,
bodyHash: SparkMD5.ArrayBuffer.hash(array.buffer),
size: array.length
},
attributes: new ResourceAttributes({
fileName: filename
})
};
};

function initNoteStore() {
if (!noteStore) {
noteStoreTransport = new Thrift.BinaryHttpTransport(note_store_url);
Expand Down

0 comments on commit ced0f7e

Please sign in to comment.