Skip to content

Build your personal knowledge base with Trilium Notes

shortening of noteIds to 12 characters

azivner966ac6f620bd
diff --git a/migrations/0016__trim_note_ids2.sql b/migrations/0016__trim_note_ids2.sql
new file mode 100644
index 0000000000..ad2772fa09
--- /dev/null
+++ b/migrations/0016__trim_note_ids2.sql
@@ -0,0 +1,6 @@
+UPDATE notes SET note_id = substr(note_id, 0, 13);
+UPDATE notes_tree SET note_id = substr(note_id, 0, 13), note_pid = substr(note_pid, 0, 13);
+UPDATE notes_history SET note_id = substr(note_id, 0, 13);
+UPDATE audit_log SET note_id = substr(note_id, 0, 13);
+UPDATE links SET note_id = substr(note_id, 0, 13);
+UPDATE images SET note_id = substr(note_id, 0, 13);
\ No newline at end of file
diff --git a/public/javascripts/add_link.js b/public/javascripts/add_link.js
index 9cb50a171f..a39abf8489 100644
--- a/public/javascripts/add_link.js
+++ b/public/javascripts/add_link.js
@@ -90,7 +90,7 @@ function goToInternalNote(e, callback) {
}
function getNoteIdFromLink(url) {
- const noteIdMatch = /app#([A-Za-z0-9]{22})/.exec(url);
+ const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
if (noteIdMatch === null) {
return null;
@@ -101,7 +101,7 @@ function getNoteIdFromLink(url) {
}
function getNodeIdFromLabel(label) {
- const noteIdMatch = / \(([A-Za-z0-9]{22})\)/.exec(label);
+ const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
if (noteIdMatch !== null) {
return noteIdMatch[1];
diff --git a/routes/api/tree.js b/routes/api/tree.js
index 914a3749e6..99da35c797 100644
--- a/routes/api/tree.js
+++ b/routes/api/tree.js
@@ -51,7 +51,7 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {
'password_derived_key_salt': await sql.getOption('password_derived_key_salt'),
'encrypted_data_key': await sql.getOption('encrypted_data_key'),
'encryption_session_timeout': await sql.getOption('encryption_session_timeout'),
- 'browser_id': utils.randomToken(8),
+ 'browser_id': utils.randomString(12),
'full_load_time': utils.nowTimestamp()
});
});
diff --git a/services/migration.js b/services/migration.js
index db215ba95d..db3f565496 100644
--- a/services/migration.js
+++ b/services/migration.js
@@ -3,7 +3,7 @@ const sql = require('./sql');
const fs = require('fs-extra');
const log = require('./log');
-const APP_DB_VERSION = 15;
+const APP_DB_VERSION = 16;
const MIGRATIONS_DIR = "./migrations";
async function migrate() {
diff --git a/services/sql.js b/services/sql.js
index 6e6e25d1d7..0b8d0c07d4 100644
--- a/services/sql.js
+++ b/services/sql.js
@@ -82,7 +82,7 @@ async function addAudit(category, req=null, noteId=null, changeFrom=null, change
log.info("audit: " + category + ", browserId=" + browserId + ", noteId=" + noteId + ", from=" + changeFrom
+ ", to=" + changeTo + ", comment=" + comment);
- const id = utils.randomToken(14);
+ const id = utils.randomString(14);
await execute("INSERT INTO audit_log (id, date_modified, category, browser_id, note_id, change_from, change_to, comment)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [id, now, category, browserId, noteId, changeFrom, changeTo, comment]);
diff --git a/services/utils.js b/services/utils.js
index 5bbc9eb0b0..1c3ef3253d 100644
--- a/services/utils.js
+++ b/services/utils.js
@@ -1,20 +1,16 @@
"use strict";
-const crypto = require('crypto');
-
-function randomToken(length) {
- return crypto.randomBytes(length).toString('base64');
-}
-
function newNoteId() {
- return randomString(22, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
+ return randomString(12);
}
+const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+
function randomString(length, chars) {
let result = '';
for (let i = length; i > 0; --i) {
- result += chars[Math.floor(Math.random() * chars.length)];
+ result += ALPHA_NUMERIC[Math.floor(Math.random() * ALPHA_NUMERIC.length)];
}
return result;
@@ -33,7 +29,7 @@ function fromBase64(encodedText) {
}
module.exports = {
- randomToken,
+ randomString,
nowTimestamp,
newNoteId,
toBase64,