Added better invalid character replacements

This commit is contained in:
Geomitron
2020-05-27 15:19:34 -04:00
parent 323a9116e3
commit 5314d9a7c3
6 changed files with 26 additions and 10 deletions

View File

@@ -2,7 +2,6 @@ import { AnyFunction } from '../../shared/UtilFunctions'
import { createWriteStream } from 'fs'
import * as needle from 'needle'
// TODO: replace needle with got (for cancel() method) (if before-headers event is possible?)
// TODO: add download throttle library and setting
import { googleTimer } from './GoogleTimer'
import { DownloadError } from './ChartDownload'

View File

@@ -1,23 +1,30 @@
import * as randomBytes from 'randombytes'
const sanitize = require('sanitize-filename')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyFunction = (...args: any) => any
/**
* @returns `filename`, but with any invalid filename characters replaced with similar valid characters.
* @returns `filename` with all invalid filename characters replaced.
*/
export function sanitizeFilename(filename: string): string {
const newName = sanitize(filename, {
const newFilename = sanitize(filename, {
replacement: ((invalidChar: string) => {
switch (invalidChar) {
case '/': return '-'
case '\\': return '-'
case '<': return ''
case '>': return ''
case ':': return ''
case '"': return "'"
default: return '_' // TODO: add more cases for replacing invalid characters
case '/': return ''
case '\\': return ''
case '|': return '⏐'
case '?': return ''
case '*': return ''
default: return '_'
}
})
})
return newName
return (newFilename == '' ? randomBytes(5).toString('hex') : newFilename)
}
/**