Feature(custom): support sha1,timestampS, fix rename bug in cloud and gallery

This commit is contained in:
Kuingsmile
2026-04-30 22:26:23 +08:00
parent 97f7cd85d9
commit 5e48e80fb5
12 changed files with 64 additions and 19 deletions

View File

@@ -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,

View File

@@ -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}' },
],

View File

@@ -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"

View File

@@ -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位数"

View File

@@ -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位數"

View File

@@ -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 ?? '',
})
})

View File

@@ -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}' },
],

View File

@@ -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
}

View File

@@ -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(

View File

@@ -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}' },
],
}

View File

@@ -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}' },
],

View File

@@ -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
}