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 }
update card title max width
reset should reset counters
access logs
retry

View file

@ -33,6 +33,7 @@ export class ObsidianFileOperations implements FileOperations {
newContent: Uint8Array
): Promise<Uint8Array> {
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);
}
@ -62,6 +63,7 @@ export class ObsidianFileOperations implements FileOperations {
return;
}
await this.createParentDirectories(normalizePath(path));
await this.vault.adapter.writeBinary(normalizePath(path), newContent);
}
@ -84,4 +86,17 @@ export class ObsidianFileOperations implements FileOperations {
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 {
const metadata = database.getDocument(relativePath);
if (metadata) {
throw new Error(
`Document metadata found for ${relativePath}, this is unexpected. Consider resetting the plugin's sync history.`
Logger.getInstance().debug(
`Document metadata already exists for ${relativePath}, it must have been downloaded from the server`
);
}
const contentBytes = await operations.read(relativePath),
contentHash = hash(contentBytes),
response = await syncServer.create({
const contentBytes = await operations.read(relativePath);
const contentHash = hash(contentBytes);
const response = await syncServer.create({
relativePath,
contentBytes,
createdDate: updateTime,
});
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
source: SyncSource.PUSH,
@ -57,8 +59,8 @@ export async function syncLocallyCreatedFile({
type: SyncType.CREATE,
});
const responseBytes = lib.base64_to_bytes(response.contentBase64),
responseHash = hash(responseBytes);
const responseBytes = lib.base64_to_bytes(response.contentBase64);
const responseHash = hash(responseBytes);
if (contentHash !== responseHash) {
await operations.write(relativePath, contentBytes, responseBytes);

View file

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

View file

@ -26,13 +26,18 @@ export async function syncRemotelyUpdatedFile({
`Syncing remotely updated file ${remoteVersion.relativePath}`
);
try {
const content = (
await syncServer.get({
documentId: remoteVersion.documentId,
})
).contentBase64,
currentVersion = database.getDocumentByDocumentId(
).contentBase64;
const contentBytes = lib.base64_to_bytes(content);
const contentHash = hash(contentBytes);
await waitForDocumentLock(remoteVersion.relativePath);
try {
const currentVersion = database.getDocumentByDocumentId(
remoteVersion.documentId
);
@ -48,18 +53,12 @@ export async function syncRemotelyUpdatedFile({
return;
}
await waitForDocumentLock(remoteVersion.relativePath);
try {
const contentBytes = lib.base64_to_bytes(content);
await operations.create(
remoteVersion.relativePath,
contentBytes
);
await operations.create(remoteVersion.relativePath, contentBytes);
await database.setDocument({
documentId: remoteVersion.documentId,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: hash(contentBytes),
hash: contentHash,
});
history.addHistoryEntry({
status: SyncStatus.SUCCESS,
@ -68,15 +67,13 @@ export async function syncRemotelyUpdatedFile({
message: `Successfully downloaded remote file which hasn't existed locally`,
type: SyncType.CREATE,
});
} finally {
unlockDocument(remoteVersion.relativePath);
}
return;
}
const [relativePath, metadata] = currentVersion;
if (relativePath !== remoteVersion.relativePath) {
await waitForDocumentLock(relativePath);
}
try {
if (remoteVersion.isDeleted) {
await operations.remove(relativePath);
@ -90,13 +87,14 @@ export async function syncRemotelyUpdatedFile({
type: SyncType.DELETE,
});
} else {
const currentContent = await operations.read(relativePath),
currentHash = hash(currentContent);
const currentContent = await operations.read(relativePath);
const currentHash = hash(currentContent);
if (currentHash !== metadata.hash) {
Logger.getInstance().info(
`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) {
await operations.move(
relativePath,
@ -104,7 +102,6 @@ export async function syncRemotelyUpdatedFile({
);
}
const contentBytes = lib.base64_to_bytes(content);
await operations.write(
remoteVersion.relativePath,
currentContent,
@ -115,7 +112,7 @@ export async function syncRemotelyUpdatedFile({
oldRelativePath: relativePath,
relativePath: remoteVersion.relativePath,
parentVersionId: remoteVersion.vaultUpdateId,
hash: metadata.hash,
hash: contentHash,
});
history.addHistoryEntry({
@ -126,10 +123,17 @@ export async function syncRemotelyUpdatedFile({
type: SyncType.UPDATE,
});
}
{
Logger.getInstance().debug(
`Document ${relativePath} is already up to date`
);
}
}
} finally {
if (relativePath !== remoteVersion.relativePath) {
unlockDocument(relativePath);
}
}
} catch (e) {
history.addHistoryEntry({
status: SyncStatus.ERROR,
@ -138,5 +142,7 @@ export async function syncRemotelyUpdatedFile({
message: `Failed to reconcile remotely updated file: ${e}`,
});
throw e;
} finally {
unlockDocument(remoteVersion.relativePath);
}
}