mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-12 22:26:08 -04:00
Remove and replace all note id compression in LZString with base64url
Signed-off-by: Max Wu <jackymaxj@gmail.com>
This commit is contained in:
parent
912cce2b40
commit
baa0418fb5
4 changed files with 34 additions and 8 deletions
|
@ -3,6 +3,7 @@
|
|||
var fs = require('fs')
|
||||
var path = require('path')
|
||||
var LZString = require('lz-string')
|
||||
var base64url = require('base64url')
|
||||
var md = require('markdown-it')()
|
||||
var metaMarked = require('meta-marked')
|
||||
var cheerio = require('cheerio')
|
||||
|
@ -114,6 +115,22 @@ module.exports = function (sequelize, DataTypes) {
|
|||
return false
|
||||
}
|
||||
},
|
||||
encodeNoteId: function (id) {
|
||||
// remove dashes in UUID and encode in url-safe base64
|
||||
return base64url.encode(id.replace(/-/g, ''))
|
||||
},
|
||||
decodeNoteId: function (encodedId) {
|
||||
// decode from url-safe base64
|
||||
let id = base64url.decode(encodedId)
|
||||
// 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('-')
|
||||
},
|
||||
checkNoteIdValid: function (id) {
|
||||
var 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
|
||||
var result = id.match(uuidRegex)
|
||||
|
@ -190,6 +207,16 @@ module.exports = function (sequelize, DataTypes) {
|
|||
return _callback(err, null)
|
||||
})
|
||||
},
|
||||
parseNoteIdByBase64Url: function (_callback) {
|
||||
// try to parse note id by base64url
|
||||
try {
|
||||
var id = Note.decodeNoteId(noteId)
|
||||
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
|
||||
} catch (err) {
|
||||
return _callback(err, null)
|
||||
}
|
||||
},
|
||||
// parse note id by LZString is deprecated, here for compability
|
||||
parseNoteIdByLZString: function (_callback) {
|
||||
// try to parse note id by LZString Base64
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue