Make replaceFilenameForbiddenChars a helper (#2904)

* Make replaceFilenameForbiddenChars a helper

* Use function for screenshot file name checking
This commit is contained in:
absidue 2022-12-02 08:29:01 +01:00 committed by GitHub
parent c98b6ab3f2
commit d793a8bcda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 53 deletions

View File

@ -422,3 +422,36 @@ export function searchFiltersMatch(filtersA, filtersB) {
filtersA?.type === filtersB?.type &&
filtersA?.duration === filtersB?.duration
}
export function replaceFilenameForbiddenChars(filenameOriginal) {
let filenameNew = filenameOriginal
let forbiddenChars = {}
switch (process.platform) {
case 'win32':
forbiddenChars = {
'<': '', // U+FF1C
'>': '', // U+FF1E
':': '', // U+FF1A
'"': '', // U+FF02
'/': '', // U+FF0F
'\\': '', // U+FF3C
'|': '', // U+FF5C
'?': '', // U+FF1F
'*': '' // U+FF0A
}
break
case 'darwin':
forbiddenChars = { '/': '', ':': '' }
break
case 'linux':
forbiddenChars = { '/': '' }
break
default:
break
}
for (const forbiddenChar in forbiddenChars) {
filenameNew = filenameNew.replaceAll(forbiddenChar, forbiddenChars[forbiddenChar])
}
return filenameNew
}

View File

@ -6,6 +6,7 @@ import { IpcChannels } from '../../../constants'
import {
createWebURL,
openExternalLink,
replaceFilenameForbiddenChars,
searchFiltersMatch,
showSaveDialog,
showToast
@ -122,46 +123,13 @@ async function invokeIRC(context, IRCtype, webCbk, payload = null) {
}
const actions = {
replaceFilenameForbiddenChars(_, filenameOriginal) {
let filenameNew = filenameOriginal
let forbiddenChars = {}
switch (process.platform) {
case 'win32':
forbiddenChars = {
'<': '', // U+FF1C
'>': '', // U+FF1E
':': '', // U+FF1A
'"': '', // U+FF02
'/': '', // U+FF0F
'\\': '', // U+FF3C
'|': '', // U+FF5C
'?': '', // U+FF1F
'*': '' // U+FF0A
}
break
case 'darwin':
forbiddenChars = { '/': '', ':': '' }
break
case 'linux':
forbiddenChars = { '/': '' }
break
default:
break
}
for (const forbiddenChar in forbiddenChars) {
filenameNew = filenameNew.replaceAll(forbiddenChar, forbiddenChars[forbiddenChar])
}
return filenameNew
},
async downloadMedia({ rootState, dispatch }, { url, title, extension, fallingBackPath }) {
async downloadMedia({ rootState }, { url, title, extension, fallingBackPath }) {
if (!process.env.IS_ELECTRON) {
openExternalLink(url)
return
}
const fileName = `${await dispatch('replaceFilenameForbiddenChars', title)}.${extension}`
const fileName = `${replaceFilenameForbiddenChars(title)}.${extension}`
const errorMessage = i18n.t('Downloading failed', { videoTitle: title })
let folderPath = rootState.settings.downloadFolderPath
@ -281,27 +249,13 @@ const actions = {
parsedString = parsedString.replaceAll(key, value)
}
const platform = process.platform
if (platform === 'win32') {
// https://www.boost.org/doc/libs/1_78_0/libs/filesystem/doc/portability_guide.htm
// https://stackoverflow.com/questions/1976007/
const noForbiddenChars = ['<', '>', ':', '"', '/', '|', '?', '*'].every(char => {
return parsedString.indexOf(char) === -1
})
if (!noForbiddenChars) {
reject(new Error('Forbidden Characters')) // use message as translation key
}
} else if (platform === 'darwin') {
// https://superuser.com/questions/204287/
if (parsedString.indexOf(':') !== -1) {
reject(new Error('Forbidden Characters'))
}
if (parsedString !== replaceFilenameForbiddenChars(parsedString)) {
reject(new Error('Forbidden Characters')) // use message as translation key
}
const dirChar = platform === 'win32' ? '\\' : '/'
let filename
if (parsedString.indexOf(dirChar) !== -1) {
const lastIndex = parsedString.lastIndexOf(dirChar)
if (parsedString.indexOf(path.sep) !== -1) {
const lastIndex = parsedString.lastIndexOf(path.sep)
filename = parsedString.substring(lastIndex + 1)
} else {
filename = parsedString