Upload files to IndexedDB
This commit is contained in:
parent
5bd5ef1f02
commit
8b421ea59e
15 changed files with 539 additions and 99 deletions
|
|
@ -12,10 +12,11 @@ export class NodeStore {
|
|||
this.nodes = {}
|
||||
this.sendQueue = null
|
||||
this.nodesHistory = null
|
||||
this.files = null
|
||||
}//}}}
|
||||
initializeDB() {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = indexedDB.open('notes', 7)
|
||||
const req = indexedDB.open('notes', 8)
|
||||
|
||||
// Schema upgrades for IndexedDB.
|
||||
// These can start from different points depending on updates to Notes2 since a device was online.
|
||||
|
|
@ -24,6 +25,7 @@ export class NodeStore {
|
|||
let appState
|
||||
let sendQueue
|
||||
let nodesHistory
|
||||
let files
|
||||
const db = event.target.result
|
||||
const trx = event.target.transaction
|
||||
|
||||
|
|
@ -61,6 +63,10 @@ export class NodeStore {
|
|||
case 7:
|
||||
trx.objectStore('nodes_history').createIndex('byUUID', 'UUID', { unique: false })
|
||||
break
|
||||
|
||||
case 8:
|
||||
files = db.createObjectStore('files', { keyPath: 'UUID' })
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -69,6 +75,7 @@ export class NodeStore {
|
|||
this.db = event.target.result
|
||||
this.sendQueue = new SimpleNodeStore(this.db, 'send_queue')
|
||||
this.nodesHistory = new SimpleNodeStore(this.db, 'nodes_history')
|
||||
this.files = new SimpleNodeStore(this.db, 'files')
|
||||
this.initializeRootNode()
|
||||
.then(() => resolve())
|
||||
}
|
||||
|
|
@ -159,39 +166,6 @@ export class NodeStore {
|
|||
})
|
||||
}//}}}
|
||||
|
||||
/*
|
||||
upsertNodeRecords(records) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
const t = this.db.transaction('nodes', 'readwrite')
|
||||
const nodeStore = t.objectStore('nodes')
|
||||
t.onerror = (event) => {
|
||||
console.log('transaction error', event.target.error)
|
||||
reject(event.target.error)
|
||||
}
|
||||
t.oncomplete = () => {
|
||||
resolve()
|
||||
}
|
||||
|
||||
// records is an object, not an array.
|
||||
for (const i in records) {
|
||||
const record = records[i]
|
||||
|
||||
let addReq
|
||||
let op
|
||||
if (record.Deleted) {
|
||||
op = 'deleting'
|
||||
addReq = nodeStore.delete(record.UUID)
|
||||
} else {
|
||||
op = 'upserting'
|
||||
// 'modified' is a local property for tracking
|
||||
// nodes needing to be synced to backend.
|
||||
record.modified = 0
|
||||
addReq = nodeStore.put(record)
|
||||
}
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
*/
|
||||
getTreeNodes(parent, newLevel) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
// Parent of toplevel nodes is ROOT_NODE in indexedDB.
|
||||
|
|
@ -376,8 +350,20 @@ class SimpleNodeStore {
|
|||
}
|
||||
})
|
||||
}//}}}
|
||||
get(key) {//{{{
|
||||
return new Promise((resolve, _reject) => {
|
||||
const req = this.db
|
||||
.transaction(['nodes', this.storeName], 'readonly')
|
||||
.objectStore(this.storeName)
|
||||
.get(key)
|
||||
|
||||
req.onsuccess = (event) => {
|
||||
resolve(event.target.result)
|
||||
}
|
||||
})
|
||||
}//}}}
|
||||
retrieve(limit) {//{{{
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise((resolve, _reject) => {
|
||||
const cursorReq = this.db
|
||||
.transaction(['nodes', this.storeName], 'readonly')
|
||||
.objectStore(this.storeName)
|
||||
|
|
@ -433,4 +419,51 @@ class SimpleNodeStore {
|
|||
}//}}}
|
||||
}
|
||||
|
||||
export class StoreFile {
|
||||
static createFromFileObject(f) {
|
||||
const obj = new StoreFile()
|
||||
obj.name = f.name
|
||||
obj.size = f.size
|
||||
obj.mime = f.type
|
||||
return obj
|
||||
}
|
||||
constructor() {
|
||||
this.name = ''
|
||||
this.size = 0
|
||||
this.mime = ''
|
||||
|
||||
this.objectURL = null // URL.createObjectURL(blob)
|
||||
}
|
||||
data() {
|
||||
return {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function uuidv7() {
|
||||
// random bytes
|
||||
const value = new Uint8Array(16)
|
||||
crypto.getRandomValues(value)
|
||||
|
||||
// current timestamp in ms
|
||||
const timestamp = BigInt(Date.now())
|
||||
|
||||
// timestamp
|
||||
value[0] = Number((timestamp >> 40n) & 0xffn)
|
||||
value[1] = Number((timestamp >> 32n) & 0xffn)
|
||||
value[2] = Number((timestamp >> 24n) & 0xffn)
|
||||
value[3] = Number((timestamp >> 16n) & 0xffn)
|
||||
value[4] = Number((timestamp >> 8n) & 0xffn)
|
||||
value[5] = Number(timestamp & 0xffn)
|
||||
|
||||
// version and variant
|
||||
value[6] = (value[6] & 0x0f) | 0x70
|
||||
value[8] = (value[8] & 0x3f) | 0x80
|
||||
|
||||
const str = Array.from(value)
|
||||
.map((b) => b.toString(16).padStart(2, "0"))
|
||||
.join("")
|
||||
return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`
|
||||
}
|
||||
|
||||
// vim: foldmethod=marker
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue