File sync

This commit is contained in:
Magnus Åhall 2026-07-10 06:32:31 +02:00
parent b76502c034
commit 47c4c99b66
7 changed files with 174 additions and 19 deletions

View file

@ -10,8 +10,6 @@ export class Sync {
async run() {//{{{
try {
let duration = 0 // in ms
// The latest sync node value is used to retrieve the changes
// from the backend.
const state = await nodeStore.getAppState('latest_sync_node')
@ -24,11 +22,12 @@ export class Sync {
_mbus.dispatch('SYNC_DOWNLOAD_COUNT', { count: nodeCountDownload })
_mbus.dispatch('SYNC_UPLOAD_COUNT', { count: nodeCountUpload })
await this.nodesFromServer(oldMax)
.then(durationNodes => {
duration = durationNodes // in ms
console.log(`Total time: ${Math.round(1000 * durationNodes) / 1000}s`)
})
const durationNodes = await this.nodesFromServer(oldMax)
console.log(`Total time: ${Math.round(1000 * durationNodes) / 1000}s`)
// Files are uploaded first since nodes can have references to them
// and the server requires linking a reference to the node and the file table.
await this.filesToServer()
// Uploads of modified nodes to server.
await this.nodesToServer()
@ -80,8 +79,9 @@ export class Sync {
await this.handleNode(backendNode, objstore)
handled++
if (handled % 100 === 0)
_mbus.dispatch('SYNC_DOWNLOADED', { handled })
// XXX - should this be back for performance or not?
// if (handled % 100 === 0)
_mbus.dispatch('SYNC_DOWNLOADED', { handled })
}
} while (res.Continue)
@ -195,7 +195,6 @@ export class Sync {
alert(e.message)
}
}// }}}
async filesFromServer() {// {{{
let offset = 0
let more = true
@ -204,13 +203,64 @@ export class Sync {
while (more) {
const res = await API.query('GET', `/file/server_uuids/${offset}`)
more = res.MoreRowsExist
console.log(res)
offset++
for (const uuid of res.UUIDs) {
const md = await globalThis.nodeStore.filesMetadata.get(uuid)
if (!md) {
await this.storeFileFromServer(uuid)
}
}
}
} catch (e) {
console.error(e)
alert(e.message)
}
}// }}}
async storeFileFromServer(uuid) {// {{{
const headers = {}
const token = localStorage.getItem('token')
if (token) {
headers.Authorization = `Bearer ${token}`
}
const res = await fetch(`/file/uuid/${uuid}`, { headers })
const size = parseInt(res.headers.get('content-length'))
const fileData = {
UUID: uuid,
uploaded: 'on_server',
name: res.headers.get('x-file-name'),
size: res.headers.get('content-length'),
type: res.headers.get('content-type'),
modified: res.headers.get('x-file-modified'),
}
_mbus.dispatch('FILE_SYNC', {
direction: 'download',
name: fileData.name,
})
let prevPercent = 0
await globalThis.nodeStore.storeFile(fileData, res.body.getReader({ mode: 'byob' }), (downloaded) => {
const percent = Math.round((downloaded / size) * 100)
if (prevPercent != percent) {
_mbus.dispatch('FILE_DOWNLOAD_PROGRESS', {
uuid,
downloaded,
size,
percent,
done: false,
})
prevPercent = percent
}
})
_mbus.dispatch('FILE_DOWNLOAD_PROGRESS', {
uuid,
done: true,
})
}// }}}
}
export class N2SyncProgress extends CustomHTMLElement {