From 1b45138451bfbd69c177d7a8fe43e678ed814403 Mon Sep 17 00:00:00 2001 From: SNEK WAIFU Date: Sun, 15 Mar 2026 20:53:13 +0700 Subject: [PATCH] fix(enhanceSaveImage): allow duplicate downloads by unique filenames --- src/components/HOC/enhanceSaveImage.js | 37 ++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/components/HOC/enhanceSaveImage.js b/src/components/HOC/enhanceSaveImage.js index 490d00f9..c722102b 100644 --- a/src/components/HOC/enhanceSaveImage.js +++ b/src/components/HOC/enhanceSaveImage.js @@ -113,6 +113,25 @@ const enhanceSaveImage = (WrappedComponent) => { }); }; + getUniqueFilePath = async (imagesDir, fileName) => { + const lastDotIndex = fileName.lastIndexOf('.'); + const hasExt = lastDotIndex > 0; + const baseName = hasExt ? fileName.slice(0, lastDotIndex) : fileName; + const ext = hasExt ? fileName.slice(lastDotIndex) : ''; + + let candidateName = fileName; + let candidatePath = `${imagesDir}/${candidateName}`; + let counter = 1; + + while (await RNFetchBlob.fs.exists(candidatePath)) { + candidateName = `${baseName}_${counter}${ext}`; + candidatePath = `${imagesDir}/${candidateName}`; + counter += 1; + } + + return { filePath: candidatePath, fileName: candidateName }; + }; + saveImage = async ({ imageUrls, imageIndex, @@ -177,9 +196,11 @@ const enhanceSaveImage = (WrappedComponent) => { saveImageSettings, imageIndex || index, ); + const { filePath: targetPath, fileName: targetFileName } = + await this.getUniqueFilePath(imagesDir, fileName); try { const res = await RNFetchBlob.config({ - path: `${imagesDir}/${fileName}`, + path: targetPath, }).fetch('GET', url, { referer: 'http://www.pixiv.net', }); @@ -188,19 +209,25 @@ const enhanceSaveImage = (WrappedComponent) => { try { await CameraRoll.saveToCameraRoll(filePath); this.showToast( - i18n.formatString(i18n.saveImageSuccess, fileName), + i18n.formatString(i18n.saveImageSuccess, targetFileName), ); } catch (err) { - this.showToast(i18n.formatString(i18n.saveImageError, fileName)); + this.showToast( + i18n.formatString(i18n.saveImageError, targetFileName), + ); } } else if (Platform.OS === 'android') { - this.showToast(i18n.formatString(i18n.saveImageSuccess, fileName)); + this.showToast( + i18n.formatString(i18n.saveImageSuccess, targetFileName), + ); try { await RNFetchBlob.fs.scanFile([{ path: filePath }]); } catch (err) {} } } catch (err) { - this.showToast(i18n.formatString(i18n.saveImageError, fileName)); + this.showToast( + i18n.formatString(i18n.saveImageError, targetFileName), + ); } }); return null;