Skip to content

Commit d08c952

Browse files
committed
Update to migrate note url in the history of browser storage and cookie
Signed-off-by: Max Wu <jackymaxj@gmail.com>
1 parent fe429e9 commit d08c952

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

public/js/history.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
import store from 'store'
55
import S from 'string'
6+
import LZString from 'lz-string'
7+
8+
import {
9+
checkNoteIdValid,
10+
encodeNoteId
11+
} from './utils'
612

713
import {
814
checkIfAuth
@@ -291,6 +297,15 @@ function parseToHistory (list, notehistory, callback) {
291297
else if (!list || !notehistory) callback(list, notehistory)
292298
else if (notehistory && notehistory.length > 0) {
293299
for (let i = 0; i < notehistory.length; i++) {
300+
// migrate LZString encoded id to base64url encoded id
301+
try {
302+
let id = LZString.decompressFromBase64(notehistory[i].id)
303+
if (id && checkNoteIdValid(id)) {
304+
notehistory[i].id = encodeNoteId(id)
305+
}
306+
} catch (err) {
307+
// na
308+
}
294309
// parse time to timestamp and fromNow
295310
const timestamp = (typeof notehistory[i].time === 'number' ? moment(notehistory[i].time) : moment(notehistory[i].time, 'MMMM Do YYYY, h:mm:ss a'))
296311
notehistory[i].timestamp = timestamp.valueOf()

public/js/utils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import base64url from 'base64url'
2+
3+
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
4+
5+
export function checkNoteIdValid (id) {
6+
let result = id.match(uuidRegex)
7+
if (result && result.length === 1) {
8+
return true
9+
} else {
10+
return false
11+
}
12+
}
13+
14+
export function encodeNoteId (id) {
15+
// remove dashes in UUID and encode in url-safe base64
16+
let str = id.replace(/-/g, '')
17+
let hexStr = Buffer.from(str, 'hex')
18+
return base64url.encode(hexStr)
19+
}
20+
21+
export function decodeNoteId (encodedId) {
22+
// decode from url-safe base64
23+
let id = base64url.toBuffer(encodedId).toString('hex')
24+
// add dashes between the UUID string parts
25+
let idParts = []
26+
idParts.push(id.substr(0, 8))
27+
idParts.push(id.substr(8, 4))
28+
idParts.push(id.substr(12, 4))
29+
idParts.push(id.substr(16, 4))
30+
idParts.push(id.substr(20, 12))
31+
return idParts.join('-')
32+
}

0 commit comments

Comments
 (0)