Initial settings, download UI, various bugfixes

This commit is contained in:
Geomitron
2020-02-09 21:15:40 -05:00
parent 89948b118b
commit de39ad4f1e
33 changed files with 1034 additions and 110 deletions

View File

@@ -1,7 +1,4 @@
import { basename } from 'path'
import { Settings } from './Settings'
const settings = Settings.getInstance()
let sanitize = require('sanitize-filename')
/**
* @param absoluteFilepath The absolute filepath to a folder
@@ -12,4 +9,48 @@ export function getRelativeFilepath(absoluteFilepath: string) {
// loads everything and connects to the database, etc...)
// return basename(scanSettings.songsFolderPath) + absoluteFilepath.substring(scanSettings.songsFolderPath.length)
return absoluteFilepath
}
/**
* @returns A random UUID
*/
export function generateUUID() { // Public Domain/MIT
var d = new Date().getTime()//Timestamp
var d2 = Date.now() // Time in microseconds since page-load or 0 if unsupported
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16//random number between 0 and 16
if (d > 0) {//Use timestamp until depleted
r = (d + r) % 16 | 0
d = Math.floor(d / 16)
} else {//Use microseconds since page-load if supported
r = (d2 + r) % 16 | 0
d2 = Math.floor(d2 / 16)
}
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
})
}
/**
* Sanitizes a filename of any characters that cannot be part of a windows filename.
* @param filename The name of the file to sanitize.
*/
export function sanitizeFilename(filename: string): string {
const newName = sanitize(filename, {
replacement: ((invalidChar: string) => {
switch (invalidChar) {
case '/': return '-'
case '\\': return '-'
case '"': return "'"
default: return '_' //TODO: add more cases for replacing invalid characters
}
})
})
return newName
}
/**
* Converts <val> from the range (<fromA>, <fromB>) to the range (<toA>, <toB>).
*/
export function interpolate(val: number, fromA: number, fromB: number, toA: number, toB: number) {
return ((val - fromA) / (fromB - fromA)) * (toB - toA) + toA
}