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: { fs: {
remove: fs.remove, remove: fs.remove,
readFile: fs.readFile, readFile: fs.readFile,
readFileSync: fs.readFileSync,
statSync: fs.statSync, statSync: fs.statSync,
}, },
crypto: { crypto: {
randomBytes: crypto.randomBytes, randomBytes: crypto.randomBytes,
createHash: crypto.createHash, createHash: (algorithm: string, text: string | Buffer) => crypto.createHash(algorithm).update(text).digest('hex'),
}, },
yaml: { yaml: {
parse: yaml.parseDocument, 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.second'), value: '{s}' },
{ label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' },
{ label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' },
{ label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' },
], ],
categoryHash: [ categoryHash: [
{ label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { 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.md5-16'), value: '{md5-16}' },
{ label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, { 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'), value: '{sha256}' },
{ label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' },
], ],

View File

@@ -1077,9 +1077,12 @@
"month": "Month, 2 Digits", "month": "Month, 2 Digits",
"randomString": "Random String (number of digits)", "randomString": "Random String (number of digits)",
"second": "Second, 2 Digits", "second": "Second, 2 Digits",
"sha1": "SHA1 Hash",
"sha1-n": "SHA1 Hash (First n Digits)",
"sha256": "SHA256 Hash", "sha256": "SHA256 Hash",
"sha256-n": "SHA256 Hash (First n Digits)", "sha256-n": "SHA256 Hash (First n Digits)",
"timestamp": "Timestamp, 13 Digits", "timestamp": "Timestamp, 13 Digits",
"timestampS": "Timestamp, 10 Digits",
"uuid": "Random UUID", "uuid": "Random UUID",
"year2": "Year, 2 Digits", "year2": "Year, 2 Digits",
"year4": "Year, 4 Digits" "year4": "Year, 4 Digits"

View File

@@ -1077,9 +1077,12 @@
"month": "月份2位数", "month": "月份2位数",
"randomString": "number位随机字符串", "randomString": "number位随机字符串",
"second": "秒2位数", "second": "秒2位数",
"sha1": "SHA1 哈希",
"sha1-n": "SHA1 哈希前n位",
"sha256": "SHA256 哈希", "sha256": "SHA256 哈希",
"sha256-n": "SHA256 哈希前n位", "sha256-n": "SHA256 哈希前n位",
"timestamp": "时间戳13位数", "timestamp": "时间戳13位数",
"timestampS": "时间戳10位数",
"uuid": "随机 UUID", "uuid": "随机 UUID",
"year2": "年份2位数", "year2": "年份2位数",
"year4": "年份4位数" "year4": "年份4位数"

View File

@@ -1077,9 +1077,12 @@
"month": "月份2位數", "month": "月份2位數",
"randomString": "number位隨機字符串", "randomString": "number位隨機字符串",
"second": "秒2位數", "second": "秒2位數",
"sha1": "SHA1 哈希",
"sha1-n": "SHA1 哈希前n位",
"sha256": "SHA256 哈希", "sha256": "SHA256 哈希",
"sha256-n": "SHA256 哈希前n位", "sha256-n": "SHA256 哈希前n位",
"timestamp": "時間戳13位數", "timestamp": "時間戳13位數",
"timestampS": "時間戳10位數",
"uuid": "隨機 UUID", "uuid": "隨機 UUID",
"year2": "年份2位數", "year2": "年份2位數",
"year4": "年份4位數" "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.second'), value: '{s}' },
{ label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' },
{ label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' },
{ label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' },
], ],
categoryHash: [ categoryHash: [
{ label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { 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.md5-16'), value: '{md5-16}' },
{ label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, { 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'), value: '{sha256}' },
{ label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' },
], ],
@@ -1701,15 +1704,16 @@ function clearTableData() {
uploadPanelFilesList.value = [] uploadPanelFilesList.value = []
} }
function renameFileBeforeUpload(filePath: string): string { function renameFileBeforeUpload(filePath: string, fullPath: string): string {
const fileName = window.node.path.basename(filePath) const fileName = window.node.path.basename(filePath)
const fileBuffer = window.node.fs.readFileSync(fullPath)
const typeMap = { const typeMap = {
timestampRename: manageStore.config.settings.timestampRename, timestampRename: manageStore.config.settings.timestampRename,
randomStringRename: manageStore.config.settings.randomStringRename, randomStringRename: manageStore.config.settings.randomStringRename,
customRenameFormat: manageStore.config.settings.customRenameFormat, customRenameFormat: manageStore.config.settings.customRenameFormat,
customRename: manageStore.config.settings.customRename, customRename: manageStore.config.settings.customRename,
} }
return renameFile(typeMap, fileName) return renameFile(typeMap, fileName, fileBuffer)
} }
function uploadFiles() { function uploadFiles() {
@@ -1719,7 +1723,7 @@ function uploadFiles() {
rawName: item.name, rawName: item.name,
path: item.path.replace(/\\/g, '/'), path: item.path.replace(/\\/g, '/'),
size: item.size, size: item.size,
renamedFileName: renameFileBeforeUpload(item.name), renamedFileName: renameFileBeforeUpload(item.name, item.path),
relativePath: item.relativePath ?? '', 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.second'), value: '{s}' },
{ label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' },
{ label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' },
{ label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' },
], ],
categoryHash: [ categoryHash: [
{ label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { 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.md5-16'), value: '{md5-16}' },
{ label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, { 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'), value: '{sha256}' },
{ label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, { 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() return num.toString().length === 1 ? `0${num}` : num.toString()
} }
function getMd5(input: any): string { function getMd5(input: Buffer | string): string {
return window.node.crypto.createHash('md5').update(input).digest('hex') return window.node.crypto.createHash('md5', input)
} }
function getSha256(input: any): string { function getSha256(input: Buffer | string): string {
return window.node.crypto.createHash('sha256').update(input).digest('hex') 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 date = new Date()
const year = date.getFullYear().toString() const year = date.getFullYear().toString()
const fileBaseName = window.node.path.basename(oldName, window.node.path.extname(oldName)) 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()), '{i}': () => renameFormatHelper(date.getMinutes()),
'{s}': () => renameFormatHelper(date.getSeconds()), '{s}': () => renameFormatHelper(date.getSeconds()),
'{ms}': () => date.getMilliseconds().toString().padStart(3, '0'), '{ms}': () => date.getMilliseconds().toString().padStart(3, '0'),
'{md5}': () => getMd5(fileBaseName), '{md5}': () => getMd5(fileBuffer || fileBaseName),
'{md5-16}': () => getMd5(fileBaseName).slice(0, 16), '{md5-16}': () => getMd5(fileBuffer || fileBaseName).slice(0, 16),
'{sha256}': () => getSha256(fileBaseName), '{sha1}': () => getSha1(fileBuffer || fileBaseName),
'{sha256}': () => getSha256(fileBuffer || fileBaseName),
'{filename}': () => '{filename}': () =>
affixFileName affixFileName
? window.node.path.basename(affixFileName, window.node.path.extname(affixFileName)) ? window.node.path.basename(affixFileName, window.node.path.extname(affixFileName))
: window.node.path.basename(oldName, window.node.path.extname(oldName)), : window.node.path.basename(oldName, window.node.path.extname(oldName)),
'{uuid}': () => uuidv4().replace(/-/g, ''), '{uuid}': () => uuidv4().replace(/-/g, ''),
'{timestamp}': () => date.getTime().toString(), '{timestamp}': () => date.getTime().toString(),
'{timestampS}': () => Math.floor(date.getTime() / 1000).toString(),
} }
if ( if (
customFormat === undefined || 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 return oldName
} }
@@ -92,9 +106,14 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st
}, customFormat) + ext }, customFormat) + ext
const strRegex = /{str-(\d+)}/gi const strRegex = /{str-(\d+)}/gi
const sha256nRegex = /{sha256-(\d+)}/gi const sha256nRegex = /{sha256-(\d+)}/gi
const sha1nRegex = /{sha1-(\d+)}/gi
newName = newName.replace(sha256nRegex, (_, group1) => { newName = newName.replace(sha256nRegex, (_, group1) => {
const length = parseInt(group1, 10) 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) => { newName = newName.replace(strRegex, (_, group1) => {
const length = parseInt(group1, 10) const length = parseInt(group1, 10)
@@ -106,6 +125,7 @@ export function renameFileNameWithCustomString(oldName: string, customFormat: st
export function renameFile( export function renameFile(
{ timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap, { timestampRename, randomStringRename, customRename, customRenameFormat }: IStringKeyMap,
oldName = '', oldName = '',
fileBuffer?: Buffer,
): string { ): string {
switch (true) { switch (true) {
case timestampRename: case timestampRename:
@@ -113,7 +133,7 @@ export function renameFile(
case randomStringRename: case randomStringRename:
return renameFileNameWithRandomString(oldName, 20) return renameFileNameWithRandomString(oldName, 20)
case customRename: case customRename:
return renameFileNameWithCustomString(oldName, customRenameFormat) return renameFileNameWithCustomString(oldName, customRenameFormat, undefined, fileBuffer)
default: default:
return oldName return oldName
} }

View File

@@ -3,7 +3,7 @@ let NC = 0
const NC_PAD = '00000000' const NC_PAD = '00000000'
function md5(text: any) { function md5(text: any) {
return window.node.crypto.createHash('md5').update(text).digest('hex') return window.node.crypto.createHash('md5', text)
} }
export function digestAuthHeader( 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.second'), value: '{s}' },
{ label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' },
{ label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' },
{ label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' },
], ],
categoryHash: [ categoryHash: [
{ label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { 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.md5-16'), value: '{md5-16}' },
{ label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, { 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'), value: '{sha256}' },
{ label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' },
], ],
categoryFile: [ categoryFile: [
{ label: t('pages.settings.upload.placeholder.filename'), value: '{filename}' }, { 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}' }, { 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.second'), value: '{s}' },
{ label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' }, { label: t('pages.settings.upload.placeholder.millisecond'), value: '{ms}' },
{ label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' }, { label: t('pages.settings.upload.placeholder.timestamp'), value: '{timestamp}' },
{ label: t('pages.settings.upload.placeholder.timestampS'), value: '{timestampS}' },
], ],
categoryHash: [ categoryHash: [
{ label: t('pages.settings.upload.placeholder.md5'), value: '{md5}' }, { 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.md5-16'), value: '{md5-16}' },
{ label: t('pages.settings.upload.placeholder.uuid'), value: '{uuid}' }, { 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'), value: '{sha256}' },
{ label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' }, { label: t('pages.settings.upload.placeholder.sha256-n'), value: '{sha256-n}' },
], ],

View File

@@ -45,14 +45,14 @@ declare global {
} }
crypto: { crypto: {
randomBytes: typeof crypto.randomBytes randomBytes: typeof crypto.randomBytes
createHash: typeof crypto.createHash createHash: (algorithm: string, text: string | Buffer) => string
} }
fs: { fs: {
remove: typeof fs.remove remove: typeof fs.remove
readFile: typeof fs.readFile readFile: typeof fs.readFile
readFileSync: typeof fs.readFileSync
statSync: typeof fs.statSync statSync: typeof fs.statSync
} }
yaml: { yaml: {
parse: typeof yaml.parseDocument parse: typeof yaml.parseDocument
} }