diff --git a/js/services/r2Service.js b/js/services/r2Service.js index 03f5b0df..4d77145f 100644 --- a/js/services/r2Service.js +++ b/js/services/r2Service.js @@ -769,6 +769,15 @@ class R2Service { const accessKeyId = (effectiveSettings.accessKeyId || "").trim(); const secretAccessKey = (effectiveSettings.secretAccessKey || "").trim(); + devLogger.debug("[R2] prepareUpload resolved settings", { + provider: effectiveSettings.provider || "", + accountId: accountId ? truncateMiddle(accountId, 6) : "", + bucket: effectiveSettings.bucket || "", + baseDomain: effectiveSettings.baseDomain || "", + publicBaseUrl: effectiveSettings.publicBaseUrl || "", + isLegacy: effectiveSettings.isLegacy !== false, + }); + if (!accountId || !accessKeyId || !secretAccessKey) { throw new Error("Missing R2 credentials. Unlock your storage or save settings."); } diff --git a/js/services/storageService.js b/js/services/storageService.js index aca5a580..a4bcd9de 100644 --- a/js/services/storageService.js +++ b/js/services/storageService.js @@ -50,6 +50,32 @@ function resolveEndpoint(config) { return config.endpoint || resolveConnectionMeta(config).endpoint; } +function resolvePublicBaseUrl(config) { + const meta = resolveConnectionMeta(config); + return ( + config.publicBaseUrl || + config.baseDomain || + config.publicUrl || + meta.publicBaseUrl || + meta.baseDomain || + meta.publicUrl || + meta.publicAccessUrl || + meta.publicBucketUrl || + "" + ); +} + +function normalizeConnectionMeta(meta = {}, config = {}) { + const normalized = { ...meta }; + const publicBaseUrl = resolvePublicBaseUrl({ ...config, meta }); + if (publicBaseUrl) { + normalized.publicBaseUrl = publicBaseUrl; + normalized.baseDomain = publicBaseUrl; + normalized.publicUrl = publicBaseUrl; + } + return normalized; +} + function resolveR2Endpoint(config) { const accountId = config.accountId || resolveConnectionMeta(config).accountId || ""; @@ -78,7 +104,7 @@ function buildS3TestConfig(config, overrides = {}) { region: resolveRegion(config), endpoint: resolveEndpoint(config), forcePathStyle: resolveForcePathStyle(config), - publicBaseUrl: config.publicBaseUrl, + publicBaseUrl: resolvePublicBaseUrl(config), ...overrides, }; } @@ -414,6 +440,7 @@ export class StorageService { const masterKey = this.masterKeys.get(pubkey); const encrypted = await this._encryptPayload(payload, masterKey); const account = (await this._getAccount(pubkey)) || { pubkey, connections: {} }; + const normalizedMeta = normalizeConnectionMeta(meta, payload); if (!account.connections) { account.connections = {}; @@ -433,7 +460,7 @@ export class StorageService { id: connectionId, provider: payload.provider || meta.provider, meta: { - ...meta, + ...normalizedMeta, lastSaved: Date.now(), provider: payload.provider || meta.provider, }, @@ -460,10 +487,18 @@ export class StorageService { const masterKey = this.masterKeys.get(pubkey); const payload = await this._decryptPayload(conn.encrypted, masterKey); + const normalizedMeta = normalizeConnectionMeta(conn.meta, payload); + const publicBaseUrl = resolvePublicBaseUrl({ + ...payload, + meta: normalizedMeta, + }); + const baseDomain = publicBaseUrl || payload.baseDomain || normalizedMeta.baseDomain || ""; return { ...payload, - meta: conn.meta, + publicBaseUrl, + baseDomain, + meta: normalizedMeta, }; }