From 5e48e80fb5b3a1dfbdb4f463fe73515f2073fad2 Mon Sep 17 00:00:00 2001 From: Kuingsmile <96409857+Kuingsmile@users.noreply.github.com> Date: Thu, 30 Apr 2026 22:26:23 +0800 Subject: [PATCH] :sparkles: Feature(custom): support sha1,timestampS, fix rename bug in cloud and gallery --- src/preload/index.ts | 3 +- .../components/ImageProcessSetting.vue | 3 ++ src/renderer/i18n/locales/en.json | 3 ++ src/renderer/i18n/locales/zh-CN.json | 3 ++ src/renderer/i18n/locales/zh-TW.json | 3 ++ src/renderer/manage/pages/BucketPage.vue | 10 +++-- src/renderer/manage/pages/ManageSetting.vue | 3 ++ src/renderer/manage/utils/common.ts | 42 ++++++++++++++----- src/renderer/manage/utils/digestAuth.ts | 2 +- src/renderer/pages/Gallery.vue | 4 +- src/renderer/pages/PicGoSetting.vue | 3 ++ src/universal/types/shims-tsx.d.ts | 4 +- 12 files changed, 64 insertions(+), 19 deletions(-) diff --git a/src/preload/index.ts b/src/preload/index.ts index 91680048..b5a5685d 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -181,11 +181,12 @@ try { fs: { remove: fs.remove, readFile: fs.readFile, + readFileSync: fs.readFileSync, statSync: fs.statSync, }, crypto: { randomBytes: crypto.randomBytes, - createHash: crypto.createHash, + createHash: (algorithm: string, text: string | Buffer) => crypto.createHash(algorithm).update(text).digest('hex'), }, yaml: { parse: yaml.parseDocument, diff --git a/src/renderer/components/ImageProcessSetting.vue b/src/renderer/components/ImageProcessSetting.vue index 6f3e41f2..27190bb7 100644 --- a/src/renderer/components/ImageProcessSetting.vue +++ b/src/renderer/components/ImageProcessSetting.vue @@ -1054,11 +1054,14 @@ const advancedRenameList = computed(() => ({ { label: t('pages.settings.upload.placeholder.second'), value: '{s}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, + { label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' }, ], categoryHash: [ { label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { label: t('pages.settings.upload.placeholder.md5-16'), value: '{md5-16}' }, { label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, + { label: t('pages.settings.upload.placeholder.sha1'), value: '{sha1}' }, + { label: t('pages.settings.upload.placeholder.sha1-n'), value: '{sha1-n}' }, { label: t('pages.settings.upload.placeholder.sha256'), value: '{sha256}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, ], diff --git a/src/renderer/i18n/locales/en.json b/src/renderer/i18n/locales/en.json index ad7393ac..d0b1fa25 100644 --- a/src/renderer/i18n/locales/en.json +++ b/src/renderer/i18n/locales/en.json @@ -1077,9 +1077,12 @@ "month": "Month, 2 Digits", "randomString": "Random String (number of digits)", "second": "Second, 2 Digits", + "sha1": "SHA1 Hash", + "sha1-n": "SHA1 Hash (First n Digits)", "sha256": "SHA256 Hash", "sha256-n": "SHA256 Hash (First n Digits)", "timestamp": "Timestamp, 13 Digits", + "timestampS": "Timestamp, 10 Digits", "uuid": "Random UUID", "year2": "Year, 2 Digits", "year4": "Year, 4 Digits" diff --git a/src/renderer/i18n/locales/zh-CN.json b/src/renderer/i18n/locales/zh-CN.json index c09e117e..41274425 100644 --- a/src/renderer/i18n/locales/zh-CN.json +++ b/src/renderer/i18n/locales/zh-CN.json @@ -1077,9 +1077,12 @@ "month": "月份,2位数", "randomString": "number位随机字符串", "second": "秒,2位数", + "sha1": "SHA1 哈希", + "sha1-n": "SHA1 哈希(前n位)", "sha256": "SHA256 哈希", "sha256-n": "SHA256 哈希(前n位)", "timestamp": "时间戳,13位数", + "timestampS": "时间戳,10位数", "uuid": "随机 UUID", "year2": "年份,2位数", "year4": "年份,4位数" diff --git a/src/renderer/i18n/locales/zh-TW.json b/src/renderer/i18n/locales/zh-TW.json index 1a4f361a..5a38bb13 100644 --- a/src/renderer/i18n/locales/zh-TW.json +++ b/src/renderer/i18n/locales/zh-TW.json @@ -1077,9 +1077,12 @@ "month": "月份,2位數", "randomString": "number位隨機字符串", "second": "秒,2位數", + "sha1": "SHA1 哈希", + "sha1-n": "SHA1 哈希(前n位)", "sha256": "SHA256 哈希", "sha256-n": "SHA256 哈希(前n位)", "timestamp": "時間戳,13位數", + "timestampS": "時間戳,10位數", "uuid": "隨機 UUID", "year2": "年份,2位數", "year4": "年份,4位數" diff --git a/src/renderer/manage/pages/BucketPage.vue b/src/renderer/manage/pages/BucketPage.vue index cbf1c94e..dadd696d 100644 --- a/src/renderer/manage/pages/BucketPage.vue +++ b/src/renderer/manage/pages/BucketPage.vue @@ -1328,11 +1328,14 @@ const advancedRenameList = computed(() => ({ { label: t('pages.settings.upload.placeholder.second'), value: '{s}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, + { label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' }, ], categoryHash: [ { label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { label: t('pages.settings.upload.placeholder.md5-16'), value: '{md5-16}' }, { label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, + { label: t('pages.settings.upload.placeholder.sha1'), value: '{sha1}' }, + { label: t('pages.settings.upload.placeholder.sha1-n'), value: '{sha1-n}' }, { label: t('pages.settings.upload.placeholder.sha256'), value: '{sha256}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, ], @@ -1701,15 +1704,16 @@ function clearTableData() { uploadPanelFilesList.value = [] } -function renameFileBeforeUpload(filePath: string): string { +function renameFileBeforeUpload(filePath: string, fullPath: string): string { const fileName = window.node.path.basename(filePath) + const fileBuffer = window.node.fs.readFileSync(fullPath) const typeMap = { timestampRename: manageStore.config.settings.timestampRename, randomStringRename: manageStore.config.settings.randomStringRename, customRenameFormat: manageStore.config.settings.customRenameFormat, customRename: manageStore.config.settings.customRename, } - return renameFile(typeMap, fileName) + return renameFile(typeMap, fileName, fileBuffer) } function uploadFiles() { @@ -1719,7 +1723,7 @@ function uploadFiles() { rawName: item.name, path: item.path.replace(/\\/g, '/'), size: item.size, - renamedFileName: renameFileBeforeUpload(item.name), + renamedFileName: renameFileBeforeUpload(item.name, item.path), relativePath: item.relativePath ?? '', }) }) diff --git a/src/renderer/manage/pages/ManageSetting.vue b/src/renderer/manage/pages/ManageSetting.vue index a05a5046..7a6f0be7 100644 --- a/src/renderer/manage/pages/ManageSetting.vue +++ b/src/renderer/manage/pages/ManageSetting.vue @@ -284,11 +284,14 @@ const advancedRenameList = computed(() => ({ { label: t('pages.settings.upload.placeholder.second'), value: '{s}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, + { label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' }, ], categoryHash: [ { label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { label: t('pages.settings.upload.placeholder.md5-16'), value: '{md5-16}' }, { label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, + { label: t('pages.settings.upload.placeholder.sha1'), value: '{sha1}' }, + { label: t('pages.settings.upload.placeholder.sha1-n'), value: '{sha1-n}' }, { label: t('pages.settings.upload.placeholder.sha256'), value: '{sha256}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, ], diff --git a/src/renderer/manage/utils/common.ts b/src/renderer/manage/utils/common.ts index 38425e54..b1a56462 100644 --- a/src/renderer/manage/utils/common.ts +++ b/src/renderer/manage/utils/common.ts @@ -48,15 +48,24 @@ function renameFormatHelper(num: number): string { return num.toString().length === 1 ? `0${num}` : num.toString() } -function getMd5(input: any): string { - return window.node.crypto.createHash('md5').update(input).digest('hex') +function getMd5(input: Buffer | string): string { + return window.node.crypto.createHash('md5', input) } -function getSha256(input: any): string { - return window.node.crypto.createHash('sha256').update(input).digest('hex') +function getSha256(input: Buffer | string): string { + return window.node.crypto.createHash('sha256', input) } -export function renameFileNameWithCustomString(oldName: string, customFormat: string, affixFileName?: string): string { +function getSha1(input: Buffer | string): string { + return window.node.crypto.createHash('sha1', input) +} + +export function renameFileNameWithCustomString( + oldName: string, + customFormat: string, + affixFileName?: string, + fileBuffer?: Buffer, +): string { const date = new Date() const year = date.getFullYear().toString() const fileBaseName = window.node.path.basename(oldName, window.node.path.extname(oldName)) @@ -69,19 +78,24 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st '{i}': () => renameFormatHelper(date.getMinutes()), '{s}': () => renameFormatHelper(date.getSeconds()), '{ms}': () => date.getMilliseconds().toString().padStart(3, '0'), - '{md5}': () => getMd5(fileBaseName), - '{md5-16}': () => getMd5(fileBaseName).slice(0, 16), - '{sha256}': () => getSha256(fileBaseName), + '{md5}': () => getMd5(fileBuffer || fileBaseName), + '{md5-16}': () => getMd5(fileBuffer || fileBaseName).slice(0, 16), + '{sha1}': () => getSha1(fileBuffer || fileBaseName), + '{sha256}': () => getSha256(fileBuffer || fileBaseName), '{filename}': () => affixFileName ? window.node.path.basename(affixFileName, window.node.path.extname(affixFileName)) : window.node.path.basename(oldName, window.node.path.extname(oldName)), '{uuid}': () => uuidv4().replace(/-/g, ''), '{timestamp}': () => date.getTime().toString(), + '{timestampS}': () => Math.floor(date.getTime() / 1000).toString(), } if ( customFormat === undefined || - (!Object.keys(conversionMap).some(item => customFormat.includes(item)) && !customFormat.includes('{str-')) + (!Object.keys(conversionMap).some(item => customFormat.includes(item)) && + !customFormat.includes('{str-') && + !/{sha256-\d+}/.test(customFormat) && + !/{sha1-\d+}/.test(customFormat)) ) { return oldName } @@ -92,9 +106,14 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st }, customFormat) + ext const strRegex = /{str-(\d+)}/gi const sha256nRegex = /{sha256-(\d+)}/gi + const sha1nRegex = /{sha1-(\d+)}/gi newName = newName.replace(sha256nRegex, (_, group1) => { const length = parseInt(group1, 10) - return getSha256(fileBaseName).slice(0, length) + return getSha256(fileBuffer || fileBaseName).slice(0, length) + }) + newName = newName.replace(sha1nRegex, (_, group1) => { + const length = parseInt(group1, 10) + return getSha1(fileBuffer || fileBaseName).slice(0, length) }) newName = newName.replace(strRegex, (_, group1) => { const length = parseInt(group1, 10) @@ -106,6 +125,7 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st export function renameFile( { timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap, oldName = '', + fileBuffer?: Buffer, ): string { switch (true) { case timestampRename: @@ -113,7 +133,7 @@ export function renameFile( case randomStringRename: return renameFileNameWithRandomString(oldName, 20) case customRename: - return renameFileNameWithCustomString(oldName, customRenameFormat) + return renameFileNameWithCustomString(oldName, customRenameFormat, undefined, fileBuffer) default: return oldName } diff --git a/src/renderer/manage/utils/digestAuth.ts b/src/renderer/manage/utils/digestAuth.ts index 7616dd74..311bad7f 100644 --- a/src/renderer/manage/utils/digestAuth.ts +++ b/src/renderer/manage/utils/digestAuth.ts @@ -3,7 +3,7 @@ let NC = 0 const NC_PAD = '00000000' function md5(text: any) { - return window.node.crypto.createHash('md5').update(text).digest('hex') + return window.node.crypto.createHash('md5', text) } export function digestAuthHeader( diff --git a/src/renderer/pages/Gallery.vue b/src/renderer/pages/Gallery.vue index bbf08298..3b0a43ba 100644 --- a/src/renderer/pages/Gallery.vue +++ b/src/renderer/pages/Gallery.vue @@ -514,17 +514,19 @@ const advancedRenameList = { { label: t('pages.settings.upload.placeholder.second'), value: '{s}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, + { label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' }, ], categoryHash: [ { label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { label: t('pages.settings.upload.placeholder.md5-16'), value: '{md5-16}' }, { label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, + { label: t('pages.settings.upload.placeholder.sha1'), value: '{sha1}' }, + { label: t('pages.settings.upload.placeholder.sha1-n'), value: '{sha1-n}' }, { label: t('pages.settings.upload.placeholder.sha256'), value: '{sha256}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, ], categoryFile: [ { label: t('pages.settings.upload.placeholder.filename'), value: '{filename}' }, - { label: t('pages.settings.upload.placeholder.localFolder'), value: '{localFolder:n}' }, { label: t('pages.settings.upload.placeholder.randomString'), value: '{str-n}' }, ], } diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index bc1ec254..33a5a4ef 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -1583,11 +1583,14 @@ const advancedRenameList = { { label: t('pages.settings.upload.placeholder.second'), value: '{s}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, + { label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' }, ], categoryHash: [ { label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { label: t('pages.settings.upload.placeholder.md5-16'), value: '{md5-16}' }, { label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, + { label: t('pages.settings.upload.placeholder.sha1'), value: '{sha1}' }, + { label: t('pages.settings.upload.placeholder.sha1-n'), value: '{sha1-n}' }, { label: t('pages.settings.upload.placeholder.sha256'), value: '{sha256}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, ], diff --git a/src/universal/types/shims-tsx.d.ts b/src/universal/types/shims-tsx.d.ts index 8d8aafec..b2a08ab4 100644 --- a/src/universal/types/shims-tsx.d.ts +++ b/src/universal/types/shims-tsx.d.ts @@ -45,14 +45,14 @@ declare global { } crypto: { randomBytes: typeof crypto.randomBytes - createHash: typeof crypto.createHash + createHash: (algorithm: string, text: string | Buffer) => string } fs: { remove: typeof fs.remove readFile: typeof fs.readFile + readFileSync: typeof fs.readFileSync statSync: typeof fs.statSync } - yaml: { parse: typeof yaml.parseDocument }