From a1bda416468378e1a4396b372213e184060f943b Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 6 Dec 2025 11:44:57 +0000 Subject: [PATCH] Always fetch the right document version content --- .../sync-client/src/services/sync-service.ts | 38 +++++++++++++++++++ .../sync-operations/unrestricted-syncer.ts | 12 +++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/frontend/sync-client/src/services/sync-service.ts b/frontend/sync-client/src/services/sync-service.ts index ee67630a..7d031c2e 100644 --- a/frontend/sync-client/src/services/sync-service.ts +++ b/frontend/sync-client/src/services/sync-service.ts @@ -276,6 +276,44 @@ export class SyncService { }); } + public async getDocumentVersionContent({ + documentId, + vaultUpdateId + }: { + documentId: DocumentId; + vaultUpdateId: VaultUpdateId; + }): Promise { + return this.retryForever(async () => { + this.logger.debug( + `Getting document with id ${documentId} and version ${vaultUpdateId}` + ); + + const response = await this.client( + this.getUrl( + `/documents/${documentId}/versions/${vaultUpdateId}/content` + ), + { + headers: this.getDefaultHeaders() + } + ); + + if (response.ok) { + const result = await response.bytes(); + this.logger.debug( + `Got document version content for document ${documentId} version ${vaultUpdateId}` + ); + return result; + } + + const result: SerializedError = + (await response.json()) as SerializedError; // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion + + throw new Error( + `Failed to get document: ${SyncService.formatError(result)}` + ); + }); + } + public async getAll( since?: VaultUpdateId ): Promise { diff --git a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts index ebbb076f..53960ae9 100644 --- a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts +++ b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts @@ -413,11 +413,11 @@ export class UnrestrictedSyncer { return; } - const content = ( - await this.syncService.get({ - documentId: remoteVersion.documentId - }) - ).contentBase64; + const contentBytes = + await this.syncService.getDocumentVersionContent({ + documentId: remoteVersion.documentId, + vaultUpdateId: remoteVersion.vaultUpdateId + }); // We're trying to create an entirely new document that didn't exist locally document = this.database.getDocumentByDocumentId( @@ -431,8 +431,6 @@ export class UnrestrictedSyncer { return; } - const contentBytes = base64ToBytes(content); - await this.operations.ensureClearPath(remoteVersion.relativePath); const [promise, resolve] = createPromise();