This commit is contained in:
Andras Schmelczer 2024-12-20 18:40:09 +00:00
parent 818812aa2d
commit 395b8b6784
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 79 additions and 55 deletions

View file

@ -67,7 +67,7 @@ pedantic = { level = "warn", priority = 0 }
cargo = { level = "warn", priority = 0 } cargo = { level = "warn", priority = 0 }
update card title max width
reset should reset counters reset should reset counters
access logs access logs
retry retry

View file

@ -33,6 +33,7 @@ export class ObsidianFileOperations implements FileOperations {
newContent: Uint8Array newContent: Uint8Array
): Promise<Uint8Array> { ): Promise<Uint8Array> {
if (!(await this.vault.adapter.exists(normalizePath(path)))) { if (!(await this.vault.adapter.exists(normalizePath(path)))) {
// The caller assumed the file exists, but it doesn't, let's not recreate it
return new Uint8Array(0); return new Uint8Array(0);
} }
@ -62,6 +63,7 @@ export class ObsidianFileOperations implements FileOperations {
return; return;
} }
await this.createParentDirectories(normalizePath(path));
await this.vault.adapter.writeBinary(normalizePath(path), newContent); await this.vault.adapter.writeBinary(normalizePath(path), newContent);
} }
@ -84,4 +86,17 @@ export class ObsidianFileOperations implements FileOperations {
normalizePath(newPath) normalizePath(newPath)
); );
} }
private async createParentDirectories(path: string): Promise<void> {
const components = path.split("/");
if (components.length === 1) {
return;
}
for (let i = 1; i < components.length; i++) {
const parentDir = components.slice(0, i).join("/");
if (!(await this.vault.adapter.exists(parentDir))) {
await this.vault.adapter.mkdir(parentDir);
}
}
}
} }

View file

@ -37,18 +37,20 @@ export async function syncLocallyCreatedFile({
try { try {
const metadata = database.getDocument(relativePath); const metadata = database.getDocument(relativePath);
if (metadata) { if (metadata) {
throw new Error( Logger.getInstance().debug(
`Document metadata found for ${relativePath}, this is unexpected. Consider resetting the plugin's sync history.` `Document metadata already exists for ${relativePath}, it must have been downloaded from the server`
); );
} }
const contentBytes = await operations.read(relativePath), const contentBytes = await operations.read(relativePath);
contentHash = hash(contentBytes), const contentHash = hash(contentBytes);
response = await syncServer.create({
relativePath, const response = await syncServer.create({
contentBytes, relativePath,
createdDate: updateTime, contentBytes,
}); createdDate: updateTime,
});
history.addHistoryEntry({ history.addHistoryEntry({
status: SyncStatus.SUCCESS, status: SyncStatus.SUCCESS,
source: SyncSource.PUSH, source: SyncSource.PUSH,
@ -57,8 +59,8 @@ export async function syncLocallyCreatedFile({
type: SyncType.CREATE, type: SyncType.CREATE,
}); });
const responseBytes = lib.base64_to_bytes(response.contentBase64), const responseBytes = lib.base64_to_bytes(response.contentBase64);
responseHash = hash(responseBytes); const responseHash = hash(responseBytes);
if (contentHash !== responseHash) { if (contentHash !== responseHash) {
await operations.write(relativePath, contentBytes, responseBytes); await operations.write(relativePath, contentBytes, responseBytes);

View file

@ -45,8 +45,8 @@ export async function syncLocallyUpdatedFile({
); );
} }
const contentBytes = await operations.read(relativePath), const contentBytes = await operations.read(relativePath);
contentHash = hash(contentBytes); const contentHash = hash(contentBytes);
if (metadata.hash === contentHash && oldPath !== undefined) { if (metadata.hash === contentHash && oldPath !== undefined) {
history.addHistoryEntry({ history.addHistoryEntry({
@ -91,6 +91,7 @@ export async function syncLocallyUpdatedFile({
} }
const responseBytes = lib.base64_to_bytes(response.contentBase64); const responseBytes = lib.base64_to_bytes(response.contentBase64);
const responseHash = hash(responseBytes);
if (response.relativePath != relativePath) { if (response.relativePath != relativePath) {
await waitForDocumentLock(response.relativePath); await waitForDocumentLock(response.relativePath);
@ -116,7 +117,7 @@ export async function syncLocallyUpdatedFile({
} finally { } finally {
unlockDocument(response.relativePath); unlockDocument(response.relativePath);
} }
} else { } else if (contentHash !== responseHash) {
await operations.write(relativePath, contentBytes, responseBytes); await operations.write(relativePath, contentBytes, responseBytes);
} }
@ -125,7 +126,7 @@ export async function syncLocallyUpdatedFile({
oldRelativePath: oldPath ?? relativePath, oldRelativePath: oldPath ?? relativePath,
relativePath: response.relativePath, relativePath: response.relativePath,
parentVersionId: response.vaultUpdateId, parentVersionId: response.vaultUpdateId,
hash: contentHash, hash: responseHash,
}); });
} catch (e) { } catch (e) {
history.addHistoryEntry({ history.addHistoryEntry({

View file

@ -26,15 +26,20 @@ export async function syncRemotelyUpdatedFile({
`Syncing remotely updated file ${remoteVersion.relativePath}` `Syncing remotely updated file ${remoteVersion.relativePath}`
); );
const content = (
await syncServer.get({
documentId: remoteVersion.documentId,
})
).contentBase64;
const contentBytes = lib.base64_to_bytes(content);
const contentHash = hash(contentBytes);
await waitForDocumentLock(remoteVersion.relativePath);
try { try {
const content = ( const currentVersion = database.getDocumentByDocumentId(
await syncServer.get({ remoteVersion.documentId
documentId: remoteVersion.documentId, );
})
).contentBase64,
currentVersion = database.getDocumentByDocumentId(
remoteVersion.documentId
);
if (!currentVersion) { if (!currentVersion) {
if (remoteVersion.isDeleted) { if (remoteVersion.isDeleted) {
@ -48,35 +53,27 @@ export async function syncRemotelyUpdatedFile({
return; return;
} }
await waitForDocumentLock(remoteVersion.relativePath); await operations.create(remoteVersion.relativePath, contentBytes);
try { await database.setDocument({
const contentBytes = lib.base64_to_bytes(content); documentId: remoteVersion.documentId,
await operations.create( relativePath: remoteVersion.relativePath,
remoteVersion.relativePath, parentVersionId: remoteVersion.vaultUpdateId,
contentBytes hash: contentHash,
); });
await database.setDocument({ history.addHistoryEntry({
documentId: remoteVersion.documentId, status: SyncStatus.SUCCESS,
relativePath: remoteVersion.relativePath, source: SyncSource.PULL,
parentVersionId: remoteVersion.vaultUpdateId, relativePath: remoteVersion.relativePath,
hash: hash(contentBytes), message: `Successfully downloaded remote file which hasn't existed locally`,
}); type: SyncType.CREATE,
history.addHistoryEntry({ });
status: SyncStatus.SUCCESS,
source: SyncSource.PULL,
relativePath: remoteVersion.relativePath,
message: `Successfully downloaded remote file which hasn't existed locally`,
type: SyncType.CREATE,
});
} finally {
unlockDocument(remoteVersion.relativePath);
}
return; return;
} }
const [relativePath, metadata] = currentVersion; const [relativePath, metadata] = currentVersion;
await waitForDocumentLock(relativePath); if (relativePath !== remoteVersion.relativePath) {
await waitForDocumentLock(relativePath);
}
try { try {
if (remoteVersion.isDeleted) { if (remoteVersion.isDeleted) {
await operations.remove(relativePath); await operations.remove(relativePath);
@ -90,13 +87,14 @@ export async function syncRemotelyUpdatedFile({
type: SyncType.DELETE, type: SyncType.DELETE,
}); });
} else { } else {
const currentContent = await operations.read(relativePath), const currentContent = await operations.read(relativePath);
currentHash = hash(currentContent); const currentHash = hash(currentContent);
if (currentHash !== metadata.hash) { if (currentHash !== metadata.hash) {
Logger.getInstance().info( Logger.getInstance().info(
`Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed` `Document ${relativePath} has been updated both remotely and locally, skipping until the event is processed`
); );
} else { } else if (contentHash !== metadata.hash) {
if (relativePath !== remoteVersion.relativePath) { if (relativePath !== remoteVersion.relativePath) {
await operations.move( await operations.move(
relativePath, relativePath,
@ -104,7 +102,6 @@ export async function syncRemotelyUpdatedFile({
); );
} }
const contentBytes = lib.base64_to_bytes(content);
await operations.write( await operations.write(
remoteVersion.relativePath, remoteVersion.relativePath,
currentContent, currentContent,
@ -115,7 +112,7 @@ export async function syncRemotelyUpdatedFile({
oldRelativePath: relativePath, oldRelativePath: relativePath,
relativePath: remoteVersion.relativePath, relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId, parentVersionId: remoteVersion.vaultUpdateId,
hash: metadata.hash, hash: contentHash,
}); });
history.addHistoryEntry({ history.addHistoryEntry({
@ -126,9 +123,16 @@ export async function syncRemotelyUpdatedFile({
type: SyncType.UPDATE, type: SyncType.UPDATE,
}); });
} }
{
Logger.getInstance().debug(
`Document ${relativePath} is already up to date`
);
}
} }
} finally { } finally {
unlockDocument(relativePath); if (relativePath !== remoteVersion.relativePath) {
unlockDocument(relativePath);
}
} }
} catch (e) { } catch (e) {
history.addHistoryEntry({ history.addHistoryEntry({
@ -138,5 +142,7 @@ export async function syncRemotelyUpdatedFile({
message: `Failed to reconcile remotely updated file: ${e}`, message: `Failed to reconcile remotely updated file: ${e}`,
}); });
throw e; throw e;
} finally {
unlockDocument(remoteVersion.relativePath);
} }
} }