Files
Netcatty/infrastructure/services/adapters/WebDAVAdapter.ts
T
陈大猫andCodex 76041cc5e8 fix: skip unchanged WebDAV vault downloads on auto-sync inspect (#4312)
Persist response ETags alongside the synced snapshot anchor and use If-None-Match for remote inspections. Honor 304 without fetching a vault body, capture upload validators from the existing verification GET, and keep full downloads for upload preflight and servers without validators.

Co-authored-by: Codex <codex@openai.com>
2026-11-23 15:58:34 +08:00

208 lines
6.5 KiB
TypeScript

/**
* WebDAV Adapter - delegates to Electron main-process cloudSyncBridge.
*/
import {
SYNC_CONSTANTS,
type WebDAVConfig,
type SyncedFile,
type ProviderAccount,
type OAuthTokens,
} from '../../../domain/sync';
import { netcattyBridge } from '../netcattyBridge';
import { createSyncedFileSignature } from '../syncSignature.js';
const normalizeEndpoint = (endpoint: string): string => {
const trimmed = endpoint.trim();
if (!/^https?:\/\//i.test(trimmed)) {
return `https://${trimmed}`;
}
return trimmed;
};
const ensureLeadingSlash = (value: string): string =>
value.startsWith('/') ? value : `/${value}`;
export class WebDAVAdapter {
private config: WebDAVConfig | null;
private resource: string | null;
private account: ProviderAccount | null;
remoteValidator?: { signature: string; etag: string };
constructor(config?: WebDAVConfig, resourceId?: string) {
this.config = config
? { ...config, endpoint: normalizeEndpoint(config.endpoint) }
: null;
this.resource = resourceId || null;
this.account = this.buildAccountInfo(this.config);
}
get isAuthenticated(): boolean {
return !!this.config;
}
get accountInfo(): ProviderAccount | null {
return this.account;
}
get resourceId(): string | null {
return this.resource;
}
signOut(): void {
this.config = null;
this.resource = null;
this.account = null;
this.remoteValidator = undefined;
}
async initializeSync(): Promise<string | null> {
return this.withWebdavErrorContext('initialize', async () => {
if (!this.config) {
throw new Error('Missing WebDAV config');
}
const bridge = netcattyBridge.get();
if (!bridge?.cloudSyncWebdavInitialize) {
throw new Error('WebDAV sync requires the Netcatty desktop sync bridge');
}
const result = await bridge.cloudSyncWebdavInitialize(this.config);
this.resource = result?.resourceId || this.getSyncPath();
return this.resource;
});
}
async upload(syncedFile: SyncedFile): Promise<string> {
return this.withWebdavErrorContext('upload', async () => {
if (!this.config) {
throw new Error('Missing WebDAV config');
}
const bridge = netcattyBridge.get();
if (!bridge?.cloudSyncWebdavUpload) {
throw new Error('WebDAV sync requires the Netcatty desktop sync bridge');
}
const result = await bridge.cloudSyncWebdavUpload(this.config, syncedFile);
await this.recordValidator(syncedFile, result?.etag);
this.resource = result?.resourceId || this.getSyncPath();
return this.resource;
});
}
async download(): Promise<SyncedFile | null> {
return (await this.readRemote()).remoteFile;
}
async inspectRemote(etag: string): Promise<{ remoteFile: SyncedFile | null; notModified?: boolean }> {
return this.readRemote(etag);
}
private async readRemote(etag?: string): Promise<{ remoteFile: SyncedFile | null; notModified?: boolean }> {
return this.withWebdavErrorContext('download', async () => {
if (!this.config) {
throw new Error('Missing WebDAV config');
}
const bridge = netcattyBridge.get();
if (!bridge?.cloudSyncWebdavDownload) {
throw new Error('WebDAV sync requires the Netcatty desktop sync bridge');
}
const result = await bridge.cloudSyncWebdavDownload(this.config, etag);
if (result?.notModified) return { remoteFile: null, notModified: true };
const remoteFile = result?.syncedFile ?? null;
await this.recordValidator(remoteFile, result?.etag);
return { remoteFile };
});
}
private async recordValidator(file: SyncedFile | null, etag?: string): Promise<void> {
this.remoteValidator = undefined;
const signature = file && etag ? await createSyncedFileSignature(file) : null;
if (signature && etag) this.remoteValidator = { signature, etag };
}
async deleteSync(): Promise<void> {
return this.withWebdavErrorContext('delete', async () => {
if (!this.config) {
throw new Error('Missing WebDAV config');
}
const bridge = netcattyBridge.get();
if (!bridge?.cloudSyncWebdavDelete) {
throw new Error('WebDAV sync requires the Netcatty desktop sync bridge');
}
await bridge.cloudSyncWebdavDelete(this.config);
});
}
getTokens(): OAuthTokens | null {
return null;
}
private async withWebdavErrorContext<T>(
operation: string,
fn: () => Promise<T>,
): Promise<T> {
try {
return await fn();
} catch (error) {
throw this.buildWebdavError(operation, error);
}
}
private buildWebdavError(operation: string, error: unknown): Error {
const baseMessage = error instanceof Error ? error.message : String(error);
const details: Record<string, string | number | boolean | null | undefined> = {
operation,
};
const raw = error as {
status?: number;
statusText?: string;
url?: string;
method?: string;
code?: string;
response?: {
status?: number;
statusText?: string;
url?: string;
};
cause?: unknown;
};
if (raw?.status) details.status = raw.status;
if (raw?.statusText) details.statusText = raw.statusText;
if (raw?.url) details.url = raw.url;
if (raw?.method) details.method = raw.method;
if (raw?.code) details.code = raw.code;
if (raw?.response?.status) details.status = raw.response.status;
if (raw?.response?.statusText) details.statusText = raw.response.statusText;
if (raw?.response?.url) details.url = raw.response.url;
if (raw?.cause && typeof raw.cause === 'object') {
Object.assign(details, raw.cause as Record<string, unknown>);
details.operation = operation;
const cause = raw.cause as { code?: string };
if (cause?.code) details.causeCode = cause.code;
} else if (raw?.cause) {
details.cause = String(raw.cause);
}
const err = new Error(`WebDAV ${operation} failed: ${baseMessage}`);
(err as Error & { cause?: unknown }).cause = details;
return err;
}
private getSyncPath(): string {
return ensureLeadingSlash(SYNC_CONSTANTS.SYNC_FILE_NAME);
}
private buildAccountInfo(config: WebDAVConfig | null): ProviderAccount | null {
if (!config) return null;
try {
const url = new URL(config.endpoint);
const host = url.host;
const name = config.username ? `${config.username}@${host}` : host;
return { id: host, name };
} catch {
return { id: config.endpoint, name: config.endpoint };
}
}
}
export default WebDAVAdapter;