Skip to content

Commit

Permalink
✨ Feature(custom): optimize aws s3 and buildin rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed May 8, 2024
1 parent 8cc07dc commit 9ca0e4b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"multer": "^1.4.5-lts.1",
"node-ssh-no-cpu-features": "^1.0.1",
"nodejs-file-downloader": "^4.12.1",
"piclist": "^1.8.7",
"piclist": "^1.8.8",
"pinia": "^2.1.7",
"pinia-plugin-persistedstate": "^3.2.0",
"proxy-agent": "^5.0.0",
Expand Down
90 changes: 53 additions & 37 deletions src/renderer/manage/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,29 @@ export function renameFileNameWithCustomString (oldName: string, customFormat: s
'{y}': () => year.slice(2),
'{m}': () => renameFormatHelper(date.getMonth() + 1),
'{d}': () => renameFormatHelper(date.getDate()),
'{h}': () => renameFormatHelper(date.getHours()),
'{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),
'{str-10}': () => randomStringGenerator(10),
'{str-20}': () => randomStringGenerator(20),
'{filename}': () => affixFileName ? path.basename(affixFileName, path.extname(affixFileName)) : path.basename(oldName, path.extname(oldName)),
'{uuid}': () => uuidv4().replace(/-/g, ''),
'{timestamp}': () => Math.floor(Date.now() / 1000).toString()
'{timestamp}': () => date.getTime().toString()
}
if (customFormat === undefined || !Object.keys(conversionMap).some(item => customFormat.includes(item))) {
if (customFormat === undefined || (!Object.keys(conversionMap).some(item => customFormat.includes(item)) && !customFormat.includes('{str-'))) {
return oldName
}
const ext = path.extname(oldName)
return Object.keys(conversionMap).reduce((acc, cur) => {
let newName = Object.keys(conversionMap).reduce((acc, cur) => {
return acc.replace(new RegExp(cur, 'g'), conversionMap[cur]())
}, customFormat) + ext
const strRegex = /{str-(\d+)}/gi
newName = newName.replace(strRegex, (_, group1) => {
const length = parseInt(group1, 10)
return randomStringGenerator(length)
})
return newName
}

export function renameFile ({ timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap, oldName = ''): string {
Expand Down Expand Up @@ -217,9 +225,21 @@ export const customRenameFormatTable = [
placeholderB: '{d}',
descriptionB: '日期(01-31)'
},
{
placeholder: '{h}',
description: '小时(00-23)',
placeholderB: '{i}',
descriptionB: '分钟(00-59)'
},
{
placeholder: '{s}',
description: '秒(00-59)',
placeholderB: '{ms}',
descriptionB: '毫秒(000-999)'
},
{
placeholder: '{timestamp}',
description: '时间戳()',
description: '时间戳(毫秒)',
placeholderB: '{uuid}',
descriptionB: 'uuid字符串'
},
Expand All @@ -230,62 +250,58 @@ export const customRenameFormatTable = [
descriptionB: 'md5前16位'
},
{
placeholder: '{str-10}',
placeholder: '{str-number}',
description: '10位随机字符串',
placeholderB: '{str-20}',
descriptionB: '20位随机字符串'
},
{
placeholder: '{filename}',
description: '原文件名'
placeholderB: '{filename}',
descriptionB: '原文件名'
}
]

export const buildInRenameFormatTable = [
{
placeholder: '{Y}',
description: '年份,4位数',
placeholderB: '{y}',
descriptionB: '年份,2位数'
placeholderB: '{timestamp}',
descriptionB: '时间戳(毫秒)'
},
{
placeholder: '{m}',
description: '月份(01-12)',
placeholderB: '{d}',
descriptionB: '日期(01-31)'
placeholder: '{y}',
description: '年份,2位数',
placeholderB: '{md5}',
descriptionB: 'md5'
},
{
placeholder: '{h}',
description: '小时(00-23)',
placeholderB: '{i}',
descriptionB: '分钟(00-59)'
placeholder: '{m}',
description: '月份(01-12)',
placeholderB: '{md5-16}',
descriptionB: 'md5前16位'
},
{
placeholder: '{s}',
description: '秒(00-59)',
placeholder: '{d}',
description: '日期(01-31)',
placeholderB: '{localFolder:<number>}',
descriptionB: '本地文件夹层级'
},
{
placeholder: '{timestamp}',
description: '时间戳(秒)',
placeholder: '{h}',
description: '小时(00-23)',
placeholderB: '{uuid}',
descriptionB: 'uuid字符串'
},
{
placeholder: '{md5}',
description: 'md5',
placeholderB: '{md5-16}',
descriptionB: 'md5前16位'
placeholder: '{i}',
description: '分钟(00-59)',
placeholderB: '{filename}',
descriptionB: '原文件名'
},
{
placeholder: '{str-10}',
description: '10位随机字符串',
placeholderB: '{str-20}',
descriptionB: '20位随机字符串'
placeholder: '{s}',
description: '秒(00-59)',
placeholderB: '{str-number}',
descriptionB: 'number位随机字符串'
},
{
placeholder: '{filename}',
description: '原文件名'
placeholder: '{ms}',
description: '毫秒(000-999)'
}
]
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12397,10 +12397,10 @@ performance-now@^2.1.0:
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==

piclist@^1.8.7:
version "1.8.7"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.8.7.tgz#aa40af42762b857ac0c45a097421278a301320cf"
integrity sha512-asSc588Fh1aMpIq/guqqHGhZb0wsLn+wZllKbtznDasbh4zNZvQECNDxRGVtmvsSYJlR+V+yyA2Z85AW/aQqyA==
piclist@^1.8.8:
version "1.8.8"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.8.8.tgz#a4b3b4bcdd88f48de5469497e140187ea6e46ab7"
integrity sha512-2sucOVbqVNukb0FZ4ne+DG6bzAJIxZJgm1HlOHKk/NOKYvbcXLsHq0whtEsbfwnsFmkF5T9uJtXHwZPE86JoQA==
dependencies:
"@aws-sdk/client-s3" "3.421.0"
"@aws-sdk/lib-storage" "3.421.0"
Expand Down

0 comments on commit 9ca0e4b

Please sign in to comment.