77 lines
No EOL
1.6 KiB
TypeScript
77 lines
No EOL
1.6 KiB
TypeScript
import { DownloadInfo, FolderTypes, ProgressData } from '../../../@types/messageHandler';
|
|
import { RandomEvent, RandomEvents } from '../../../@types/randomEvents';
|
|
import WebSocketHandler from '../websocket';
|
|
import copy from "copy-to-clipboard";
|
|
import open from 'open';
|
|
import { cfg } from '..';
|
|
import path from 'path';
|
|
|
|
export default class Base {
|
|
|
|
constructor(private ws: WebSocketHandler) {}
|
|
|
|
private downloading = false;
|
|
|
|
setDownloading(downloading: boolean) {
|
|
this.downloading = downloading;
|
|
}
|
|
|
|
getDownloading() {
|
|
return this.downloading;
|
|
}
|
|
|
|
alertError(error: Error) {
|
|
console.log(`[ERROR] ${error}`);
|
|
}
|
|
|
|
makeProgressHandler(videoInfo: DownloadInfo) {
|
|
return ((data: ProgressData) => {
|
|
this.sendMessage({
|
|
name: 'progress',
|
|
data: {
|
|
downloadInfo: videoInfo,
|
|
progress: data
|
|
}
|
|
});
|
|
}).bind(this);
|
|
}
|
|
|
|
sendMessage<T extends keyof RandomEvents>(data: RandomEvent<T>) {
|
|
this.ws.sendMessage(data);
|
|
}
|
|
|
|
async isDownloading() {
|
|
return this.downloading;
|
|
}
|
|
|
|
async writeToClipboard(text: string) {
|
|
copy(text);
|
|
return true;
|
|
}
|
|
|
|
async openFolder(folderType: FolderTypes) {
|
|
switch (folderType) {
|
|
case 'content':
|
|
open(cfg.dir.content);
|
|
break;
|
|
case 'config':
|
|
open(cfg.dir.config);
|
|
break;
|
|
}
|
|
}
|
|
|
|
async openFile(data: [FolderTypes, string]) {
|
|
switch (data[0]) {
|
|
case 'config':
|
|
open(path.join(cfg.dir.config, data[1]));
|
|
break;
|
|
case 'content':
|
|
throw new Error('No subfolders');
|
|
}
|
|
}
|
|
|
|
async openURL(data: string) {
|
|
open(data);
|
|
}
|
|
|
|
} |