Files
Bridge-Multi/src-electron/ElectronUtilFunctions.ts
2024-07-09 19:50:44 -05:00

54 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { randomBytes } from 'crypto'
import { basename, parse } from 'path'
import sanitize from 'sanitize-filename'
import { inspect } from 'util'
import { lower } from '../src-shared/UtilFunctions.js'
import { settings } from './ipc/SettingsHandler.ipc.js'
import { emitIpcEvent } from './main.js'
/**
* @returns The relative filepath from the library folder to `absoluteFilepath`.
*/
export async function getRelativeFilepath(absoluteFilepath: string) {
if (!settings.libraryPath) { throw 'getRelativeFilepath() failed; libraryPath is undefined' }
return basename(settings.libraryPath) + absoluteFilepath.substring(settings.libraryPath.length)
}
/**
* @returns `true` if `name` has a valid video file extension.
*/
export function hasVideoExtension(name: string) {
return (['.mp4', '.avi', '.webm', '.ogv', '.mpeg'].includes(parse(lower(name)).ext))
}
/**
* Log a message in the main BrowserWindow's console.
*/
export function devLog(message: unknown) {
emitIpcEvent('errorLog', typeof message === 'string' ? message : inspect(message))
}
/**
* @returns `filename` with all invalid filename characters replaced.
*/
export function sanitizeFilename(filename: string): string {
const newFilename = sanitize(filename, {
replacement: ((invalidChar: string) => {
switch (invalidChar) {
case '<': return ''
case '>': return ''
case ':': return ''
case '"': return "'"
case '/': return ''
case '\\': return ''
case '|': return '⏐'
case '?': return ''
case '*': return ''
default: return '_'
}
}),
})
return (newFilename === '' ? randomBytes(5).toString('hex') : newFilename)
}