mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-04-11 14:19:38 +00:00
Initial settings, download UI, various bugfixes
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { IPCHandler } from '../shared/IPCHandler'
|
||||
import { IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import Database from '../shared/Database'
|
||||
import { AlbumArtResult } from '../shared/interfaces/songDetails.interface'
|
||||
|
||||
export default class AlbumArtHandler implements IPCHandler<'album-art'> {
|
||||
export default class AlbumArtHandler implements IPCInvokeHandler<'album-art'> {
|
||||
event: 'album-art' = 'album-art'
|
||||
// TODO: add method documentation
|
||||
|
||||
|
||||
52
src/electron/ipc/InitSettingsHandler.ipc.ts
Normal file
52
src/electron/ipc/InitSettingsHandler.ipc.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { exists as _exists, mkdir as _mkdir, readFile as _readFile, writeFile as _writeFile } from 'fs'
|
||||
import { dataPath, tempPath, themesPath, settingsPath } from '../shared/Paths'
|
||||
import { promisify } from 'util'
|
||||
import { IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import { defaultSettings, Settings } from '../shared/Settings'
|
||||
|
||||
const exists = promisify(_exists)
|
||||
const mkdir = promisify(_mkdir)
|
||||
const readFile = promisify(_readFile)
|
||||
const writeFile = promisify(_writeFile)
|
||||
|
||||
export default class InitSettingsHandler implements IPCInvokeHandler<'init-settings'> {
|
||||
event: 'init-settings' = 'init-settings'
|
||||
|
||||
private static settings: Settings
|
||||
static async getSettings() {
|
||||
if (this.settings == undefined) {
|
||||
this.settings = await InitSettingsHandler.initSettings()
|
||||
}
|
||||
|
||||
return this.settings
|
||||
}
|
||||
|
||||
async handler() {
|
||||
return InitSettingsHandler.getSettings()
|
||||
}
|
||||
|
||||
private static async initSettings(): Promise<Settings> {
|
||||
try {
|
||||
// Create data directories if they don't exists
|
||||
for (const path of [dataPath, tempPath, themesPath]) {
|
||||
if (!await exists(path)) {
|
||||
await mkdir(path)
|
||||
}
|
||||
}
|
||||
|
||||
// Read/create settings
|
||||
if (await exists(settingsPath)) {
|
||||
return JSON.parse(await readFile(settingsPath, 'utf8'))
|
||||
} else {
|
||||
const newSettings = JSON.stringify(defaultSettings, undefined, 2)
|
||||
await writeFile(settingsPath, newSettings, 'utf8')
|
||||
return defaultSettings
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to initialize settings!')
|
||||
console.error('Several actions (including downloading) will unexpectedly fail')
|
||||
console.error(e)
|
||||
return defaultSettings
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { IPCHandler } from '../shared/IPCHandler'
|
||||
import { IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import Database from '../shared/Database'
|
||||
import { SongSearch, SearchType, SongResult } from '../shared/interfaces/search.interface'
|
||||
import { escape } from 'mysql'
|
||||
|
||||
export default class SearchHandler implements IPCHandler<'song-search'> {
|
||||
export default class SearchHandler implements IPCInvokeHandler<'song-search'> {
|
||||
event: 'song-search' = 'song-search'
|
||||
// TODO: add method documentation
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IPCHandler } from '../shared/IPCHandler'
|
||||
import { IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import Database from '../shared/Database'
|
||||
import { VersionResult } from '../shared/interfaces/songDetails.interface'
|
||||
|
||||
export default class SongDetailsHandler implements IPCHandler<'song-details'> {
|
||||
export default class SongDetailsHandler implements IPCInvokeHandler<'song-details'> {
|
||||
event: 'song-details' = 'song-details'
|
||||
// TODO: add method documentation
|
||||
|
||||
|
||||
153
src/electron/ipc/download/AddDownloadHandler.ts
Normal file
153
src/electron/ipc/download/AddDownloadHandler.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { FileDownloader } from './FileDownloader'
|
||||
import { IPCEmitHandler } from '../../shared/IPCHandler'
|
||||
import { createHash, randomBytes as _randomBytes } from 'crypto'
|
||||
import { tempPath } from '../../shared/Paths'
|
||||
import { promisify } from 'util'
|
||||
import { join } from 'path'
|
||||
import { Download, NewDownload } from '../../shared/interfaces/download.interface'
|
||||
import { emitIPCEvent } from '../../main'
|
||||
import { mkdir as _mkdir } from 'fs'
|
||||
import { FileExtractor } from './FileExtractor'
|
||||
import { sanitizeFilename, interpolate } from '../../shared/UtilFunctions'
|
||||
|
||||
const randomBytes = promisify(_randomBytes)
|
||||
const mkdir = promisify(_mkdir)
|
||||
|
||||
export class AddDownloadHandler implements IPCEmitHandler<'add-download'> {
|
||||
event: 'add-download' = 'add-download'
|
||||
|
||||
//TODO: update percent in a way that makes its progress seem as smooth as possible
|
||||
|
||||
async handler(data: NewDownload) {
|
||||
const download: Download = {
|
||||
versionID: data.versionID,
|
||||
title: `${data.avTagName} - ${data.artist}`,
|
||||
header: '',
|
||||
description: '',
|
||||
percent: 0
|
||||
}
|
||||
const randomString = (await randomBytes(5)).toString('hex')
|
||||
const chartPath = join(tempPath, `chart_${randomString}`)
|
||||
await mkdir(chartPath)
|
||||
|
||||
let allFilesProgress = 0
|
||||
// Only iterate over the keys in data.links that have link values (not hashes)
|
||||
const fileKeys = Object.keys(data.links).filter(link => data.links[link].includes('.'))
|
||||
const individualFileProgressPortion = 80 / fileKeys.length
|
||||
for (let i = 0; i < fileKeys.length; i++) {
|
||||
const typeHash = createHash('md5').update(data.links[fileKeys[i]]).digest('hex')
|
||||
const downloader = new FileDownloader(data.links[fileKeys[i]], chartPath, data.links[typeHash])
|
||||
let fileProgress = 0
|
||||
|
||||
let waitTime: number
|
||||
downloader.on('wait', (_waitTime) => {
|
||||
download.header = `[${fileKeys[i]}] (file ${i + 1}/${fileKeys.length})`
|
||||
download.description = `Waiting for Google rate limit... (${_waitTime}s)`
|
||||
waitTime = _waitTime
|
||||
})
|
||||
|
||||
downloader.on('waitProgress', (secondsRemaining) => {
|
||||
download.description = `Waiting for Google rate limit... (${secondsRemaining}s)`
|
||||
fileProgress = interpolate(secondsRemaining, waitTime, 0, 0, individualFileProgressPortion / 2)
|
||||
download.percent = allFilesProgress + fileProgress
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
downloader.on('request', () => {
|
||||
download.description = `Sending request...`
|
||||
fileProgress = individualFileProgressPortion / 2
|
||||
download.percent = allFilesProgress + fileProgress
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
downloader.on('warning', (continueAnyway) => {
|
||||
download.description = 'WARNING'
|
||||
emitIPCEvent('download-updated', download)
|
||||
//TODO: continue anyway
|
||||
})
|
||||
|
||||
let filesize = -1
|
||||
downloader.on('download', (filename, _filesize) => {
|
||||
download.header = `[${filename}] (file ${i + 1}/${fileKeys.length})`
|
||||
if (_filesize != undefined) {
|
||||
filesize = _filesize
|
||||
download.description = `Downloading... (0%)`
|
||||
} else {
|
||||
download.description = `Downloading... (0 MB)`
|
||||
}
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
downloader.on('downloadProgress', (bytesDownloaded) => {
|
||||
if (filesize != -1) {
|
||||
download.description = `Downloading... (${Math.round(1000 * bytesDownloaded / filesize) / 10}%)`
|
||||
fileProgress = interpolate(bytesDownloaded, 0, filesize, individualFileProgressPortion / 2, individualFileProgressPortion)
|
||||
download.percent = allFilesProgress + fileProgress
|
||||
} else {
|
||||
download.description = `Downloading... (${Math.round(bytesDownloaded / 1e+5) / 10} MB)`
|
||||
download.percent = allFilesProgress + fileProgress
|
||||
}
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
downloader.on('error', (error, retry) => {
|
||||
download.header = error.header
|
||||
download.description = error.body
|
||||
emitIPCEvent('download-updated', download)
|
||||
// TODO: retry
|
||||
})
|
||||
|
||||
// Wait for the 'complete' event before moving on to another file download
|
||||
await new Promise<void>(resolve => {
|
||||
downloader.on('complete', () => {
|
||||
emitIPCEvent('download-updated', download)
|
||||
allFilesProgress += individualFileProgressPortion
|
||||
resolve()
|
||||
})
|
||||
|
||||
downloader.beginDownload()
|
||||
})
|
||||
}
|
||||
|
||||
const destinationFolderName = sanitizeFilename(`${data.artist} - ${data.avTagName} (${data.charter})`)
|
||||
const extractor = new FileExtractor(chartPath, fileKeys.includes('archive'), destinationFolderName)
|
||||
|
||||
let archive = ''
|
||||
extractor.on('extract', (filename) => {
|
||||
archive = filename
|
||||
download.header = `[${archive}]`
|
||||
download.description = `Extracting...`
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
extractor.on('extractProgress', (percent, filecount) => {
|
||||
download.header = `[${archive}] (${filecount} file${filecount == 1 ? '' : 's'} extracted)`
|
||||
download.description = `Extracting... (${percent}%)`
|
||||
download.percent = interpolate(percent, 0, 100, 80, 95)
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
extractor.on('transfer', (filepath) => {
|
||||
download.header = `Moving files to library folder...`
|
||||
download.description = filepath
|
||||
download.percent = 95
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
extractor.on('complete', (filepath) => {
|
||||
download.header = `Download complete.`
|
||||
download.description = filepath
|
||||
download.percent = 100
|
||||
emitIPCEvent('download-updated', download)
|
||||
})
|
||||
|
||||
extractor.on('error', (error, retry) => {
|
||||
download.header = error.header
|
||||
download.description = error.body
|
||||
emitIPCEvent('download-updated', download)
|
||||
// TODO: retry
|
||||
})
|
||||
|
||||
extractor.beginExtract()
|
||||
}
|
||||
}
|
||||
223
src/electron/ipc/download/FileDownloader.ts
Normal file
223
src/electron/ipc/download/FileDownloader.ts
Normal file
@@ -0,0 +1,223 @@
|
||||
import { generateUUID, sanitizeFilename } from '../../shared/UtilFunctions'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as needle from 'needle'
|
||||
import InitSettingsHandler from '../InitSettingsHandler.ipc'
|
||||
type EventCallback = {
|
||||
'wait': (waitTime: number) => void
|
||||
'waitProgress': (secondsRemaining: number) => void
|
||||
'request': () => void
|
||||
'warning': (continueAnyway: () => void) => void
|
||||
'download': (filename: string, filesize?: number) => void
|
||||
'downloadProgress': (bytesDownloaded: number) => void
|
||||
'complete': () => void
|
||||
'error': (error: DownloadError, retry: () => void) => void
|
||||
}
|
||||
type Callbacks = { [E in keyof EventCallback]: EventCallback[E] }
|
||||
|
||||
export type DownloadError = { header: string, body: string }
|
||||
|
||||
export class FileDownloader {
|
||||
private RATE_LIMIT_DELAY: number
|
||||
private readonly RETRY_MAX = 3
|
||||
private static waitTime = 0
|
||||
private static clock: NodeJS.Timer
|
||||
|
||||
private callbacks = {} as Callbacks
|
||||
private retryCount: number
|
||||
|
||||
constructor(private url: string, private destinationFolder: string, private expectedHash?: string) {
|
||||
if (FileDownloader.clock == undefined) {
|
||||
FileDownloader.clock = setInterval(() => FileDownloader.waitTime = Math.max(0, FileDownloader.waitTime - 1), 1000)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls <callback> when <event> fires.
|
||||
* @param event The event to listen for.
|
||||
* @param callback The function to be called when the event fires.
|
||||
*/
|
||||
on<E extends keyof EventCallback>(event: E, callback: EventCallback[E]) {
|
||||
this.callbacks[event] = callback
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait RATE_LIMIT_DELAY seconds between each download,
|
||||
* then download the file.
|
||||
*/
|
||||
async beginDownload() {
|
||||
const settings = await InitSettingsHandler.getSettings()
|
||||
if (settings.libraryPath == undefined) {
|
||||
this.callbacks.error({header: 'Library folder not specified', body: 'Please go to the settings to set your library folder.'}, () => this.beginDownload())
|
||||
return
|
||||
}
|
||||
this.RATE_LIMIT_DELAY = (await InitSettingsHandler.getSettings()).rateLimitDelay
|
||||
let waitTime = FileDownloader.waitTime
|
||||
if (this.url.toLocaleLowerCase().includes('google')) {
|
||||
FileDownloader.waitTime += this.RATE_LIMIT_DELAY
|
||||
} else {
|
||||
waitTime = 0 // Don't rate limit if not downloading from Google
|
||||
}
|
||||
this.callbacks.wait(waitTime)
|
||||
const clock = setInterval(() => {
|
||||
waitTime--
|
||||
this.callbacks.waitProgress(waitTime)
|
||||
if (waitTime <= 0) {
|
||||
this.retryCount = 0
|
||||
this.requestDownload()
|
||||
clearInterval(clock)
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to download the file at <this.url>.
|
||||
* @param cookieHeader the "cookie=" header to include this request.
|
||||
*/
|
||||
private requestDownload(cookieHeader?: string) {
|
||||
this.callbacks.request()
|
||||
let uuid = generateUUID()
|
||||
const req = needle.get(this.url, {
|
||||
follow_max: 10,
|
||||
headers: Object.assign({
|
||||
'User-Agent': 'PostmanRuntime/7.22.0',
|
||||
'Referer': this.url,
|
||||
'Accept': '*/*',
|
||||
'Postman-Token': uuid
|
||||
},
|
||||
(cookieHeader ? { 'Cookie': cookieHeader } : undefined)
|
||||
)
|
||||
})
|
||||
|
||||
req.on('timeout', (type: string) => {
|
||||
this.retryCount++
|
||||
if (this.retryCount <= this.RETRY_MAX) {
|
||||
console.log(`TIMEOUT: Retry attempt ${this.retryCount}...`)
|
||||
this.requestDownload(cookieHeader)
|
||||
} else {
|
||||
this.callbacks.error({ header: 'Timeout', body: `The download server could not be reached. (type=${type})` }, () => this.beginDownload())
|
||||
}
|
||||
})
|
||||
|
||||
req.on('err', (err) => {
|
||||
// TODO: this is called on timeout; if there are other cases where this can fail, they should be printed correctly
|
||||
// this.callbacks.error({ header: 'Error', description: `${err}` }, () => this.beginDownload())
|
||||
})
|
||||
|
||||
req.on('header', (statusCode, headers: Headers) => {
|
||||
if (statusCode != 200) {
|
||||
this.callbacks.error({ header: 'Connection failed', body: `Server returned status code: ${statusCode}` }, () => this.beginDownload())
|
||||
return
|
||||
}
|
||||
|
||||
let fileType = headers['content-type']
|
||||
if (fileType.startsWith('text/html')) {
|
||||
this.handleHTMLResponse(req, headers['set-cookie'])
|
||||
} else {
|
||||
const fileName = this.getDownloadFileName(headers)
|
||||
const downloadHash = this.getDownloadHash(headers)
|
||||
if (this.expectedHash !== undefined && downloadHash !== this.expectedHash) {
|
||||
this.callbacks.warning(() => {
|
||||
//TODO: check if this will actually work (or will the data get lost in the time before the button is clicked?)
|
||||
// Maybe show the message at the end, and ask if they want to keep it.
|
||||
this.handleDownloadResponse(req, fileName, headers['content-length'])
|
||||
})
|
||||
} else {
|
||||
this.handleDownloadResponse(req, fileName, headers['content-length'])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* A Google Drive HTML response to a download request means this is the "file too large to scan for viruses" warning.
|
||||
* This function sends the request that results from clicking "download anyway".
|
||||
* @param req The download request.
|
||||
* @param cookieHeader The "cookie=" header of this request.
|
||||
*/
|
||||
private handleHTMLResponse(req: NodeJS.ReadableStream, cookieHeader: string) {
|
||||
let virusScanHTML = ''
|
||||
req.on('data', data => virusScanHTML += data)
|
||||
req.on('done', (err: Error) => {
|
||||
if (!err) {
|
||||
const confirmTokenRegex = /confirm=([0-9A-Za-z\-_]+)&/g
|
||||
const confirmTokenResults = confirmTokenRegex.exec(virusScanHTML)
|
||||
if (confirmTokenResults != null) {
|
||||
const confirmToken = confirmTokenResults[1]
|
||||
const downloadID = this.url.substr(this.url.indexOf('id=') + 'id='.length)
|
||||
this.url = `https://drive.google.com/uc?confirm=${confirmToken}&id=${downloadID}`
|
||||
const warningCode = /download_warning_([^=]*)=/.exec(cookieHeader)[1]
|
||||
const NID = /NID=([^;]*);/.exec(cookieHeader)[1].replace('=', '%')
|
||||
const newHeader = `download_warning_${warningCode}=${confirmToken}; NID=${NID}`
|
||||
this.requestDownload(newHeader)
|
||||
} else {
|
||||
this.callbacks.error({ header: 'Invalid response', body: 'Download server returned HTML instead of a file.' }, () => this.beginDownload())
|
||||
}
|
||||
} else {
|
||||
this.callbacks.error({ header: 'Connection Failed', body: `Connection failed while downloading HTML: ${err.name}` }, () => this.beginDownload())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Pipes the data from a download response to <filename> and extracts it if <isArchive> is true.
|
||||
* @param req The download request.
|
||||
* @param fileName The name of the output file.
|
||||
* @param contentLength The number of bytes to be downloaded.
|
||||
*/
|
||||
private handleDownloadResponse(req: NodeJS.ReadableStream, fileName: string, contentLength?: number) {
|
||||
this.callbacks.download(fileName, contentLength)
|
||||
let downloadedSize = 0
|
||||
const filePath = path.join(this.destinationFolder, fileName)
|
||||
req.pipe(fs.createWriteStream(filePath))
|
||||
req.on('data', (data) => {
|
||||
downloadedSize += data.length
|
||||
this.callbacks.downloadProgress(downloadedSize)
|
||||
})
|
||||
|
||||
req.on('err', (err: Error) => {
|
||||
this.callbacks.error({ header: 'Connection Failed', body: `Connection failed while downloading file: ${err.name}` }, () => this.beginDownload())
|
||||
})
|
||||
|
||||
req.on('end', () => {
|
||||
this.callbacks.complete()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the downloaded file's filename from <headers> or <url>, depending on the file's host server.
|
||||
* @param url The URL of this request.
|
||||
* @param headers The response headers for this request.
|
||||
*/
|
||||
private getDownloadFileName(headers: Headers) {
|
||||
if (headers['server'] && headers['server'] == 'cloudflare' || this.url.startsWith('https://public.fightthe.pw/')) {
|
||||
// Cloudflare and Chorus specific jazz
|
||||
return sanitizeFilename(decodeURIComponent(path.basename(this.url)))
|
||||
} else {
|
||||
// GDrive specific jazz
|
||||
const filenameRegex = /filename="(.*?)"/g
|
||||
let results = filenameRegex.exec(headers['content-disposition'])
|
||||
if (results == null) {
|
||||
console.log(`Warning: couldn't find filename in content-disposition header: [${headers['content-disposition']}]`)
|
||||
return 'unknownFilename'
|
||||
} else {
|
||||
return sanitizeFilename(results[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the downloaded file's hash from <headers>, depending on the file's host server.
|
||||
* @param url The URL of the request.
|
||||
* @param headers The response headers for this request.
|
||||
*/
|
||||
private getDownloadHash(headers: Headers): string {
|
||||
if (headers['server'] && headers['server'] == 'cloudflare' || this.url.startsWith('https://public.fightthe.pw/')) {
|
||||
// Cloudflare and Chorus specific jazz
|
||||
return String(headers['content-length']) // No good hash is provided in the header, so this is the next best thing
|
||||
} else {
|
||||
// GDrive specific jazz
|
||||
return headers['x-goog-hash']
|
||||
}
|
||||
}
|
||||
}
|
||||
136
src/electron/ipc/download/FileExtractor.ts
Normal file
136
src/electron/ipc/download/FileExtractor.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import { DownloadError } from './FileDownloader'
|
||||
import { readdir as _readdir, unlink as _unlink, lstat as _lstat, copyFile as _copyFile,
|
||||
rmdir as _rmdir, access as _access, mkdir as _mkdir, constants } from 'fs'
|
||||
import { promisify } from 'util'
|
||||
import { join, extname } from 'path'
|
||||
import * as node7z from 'node-7z'
|
||||
import * as zipBin from '7zip-bin'
|
||||
import * as unrarjs from 'node-unrar-js'
|
||||
import InitSettingsHandler from '../InitSettingsHandler.ipc'
|
||||
|
||||
const readdir = promisify(_readdir)
|
||||
const unlink = promisify(_unlink)
|
||||
const lstat = promisify(_lstat)
|
||||
const copyFile = promisify(_copyFile)
|
||||
const rmdir = promisify(_rmdir)
|
||||
const access = promisify(_access)
|
||||
const mkdir = promisify(_mkdir)
|
||||
|
||||
type EventCallback = {
|
||||
'extract': (filename: string) => void
|
||||
'extractProgress': (percent: number, fileCount: number) => void
|
||||
'transfer': (filepath: string) => void
|
||||
'complete': (filepath: string) => void
|
||||
'error': (error: DownloadError, retry: () => void) => void
|
||||
}
|
||||
type Callbacks = { [E in keyof EventCallback]: EventCallback[E] }
|
||||
|
||||
export class FileExtractor {
|
||||
|
||||
private callbacks = {} as Callbacks
|
||||
private libraryFolder: string
|
||||
constructor(private sourceFolder: string, private isArchive: boolean, private destinationFolderName: string) { }
|
||||
|
||||
/**
|
||||
* Calls <callback> when <event> fires.
|
||||
* @param event The event to listen for.
|
||||
* @param callback The function to be called when the event fires.
|
||||
*/
|
||||
on<E extends keyof EventCallback>(event: E, callback: EventCallback[E]) {
|
||||
this.callbacks[event] = callback
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the chart extraction process.
|
||||
*/
|
||||
async beginExtract() {
|
||||
this.libraryFolder = (await InitSettingsHandler.getSettings()).libraryPath
|
||||
const files = await readdir(this.sourceFolder)
|
||||
if (this.isArchive) {
|
||||
this.extract(files[0])
|
||||
} else {
|
||||
this.transfer()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the file at <filename> to <this.sourceFolder>.
|
||||
* @param filename The name of the archive file.
|
||||
*/
|
||||
private extract(filename: string) {
|
||||
this.callbacks.extract(filename)
|
||||
const source = join(this.sourceFolder, filename)
|
||||
|
||||
if (extname(filename) == '.rar') {
|
||||
// Use node-unrar-js to extract the archive
|
||||
try {
|
||||
let extractor = unrarjs.createExtractorFromFile(source, this.sourceFolder)
|
||||
extractor.extractAll()
|
||||
} catch (err) {
|
||||
this.callbacks.error({ header: 'Extract Failed.', body: `Unable to extract [${filename}]: ${err.name}` }, () => this.extract(filename))
|
||||
return
|
||||
}
|
||||
this.transfer(source)
|
||||
} else {
|
||||
// Use node-7z to extract the archive
|
||||
const stream = node7z.extractFull(source, this.sourceFolder, { $progress: true, $bin: zipBin.path7za })
|
||||
|
||||
stream.on('progress', (progress: { percent: number, fileCount: number }) => {
|
||||
this.callbacks.extractProgress(progress.percent, progress.fileCount)
|
||||
})
|
||||
|
||||
stream.on('error', (err: Error) => {
|
||||
this.callbacks.error({ header: 'Extract Failed.', body: `Unable to extract [${filename}]: ${err.name}` }, () => this.extract(filename))
|
||||
})
|
||||
|
||||
stream.on('end', () => {
|
||||
this.transfer(source)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the archive at <archiveFilepath>, then transfers the extracted chart to <this.libraryFolder>.
|
||||
*/
|
||||
private async transfer(archiveFilepath?: string) {
|
||||
try {
|
||||
|
||||
// Create destiniation folder if it doesn't exist
|
||||
const destinationFolder = join(this.libraryFolder, this.destinationFolderName)
|
||||
this.callbacks.transfer(destinationFolder)
|
||||
try {
|
||||
await access(destinationFolder, constants.F_OK)
|
||||
} catch (e) {
|
||||
await mkdir(destinationFolder)
|
||||
}
|
||||
|
||||
// Delete archive
|
||||
if (archiveFilepath != undefined) {
|
||||
await unlink(archiveFilepath)
|
||||
}
|
||||
|
||||
// Check if it extracted to a folder instead of a list of files
|
||||
let files = await readdir(this.sourceFolder)
|
||||
const isFolderArchive = (files.length < 2 && !(await lstat(join(this.sourceFolder, files[0]))).isFile())
|
||||
if (isFolderArchive) {
|
||||
this.sourceFolder = join(this.sourceFolder, files[0])
|
||||
files = await readdir(this.sourceFolder)
|
||||
}
|
||||
|
||||
// Copy the files from the temporary directory to the destination
|
||||
for (const file of files) {
|
||||
await copyFile(join(this.sourceFolder, file), join(destinationFolder, file))
|
||||
await unlink(join(this.sourceFolder, file))
|
||||
}
|
||||
|
||||
// Delete the temporary folders
|
||||
await rmdir(this.sourceFolder)
|
||||
if (isFolderArchive) {
|
||||
await rmdir(join(this.sourceFolder, '..'))
|
||||
}
|
||||
this.callbacks.complete(destinationFolder)
|
||||
} catch (e) {
|
||||
this.callbacks.error({ header: 'Transfer Failed', body: `Unable to transfer downloaded files to the library folder: ${e.name}` }, undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import * as path from 'path'
|
||||
import * as url from 'url'
|
||||
|
||||
// IPC Handlers
|
||||
import { getIPCHandlers } from './shared/IPCHandler'
|
||||
import { getIPCInvokeHandlers, getIPCEmitHandlers, IPCEmitEvents } from './shared/IPCHandler'
|
||||
import Database from './shared/Database'
|
||||
|
||||
let mainWindow: BrowserWindow
|
||||
@@ -61,7 +61,8 @@ function createBridgeWindow() {
|
||||
mainWindow.setMenu(null)
|
||||
|
||||
// IPC handlers
|
||||
getIPCHandlers().map(handler => ipcMain.handle(handler.event, (_event, ...args) => handler.handler(args[0])))
|
||||
getIPCInvokeHandlers().map(handler => ipcMain.handle(handler.event, (_event, ...args) => handler.handler(args[0])))
|
||||
getIPCEmitHandlers().map(handler => ipcMain.on(handler.event, (_event, ...args) => handler.handler(args[0])))
|
||||
|
||||
// Load angular app
|
||||
mainWindow.loadURL(getLoadUrl())
|
||||
@@ -119,4 +120,8 @@ function setUpDevTools() {
|
||||
})
|
||||
|
||||
mainWindow.webContents.openDevTools()
|
||||
}
|
||||
|
||||
export function emitIPCEvent<E extends keyof IPCEmitEvents>(event: E, data: IPCEmitEvents[E]) {
|
||||
mainWindow.webContents.send(event, data)
|
||||
}
|
||||
@@ -3,6 +3,10 @@ import { VersionResult, AlbumArtResult } from './interfaces/songDetails.interfac
|
||||
import SearchHandler from '../ipc/SearchHandler.ipc'
|
||||
import SongDetailsHandler from '../ipc/SongDetailsHandler.ipc'
|
||||
import AlbumArtHandler from '../ipc/AlbumArtHandler.ipc'
|
||||
import { Download, NewDownload } from './interfaces/download.interface'
|
||||
import { AddDownloadHandler } from '../ipc/download/AddDownloadHandler'
|
||||
import { Settings } from './Settings'
|
||||
import InitSettingsHandler from '../ipc/InitSettingsHandler.ipc'
|
||||
|
||||
/**
|
||||
* To add a new IPC listener:
|
||||
@@ -12,30 +16,51 @@ import AlbumArtHandler from '../ipc/AlbumArtHandler.ipc'
|
||||
* 4.) Add the class to getIPCHandlers
|
||||
*/
|
||||
|
||||
export function getIPCHandlers(): IPCHandler<keyof IPCEvents>[] {
|
||||
export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[] {
|
||||
return [
|
||||
new InitSettingsHandler(),
|
||||
new SearchHandler(),
|
||||
new SongDetailsHandler(),
|
||||
new AlbumArtHandler()
|
||||
]
|
||||
}
|
||||
|
||||
export type IPCEvents = {
|
||||
['song-search']: {
|
||||
export type IPCInvokeEvents = {
|
||||
'init-settings': {
|
||||
input: undefined
|
||||
output: Settings
|
||||
}
|
||||
'song-search': {
|
||||
input: SongSearch
|
||||
output: SongResult[]
|
||||
}
|
||||
['album-art']: {
|
||||
'album-art': {
|
||||
input: SongResult['id']
|
||||
output: AlbumArtResult
|
||||
}
|
||||
['song-details']: {
|
||||
'song-details': {
|
||||
input: SongResult['id']
|
||||
output: VersionResult[]
|
||||
}
|
||||
}
|
||||
|
||||
export interface IPCHandler<E extends keyof IPCEvents> {
|
||||
export interface IPCInvokeHandler<E extends keyof IPCInvokeEvents> {
|
||||
event: E
|
||||
handler(data: IPCEvents[E]['input']): Promise<IPCEvents[E]['output']> | IPCEvents[E]['output']
|
||||
handler(data: IPCInvokeEvents[E]['input']): Promise<IPCInvokeEvents[E]['output']> | IPCInvokeEvents[E]['output']
|
||||
}
|
||||
|
||||
export function getIPCEmitHandlers(): IPCEmitHandler<keyof IPCEmitEvents>[]{
|
||||
return [
|
||||
new AddDownloadHandler()
|
||||
]
|
||||
}
|
||||
|
||||
export type IPCEmitEvents = {
|
||||
'add-download': NewDownload
|
||||
'download-updated': Download
|
||||
}
|
||||
|
||||
export interface IPCEmitHandler<E extends keyof IPCEmitEvents> {
|
||||
event: E
|
||||
handler(data: IPCEmitEvents[E]): void
|
||||
}
|
||||
9
src/electron/shared/Paths.ts
Normal file
9
src/electron/shared/Paths.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import * as path from 'path'
|
||||
import { app } from 'electron'
|
||||
|
||||
// Data paths
|
||||
export const dataPath = path.join(app.getPath('userData'), 'bridge_data')
|
||||
export const libraryPath = path.join(dataPath, 'library.db')
|
||||
export const settingsPath = path.join(dataPath, 'settings.json')
|
||||
export const tempPath = path.join(dataPath, 'temp')
|
||||
export const themesPath = path.join(dataPath, 'themes')
|
||||
@@ -1,19 +1,11 @@
|
||||
export class Settings {
|
||||
export interface Settings {
|
||||
rateLimitDelay: number // Number of seconds to wait between each file download from Google servers
|
||||
theme: string // The name of the currently enabled UI theme
|
||||
libraryPath: string // The path to the user's library
|
||||
}
|
||||
|
||||
// Singleton
|
||||
private constructor() { }
|
||||
private static settings: Settings
|
||||
static async getInstance() {
|
||||
if (this.settings == undefined) {
|
||||
this.settings = new Settings()
|
||||
}
|
||||
await this.settings.initSettings()
|
||||
return this.settings
|
||||
}
|
||||
|
||||
songsFolderPath: string
|
||||
|
||||
private async initSettings() {
|
||||
// TODO: load settings from settings file or set defaults
|
||||
}
|
||||
export const defaultSettings: Settings = {
|
||||
rateLimitDelay: 31,
|
||||
theme: 'Default',
|
||||
libraryPath: 'C:/Users/bouviejs/Desktop/Bridge Notes/TestLibrary'
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
33
src/electron/shared/interfaces/download.interface.ts
Normal file
33
src/electron/shared/interfaces/download.interface.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Represents the download of a single chart
|
||||
*/
|
||||
export interface Download {
|
||||
versionID: number
|
||||
title: string
|
||||
header: string
|
||||
description: string
|
||||
percent: number
|
||||
|
||||
//TODO: figure out how to handle user clicking "retry"
|
||||
}
|
||||
|
||||
export interface NewDownload {
|
||||
versionID: number
|
||||
avTagName: string
|
||||
artist: string
|
||||
charter: string
|
||||
links: { [type: string]: string }
|
||||
}
|
||||
|
||||
|
||||
export enum DownloadState {
|
||||
wait, // Waiting for Google rate limit...
|
||||
request, // [song.ini] Sending request...
|
||||
warning, // Warning! [song.ini] has been modified recently and may not match how it was displayed in search results. Download anyway?
|
||||
download, // [song.ini] Downloading: 25%
|
||||
extract, // [archive.zip] Extracting: 44%
|
||||
transfer, // Copying files to library...
|
||||
complete // Complete
|
||||
}
|
||||
|
||||
// Try again button appears after an error: restarts the stage that failed
|
||||
1
src/electron/shared/typeDefinitions/node-7z.d.ts
vendored
Normal file
1
src/electron/shared/typeDefinitions/node-7z.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
declare module 'node-7z'
|
||||
Reference in New Issue
Block a user