From d08c9522c0dd414a6fed1671064701160d233603 Mon Sep 17 00:00:00 2001 From: Max Wu Date: Sat, 3 Mar 2018 16:25:30 +0800 Subject: Update to migrate note url in the history of browser storage and cookie Signed-off-by: Max Wu --- public/js/history.js | 15 +++++++++++++++ public/js/utils.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 public/js/utils.js (limited to 'public') diff --git a/public/js/history.js b/public/js/history.js index e14b80d8..e7d289fb 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -3,6 +3,12 @@ import store from 'store' import S from 'string' +import LZString from 'lz-string' + +import { + checkNoteIdValid, + encodeNoteId +} from './utils' import { checkIfAuth @@ -291,6 +297,15 @@ function parseToHistory (list, notehistory, callback) { else if (!list || !notehistory) callback(list, notehistory) else if (notehistory && notehistory.length > 0) { for (let i = 0; i < notehistory.length; i++) { + // migrate LZString encoded id to base64url encoded id + try { + let id = LZString.decompressFromBase64(notehistory[i].id) + if (id && checkNoteIdValid(id)) { + notehistory[i].id = encodeNoteId(id) + } + } catch (err) { + // na + } // parse time to timestamp and fromNow const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')) notehistory[i].timestamp = timestamp.valueOf() diff --git a/public/js/utils.js b/public/js/utils.js new file mode 100644 index 00000000..91e7f133 --- /dev/null +++ b/public/js/utils.js @@ -0,0 +1,32 @@ +import base64url from 'base64url' + +let uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i + +export function checkNoteIdValid (id) { + let result = id.match(uuidRegex) + if (result && result.length === 1) { + return true + } else { + return false + } +} + +export function encodeNoteId (id) { + // remove dashes in UUID and encode in url-safe base64 + let str = id.replace(/-/g, '') + let hexStr = Buffer.from(str, 'hex') + return base64url.encode(hexStr) +} + +export function decodeNoteId (encodedId) { + // decode from url-safe base64 + let id = base64url.toBuffer(encodedId).toString('hex') + // add dashes between the UUID string parts + let idParts = [] + idParts.push(id.substr(0, 8)) + idParts.push(id.substr(8, 4)) + idParts.push(id.substr(12, 4)) + idParts.push(id.substr(16, 4)) + idParts.push(id.substr(20, 12)) + return idParts.join('-') +} -- cgit v1.2.3 From dfd833dbe2c21ea6ccdc185b717c59894950816d Mon Sep 17 00:00:00 2001 From: Max Wu Date: Sun, 11 Mar 2018 02:55:54 +0800 Subject: Update to show log on migrate LZString type note url in history Signed-off-by: Max Wu --- public/js/history.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public') diff --git a/public/js/history.js b/public/js/history.js index e7d289fb..a6575360 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -304,7 +304,7 @@ function parseToHistory (list, notehistory, callback) { notehistory[i].id = encodeNoteId(id) } } catch (err) { - // na + logger.error(err) } // parse time to timestamp and fromNow const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')) -- cgit v1.2.3 From 8bfe51940f2eff035394b7713cbbce5b9b446842 Mon Sep 17 00:00:00 2001 From: Max Wu Date: Sun, 11 Mar 2018 03:00:36 +0800 Subject: Fix typo Signed-off-by: Max Wu --- public/js/history.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public') diff --git a/public/js/history.js b/public/js/history.js index a6575360..71322818 100644 --- a/public/js/history.js +++ b/public/js/history.js @@ -304,7 +304,7 @@ function parseToHistory (list, notehistory, callback) { notehistory[i].id = encodeNoteId(id) } } catch (err) { - logger.error(err) + console.error(err) } // parse time to timestamp and fromNow const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a')) -- cgit v1.2.3