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

109
package-lock.json generated
View File

@@ -7,8 +7,7 @@
"7zip-bin": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/7zip-bin/-/7zip-bin-5.0.3.tgz",
"integrity": "sha512-GLyWIFBbGvpKPGo55JyRZAo4lVbnBiD52cKlw/0Vt+wnmKvWJkpZvsjVoaIolyBXDeAQKSicRtqFNPem9w0WYA==",
"dev": true
"integrity": "sha512-GLyWIFBbGvpKPGo55JyRZAo4lVbnBiD52cKlw/0Vt+wnmKvWJkpZvsjVoaIolyBXDeAQKSicRtqFNPem9w0WYA=="
},
"@angular-devkit/architect": {
"version": "0.803.24",
@@ -2320,6 +2319,14 @@
"@types/node": "*"
}
},
"@types/needle": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@types/needle/-/needle-2.0.4.tgz",
"integrity": "sha512-dBSmQsIIGjqSu//zVPhQbTsAUI6sA05Q/raZTXqEna8+jjW7BxF9Sxjif9wkKwP6CYHfnz+E8nP3PdExGm6jOQ==",
"requires": {
"@types/node": "*"
}
},
"@types/node": {
"version": "8.9.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.9.5.tgz",
@@ -2331,6 +2338,11 @@
"integrity": "sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==",
"dev": true
},
"@types/underscore": {
"version": "1.9.4",
"resolved": "https://registry.npmjs.org/@types/underscore/-/underscore-1.9.4.tgz",
"integrity": "sha512-CjHWEMECc2/UxOZh0kpiz3lEyX2Px3rQS9HzD20lxMvx571ivOBQKeLnqEjxUY0BMgp6WJWo/pQLRBwMW5v4WQ=="
},
"@types/webpack-sources": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/@types/webpack-sources/-/webpack-sources-0.1.6.tgz",
@@ -4862,7 +4874,6 @@
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"dev": true,
"requires": {
"ms": "^2.1.1"
}
@@ -10990,8 +11001,7 @@
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"multicast-dns": {
"version": "6.2.3",
@@ -11068,6 +11078,31 @@
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
"dev": true
},
"needle": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/needle/-/needle-2.3.2.tgz",
"integrity": "sha512-DUzITvPVDUy6vczKKYTnWc/pBZ0EnjMJnQ3y+Jo5zfKFimJs7S3HFCxCRZYB9FUZcrzUQr3WsmvZgddMEIZv6w==",
"requires": {
"debug": "^3.2.6",
"iconv-lite": "^0.4.4",
"sax": "^1.2.4"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"sax": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
}
}
},
"negotiator": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
@@ -11090,6 +11125,55 @@
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ=="
},
"node-7z": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/node-7z/-/node-7z-2.0.5.tgz",
"integrity": "sha512-f6vS64T2q53ZenPGD2DEmCN9l2qLJHLje49yDA7DE1ZDqxOV1dOoB/z4jzxJAjHwrvgi9EjddaMXnJWoio52jg==",
"requires": {
"cross-spawn": "^7.0.1",
"debug": "^4.1.1",
"lodash": "^4.17.15",
"normalize-path": "^3.0.0"
},
"dependencies": {
"cross-spawn": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.1.tgz",
"integrity": "sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==",
"requires": {
"path-key": "^3.1.0",
"shebang-command": "^2.0.0",
"which": "^2.0.1"
}
},
"path-key": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="
},
"shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
"requires": {
"shebang-regex": "^3.0.0"
}
},
"shebang-regex": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="
},
"which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"requires": {
"isexe": "^2.0.0"
}
}
}
},
"node-fetch": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
@@ -11178,6 +11262,11 @@
"semver": "^6.3.0"
}
},
"node-unrar-js": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/node-unrar-js/-/node-unrar-js-0.8.1.tgz",
"integrity": "sha1-NRiO96rrq4VC/d0jfUBA8fP+5u4="
},
"node.extend": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/node.extend/-/node.extend-2.0.2.tgz",
@@ -13156,7 +13245,6 @@
"version": "1.6.3",
"resolved": "https://registry.npmjs.org/sanitize-filename/-/sanitize-filename-1.6.3.tgz",
"integrity": "sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==",
"dev": true,
"requires": {
"truncate-utf8-bytes": "^1.0.0"
}
@@ -14561,7 +14649,6 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz",
"integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=",
"dev": true,
"requires": {
"utf8-byte-length": "^1.0.1"
}
@@ -14754,6 +14841,11 @@
"resolved": "https://registry.npmjs.org/unc-path-regex/-/unc-path-regex-0.1.2.tgz",
"integrity": "sha1-5z3T17DXxe2G+6xrCufYxqadUPo="
},
"underscore": {
"version": "1.9.2",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.2.tgz",
"integrity": "sha512-D39qtimx0c1fI3ya1Lnhk3E9nONswSKhnffBI0gME9C99fYOkNi04xs8K6pePLhvl1frbDemkaBQ5ikWllR2HQ=="
},
"undertaker": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/undertaker/-/undertaker-1.2.1.tgz",
@@ -15086,8 +15178,7 @@
"utf8-byte-length": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz",
"integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=",
"dev": true
"integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E="
},
"util": {
"version": "0.11.1",

View File

@@ -25,6 +25,7 @@
"serve:electron": "wait-on http-get://localhost:4200/ && tsc -p tsconfig.electron.json && electron . --dev"
},
"dependencies": {
"7zip-bin": "^5.0.3",
"@angular/animations": "~8.2.14",
"@angular/common": "~8.2.14",
"@angular/compiler": "~8.2.14",
@@ -35,12 +36,19 @@
"@angular/router": "~8.2.14",
"@types/cli-color": "^2.0.0",
"@types/mysql": "^2.15.8",
"@types/needle": "^2.0.4",
"@types/underscore": "^1.9.4",
"cli-color": "^2.0.0",
"fomantic-ui": "^2.8.3",
"jquery": "^3.4.1",
"mysql": "^2.18.1",
"needle": "^2.3.2",
"node-7z": "^2.0.5",
"node-unrar-js": "^0.8.1",
"rxjs": "~6.4.0",
"sanitize-filename": "^1.6.3",
"tslib": "^1.10.0",
"underscore": "^1.9.2",
"zone.js": "~0.9.1"
},
"devDependencies": {

View File

@@ -1,11 +1,17 @@
import { Component } from '@angular/core'
import { Component, OnInit } from '@angular/core'
import { SettingsService } from './core/services/settings.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styles: []
})
export class AppComponent {
export class AppComponent implements OnInit {
constructor() { }
constructor(private settingsService: SettingsService) { }
ngOnInit() {
// Load settings
this.settingsService.getSettings()
}
}

View File

@@ -9,7 +9,9 @@ import { SearchBarComponent } from './components/browse/search-bar/search-bar.co
import { StatusBarComponent } from './components/browse/status-bar/status-bar.component';
import { ResultTableComponent } from './components/browse/result-table/result-table.component';
import { ChartSidebarComponent } from './components/browse/chart-sidebar/chart-sidebar.component';
import { ResultTableRowComponent } from './components/browse/result-table/result-table-row/result-table-row.component'
import { ResultTableRowComponent } from './components/browse/result-table/result-table-row/result-table-row.component';
import { DownloadsModalComponent } from './components/browse/status-bar/downloads-modal/downloads-modal.component';
import { ProgressBarDirective } from './core/directives/progress-bar.directive'
@NgModule({
declarations: [
@@ -20,7 +22,9 @@ import { ResultTableRowComponent } from './components/browse/result-table/result
StatusBarComponent,
ResultTableComponent,
ChartSidebarComponent,
ResultTableRowComponent
ResultTableRowComponent,
DownloadsModalComponent,
ProgressBarDirective
],
imports: [
BrowserModule,

View File

@@ -2,7 +2,7 @@
<div class="ui placeholder" [ngClass]="{'placeholder': !albumArtBuffer}">
<img class="ui square image" [src]="getAlbumArtSrc()">
</div>
<div id="chartDropdown" class="ui fluid selection dropdown">
<div *ngIf="charts.length > 1" id="chartDropdown" class="ui fluid selection dropdown">
<input type="hidden" name="Chart">
<i class="dropdown icon"></i>
<div class="default text">Chart</div>
@@ -26,7 +26,7 @@
</div>
<div class="ui positive buttons">
<div class="ui button" (click)="onDownloadClicked()">Download Latest</div>
<div id="versionDropdown" class="ui floating dropdown icon button">
<div *ngIf="getVersions().length > 1" id="versionDropdown" class="ui floating dropdown icon button">
<i class="dropdown icon"></i>
<div class="menu">
<div class="item">Version 1</div>

View File

@@ -1,8 +1,9 @@
import { Component } from '@angular/core'
import { SongResult } from 'src/electron/shared/interfaces/search.interface'
import { ElectronService } from 'src/app/core/services/electron.service'
import { VersionResult } from 'src/electron/shared/interfaces/songDetails.interface'
import { AlbumArtService } from 'src/app/core/services/album-art.service'
import { SongResult } from '../../../../electron/shared/interfaces/search.interface'
import { ElectronService } from '../../../core/services/electron.service'
import { VersionResult } from '../../../../electron/shared/interfaces/songDetails.interface'
import { AlbumArtService } from '../../../core/services/album-art.service'
import { DownloadService } from '../../../core/services/download.service'
@Component({
selector: 'app-chart-sidebar',
@@ -11,15 +12,17 @@ import { AlbumArtService } from 'src/app/core/services/album-art.service'
})
export class ChartSidebarComponent {
private songResult: SongResult
selectedVersion: VersionResult
charterPlural: string
albumArtBuffer: Buffer
private charts: { chartID: number, versions: VersionResult[] }[]
charts: { chartID: number, versions: VersionResult[] }[]
constructor(private electronService: ElectronService, private albumArtService: AlbumArtService) { }
constructor(private electronService: ElectronService, private albumArtService: AlbumArtService, private downloadService: DownloadService) { }
async onRowClicked(result: SongResult) {
this.songResult = result
const albumArt = this.albumArtService.getImage(result.id)
const results = await this.electronService.invoke('song-details', result.id)
@@ -42,32 +45,28 @@ export class ChartSidebarComponent {
if (this.albumArtBuffer) {
return 'data:image/jpg;base64,' + this.albumArtBuffer.toString('base64')
} else {
return undefined
return ''
}
}
/**
* Initializes the chart dropdown from <this.charts> (or removes it if there's only one chart)
*/
private initChartDropdown() {
private async initChartDropdown() {
this.switchChart(this.charts[0].chartID)
await new Promise<void>(resolve => setTimeout(() => resolve(), 0)) // Wait for *ngIf to update DOM
const values = this.charts.map(chart => {
const version = chart.versions[0]
return {
value: chart.chartID,
text: version.avTagName,
name: `${version.avTagName} <b>[${version.charters}]</b>`
}
})
const $chartDropdown = $('#chartDropdown')
if (this.charts.length < 2) {
$chartDropdown.hide()
this.switchChart(this.charts[0].chartID)
} else {
const values = this.charts.map(chart => {
const version = chart.versions[0]
return {
value: chart.chartID,
text: version.avTagName,
name: `${version.avTagName} <b>[${version.charters}]</b>`
}
})
$chartDropdown.dropdown('setup menu', { values })
$chartDropdown.dropdown('setting', 'onChange', (chartID: number) => this.switchChart(chartID))
$chartDropdown.dropdown('set selected', values[0].value)
$chartDropdown.show()
}
$chartDropdown.dropdown('setup menu', { values })
$chartDropdown.dropdown('setting', 'onChange', (chartID: number) => this.switchChart(chartID))
$chartDropdown.dropdown('set selected', values[0].value)
}
/**
@@ -104,25 +103,20 @@ export class ChartSidebarComponent {
*/
private initVersionDropdown() {
const $versionDropdown = $('#versionDropdown')
const versions = this.charts.find(chart => chart.chartID == this.selectedVersion.chartID).versions
if (versions.length < 2) {
$versionDropdown.hide()
} else {
const values = versions.map(version => {
return {
value: version.versionID,
text: this.getLastModified(version.lastModified),
name: this.getLastModified(version.lastModified)
}
})
$versionDropdown.dropdown('setup menu', { values })
$versionDropdown.dropdown('setting', 'onChange', (versionID: number) => {
console.log(`Selected version: ${versionID}`)
return this.selectedVersion = versions.find(version => version.versionID = versionID)
})
$versionDropdown.dropdown('set selected', values[0].value)
$versionDropdown.show()
}
const versions = this.getVersions()
const values = versions.map(version => {
return {
value: version.versionID,
text: this.getLastModified(version.lastModified),
name: this.getLastModified(version.lastModified)
}
})
$versionDropdown.dropdown('setup menu', { values })
$versionDropdown.dropdown('setting', 'onChange', (versionID: number) => {
console.log(`Selected version: ${versionID}`)
this.selectedVersion = versions.find(version => version.versionID = versionID)
})
$versionDropdown.dropdown('set selected', values[0].value)
}
/**
@@ -134,6 +128,16 @@ export class ChartSidebarComponent {
}
onDownloadClicked() {
console.log(this.selectedVersion.downloadLink)
this.downloadService.addDownload({
versionID: this.selectedVersion.versionID,
avTagName: this.selectedVersion.avTagName,
artist: this.songResult.artist,
charter: this.selectedVersion.charters,
links: JSON.parse(this.selectedVersion.downloadLink)
})
}
getVersions() {
return this.charts.find(chart => chart.chartID == this.selectedVersion.chartID).versions
}
}

View File

@@ -1,5 +1,5 @@
import { Component, AfterViewInit, Input } from '@angular/core'
import { SongResult } from 'src/electron/shared/interfaces/search.interface'
import { SongResult } from '../../../../../electron/shared/interfaces/search.interface'
@Component({
selector: 'tr[app-result-table-row]',

View File

@@ -1,5 +1,5 @@
import { Component, AfterViewInit, Input, Output, EventEmitter } from '@angular/core'
import { SongResult } from 'src/electron/shared/interfaces/search.interface'
import { SongResult } from '../../../../electron/shared/interfaces/search.interface'
@Component({
selector: 'app-result-table',

View File

@@ -1,6 +1,6 @@
import { Component, AfterViewInit, Output, EventEmitter } from '@angular/core'
import { ElectronService } from 'src/app/core/services/electron.service'
import { SearchType, SongResult } from 'src/electron/shared/interfaces/search.interface'
import { ElectronService } from '../../../core/services/electron.service'
import { SearchType, SongResult } from '../../../../electron/shared/interfaces/search.interface'
@Component({
selector: 'app-search-bar',

View File

@@ -0,0 +1,16 @@
<div class="ui cards">
<div *ngFor="let download of downloads; trackBy:trackByVersionID" class="card">
<div class="content">
<div class="header">{{download.title}}</div>
</div>
<div class="content">
<div class="header">{{download.header}}</div>
<div class="description">{{download.description}}</div>
<div appProgressBar [percent]="download.percent" class="ui progress">
<div class="bar">
<div class="progress"></div>
</div>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,7 @@
.card {
flex-grow: 1;
}
.ui.progress {
margin: 0;
}

View File

@@ -0,0 +1,31 @@
import { Component, ChangeDetectorRef } from '@angular/core'
import { Download } from '../../../../../electron/shared/interfaces/download.interface'
import { DownloadService } from '../../../../core/services/download.service'
import * as _ from 'underscore'
@Component({
selector: 'app-downloads-modal',
templateUrl: './downloads-modal.component.html',
styleUrls: ['./downloads-modal.component.scss']
})
export class DownloadsModalComponent {
downloads: Download[] = []
constructor(downloadService: DownloadService, private ref: ChangeDetectorRef) {
const detectChanges = _.throttle(() => this.ref.detectChanges(), 30)
downloadService.onDownloadUpdated(download => {
const index = this.downloads.findIndex(thisDownload => thisDownload.versionID == download.versionID)
if (index == -1) {
this.downloads.push(download)
} else {
this.downloads[index] = download
}
detectChanges()
})
}
trackByVersionID(_index: number, item: Download) {
return item.versionID
}
}

View File

@@ -3,12 +3,19 @@
<div class="item">
<button class="ui positive button">Download Selected</button>
</div>
<a class="item right">
<div #progressBar class="ui progress">
<a *ngIf="!downloading" class="item right" (click)="showDownloads()">
<div #progressBar appProgressBar [percent]="percent" class="ui progress">
<div class="bar">
<div class="progress"></div>
</div>
</div>
</a>
<div id="downloadsModal" class="ui modal">
<i class="inside close icon"></i>
<div class="header">Downloads</div>
<div class="scrolling content">
<app-downloads-modal></app-downloads-modal>
</div>
</div>
</div>

View File

@@ -7,6 +7,11 @@ import { Component } from '@angular/core'
})
export class StatusBarComponent {
downloading = false
constructor() { }
showDownloads() {
$('#downloadsModal').modal('show')
}
}

View File

@@ -1,5 +1,5 @@
import { Component } from '@angular/core'
import { ElectronService } from 'src/app/core/services/electron.service'
import { ElectronService } from '../../core/services/electron.service'
@Component({
selector: 'app-toolbar',

View File

@@ -0,0 +1,20 @@
import { Directive, ElementRef, Input } from '@angular/core'
import * as _ from 'underscore'
@Directive({
selector: '[appProgressBar]'
})
export class ProgressBarDirective {
progress: (percent: number) => void
@Input() set percent(percent: number) {
this.progress(percent)
}
constructor(element: ElementRef) {
const $progressBar = $(element.nativeElement)
this.progress = _.throttle((percent: number) => $progressBar.progress({ percent }), 100)
this.percent = 0
}
}

View File

@@ -1,9 +1,24 @@
import { Injectable } from '@angular/core'
import { Injectable, EventEmitter } from '@angular/core'
import { ElectronService } from './electron.service'
import { Download, NewDownload } from '../../../electron/shared/interfaces/download.interface'
@Injectable({
providedIn: 'root'
})
export class DownloadService {
constructor() { }
private downloadUpdatedEmitter = new EventEmitter<Download>()
constructor(private electronService: ElectronService) { }
addDownload(newDownload: NewDownload) {
this.electronService.receiveIPC('download-updated', result => {
this.downloadUpdatedEmitter.emit(result)
})
this.electronService.sendIPC('add-download', newDownload)
}
onDownloadUpdated(callback: (download: Download) => void) {
this.downloadUpdatedEmitter.subscribe(callback)
}
}

View File

@@ -6,7 +6,7 @@ import { ipcRenderer, webFrame, remote } from 'electron'
import * as childProcess from 'child_process'
import * as fs from 'fs'
import * as util from 'util'
import { IPCEvents } from '../../../electron/shared/IPCHandler'
import { IPCInvokeEvents, IPCEmitEvents } from '../../../electron/shared/IPCHandler'
@Injectable({
providedIn: 'root'
@@ -42,7 +42,27 @@ export class ElectronService {
* @param data The data object to send across IPC.
* @returns A promise that resolves to the output data.
*/
async invoke<E extends keyof IPCEvents>(event: E, data: IPCEvents[E]['input']) {
return this.ipcRenderer.invoke(event, data) as Promise<IPCEvents[E]['output']>
async invoke<E extends keyof IPCInvokeEvents>(event: E, data: IPCInvokeEvents[E]['input']) {
return this.ipcRenderer.invoke(event, data) as Promise<IPCInvokeEvents[E]['output']>
}
/**
* Sends an IPC message to the main process.
* @param event The name of the IPC event to send.
* @param data The data object to send across IPC.
*/
sendIPC<E extends keyof IPCEmitEvents>(event: E, data: IPCEmitEvents[E]) {
this.ipcRenderer.send(event, data)
}
/**
* Receives an IPC message from the main process.
* @param event The name of the IPC event to receive.
* @param callback The data object to receive across IPC.
*/
receiveIPC<E extends keyof IPCEmitEvents>(event: E, callback: (result: IPCEmitEvents[E]) => void) {
this.ipcRenderer.on(event, (_event, ...results) => {
callback(results[0])
})
}
}

View File

@@ -0,0 +1,20 @@
import { Injectable } from '@angular/core'
import { ElectronService } from './electron.service'
import { Settings } from 'src/electron/shared/Settings'
@Injectable({
providedIn: 'root'
})
export class SettingsService {
private settings: Settings
constructor(private electronService: ElectronService) { }
async getSettings() {
if (this.settings == undefined) {
this.settings = await this.electronService.invoke('init-settings', undefined)
}
return this.settings
}
}

View File

@@ -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

View 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
}
}
}

View File

@@ -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

View File

@@ -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

View 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()
}
}

View 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']
}
}
}

View 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)
}
}
}

View File

@@ -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)
}

View File

@@ -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
}

View 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')

View File

@@ -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'
}

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
}

View 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

View File

@@ -0,0 +1 @@
declare module 'node-7z'