From ae366bab21028ef9ce33cc2ee9e6ef359fc7d32e Mon Sep 17 00:00:00 2001 From: yang07861 Date: Thu, 14 Aug 2025 15:41:02 +0800 Subject: [PATCH] fix(s3): Correctly handle file deletion with custom URL (#358) * fix(s3): Correctly handle file deletion with custom URL When a custom URL is configured for an S3-compatible provider, the application was failing to delete files from the remote bucket. This was because the deletion logic incorrectly used the full pathname of the custom URL as the S3 object key, instead of the object's actual key within the bucket. This commit modifies the `removeFileFromS3InMain` function to be aware of the `customUrl` setting. If a custom URL is in use, the logic now correctly strips the custom URL's prefix from the image URL to derive the proper S3 object key before sending the `DeleteObject` command. * feat(debug): Add detailed logging to S3 deletion This commit adds extensive console logging to the `removeFileFromS3InMain` function in `src/main/utils/deleteFunc.ts`. The purpose of this change is to debug an issue where file deletion fails with a custom URL. These logs will capture the state of key variables (configMap, imgUrl, customUrl, fileKey) and any errors returned by the S3 client during the deletion process. This is a temporary debugging measure. * fix(s3): Correctly handle file deletion using urlPrefix This commit fixes a bug where file deletion from an S3-compatible provider would fail if a custom URL was configured. The root cause was that the logic was looking for a `customUrl` property in the config, when the correct property name is `urlPrefix`. The `removeFileFromS3InMain` function has been updated to use the correct `urlPrefix` property. This ensures that when a custom URL is present, the S3 object key is derived correctly by stripping the prefix, allowing the deletion to succeed. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/main/utils/deleteFunc.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/utils/deleteFunc.ts b/src/main/utils/deleteFunc.ts index d2c230e3..a51a2f17 100644 --- a/src/main/utils/deleteFunc.ts +++ b/src/main/utils/deleteFunc.ts @@ -93,7 +93,16 @@ export async function removeFileFromS3InMain (configMap: IStringKeyMap, dogeMode const { url: rawUrl, type, - config: { accessKeyID, secretAccessKey, bucketName, endpoint, pathStyleAccess, rejectUnauthorized, proxy } + config: { + accessKeyID, + secretAccessKey, + bucketName, + endpoint, + pathStyleAccess, + rejectUnauthorized, + proxy, + urlPrefix + } } = configMap let { imgUrl, @@ -102,10 +111,21 @@ export async function removeFileFromS3InMain (configMap: IStringKeyMap, dogeMode if (type === 'aws-s3' || type === 'aws-s3-plist') { imgUrl = rawUrl || imgUrl || '' } - const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl) - let fileKey = url.pathname.replace(/^\/+/, '') - if (pathStyleAccess) { - fileKey = fileKey.replace(/^[^/]+\//, '') + let fileKey + if (urlPrefix && imgUrl.startsWith(urlPrefix)) { + const urlPrefixObj = new URL(urlPrefix) + const imgUrlObj = new URL(imgUrl) + if (imgUrlObj.pathname.startsWith(urlPrefixObj.pathname)) { + fileKey = imgUrlObj.pathname.substring(urlPrefixObj.pathname.length).replace(/^\/+/, '') + } else { + fileKey = imgUrlObj.pathname.replace(/^\/+/, '') + } + } else { + const url = new URL(!/^https?:\/\//.test(imgUrl) ? `http://${imgUrl}` : imgUrl) + fileKey = url.pathname.replace(/^\/+/, '') + if (pathStyleAccess) { + fileKey = fileKey.replace(/^[^/]+\//, '') + } } const endpointUrl: string | undefined = endpoint ? /^https?:\/\//.test(endpoint)