generated from electron-react-boilerplate/electron-react-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
114 changed files
with
2,144 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* eslint-disable global-require */ | ||
/* eslint-disable promise/always-return */ | ||
/* eslint-disable no-case-declarations */ | ||
/* eslint-disable import/no-named-as-default-member */ | ||
import path from 'path'; | ||
import { downloadMP3, getYoutubeLinkInfo } from '../libs/youtube-dl'; | ||
import { setState } from '../main/store'; | ||
import { errorResponse, successResponse, validateYoutubeLink } from '../utils'; | ||
/* eslint-disable class-methods-use-this */ | ||
import { | ||
DownloadQueueItem, | ||
IpcChannelInterface, | ||
IpcRequest, | ||
} from '../interfaces'; | ||
import { DownloaderActions } from '../main/actions/Downloader'; | ||
import { | ||
DownloaderReducer, | ||
updateDownloadItemStatus, | ||
updateMetadata, | ||
} from '../main/reducers/Downloader'; | ||
|
||
export default class DownloaderChannel implements IpcChannelInterface { | ||
getName(): string { | ||
return 'downloader-channel'; | ||
} | ||
|
||
handleFileDialog(): { | ||
status: string; | ||
file?: string; | ||
message?: string; | ||
data?: string[]; | ||
error?: string; | ||
} { | ||
const { dialog } = require('electron'); | ||
const selectedFile = dialog.showOpenDialogSync({ | ||
properties: ['openFile'], | ||
filters: [{ name: 'Text Files', extensions: ['txt'] }], | ||
}); | ||
if (!selectedFile?.length) { | ||
return { | ||
status: 'error', | ||
message: 'No file selected', | ||
}; | ||
} | ||
let reply = { | ||
status: 'success', | ||
file: path.basename(selectedFile[0]), | ||
data: [], | ||
}; | ||
try { | ||
const fs = require('fs'); | ||
const data = fs.readFileSync(selectedFile[0], 'utf8'); | ||
const urls = data.trim().split('\n'); | ||
const validatedUrls = urls | ||
.map((url: string) => url.trim()) | ||
.filter((url: string) => validateYoutubeLink(url)); | ||
reply = { ...reply, data: validatedUrls }; | ||
} catch (err) { | ||
console.error(err); | ||
reply = { ...reply, status: 'error' }; | ||
} | ||
return reply; | ||
} | ||
|
||
handle(event: Electron.IpcMainEvent, request: IpcRequest): void { | ||
if (!request.responseChannel) { | ||
request.responseChannel = `${this.getName()}_response`; | ||
} | ||
const { responseChannel } = request; | ||
const { action, payload } = request; | ||
if (!action || !payload) return; | ||
const { | ||
START_DOWNLOAD, | ||
CANCEL_DOWNLOAD, | ||
ADD_TO_DOWNLOAD_QUEUE, | ||
CLEAR_DOWNLOAD_QUEUE, | ||
REMOVE_FROM_DOWNLOAD_QUEUE, | ||
DOWNLOAD_FROM_TEXT_FILE, | ||
LOAD_LINK_METADATA, | ||
} = DownloaderActions; | ||
switch (action) { | ||
case ADD_TO_DOWNLOAD_QUEUE: | ||
setState((state) => | ||
DownloaderReducer[ADD_TO_DOWNLOAD_QUEUE]( | ||
state, | ||
payload as DownloadQueueItem | ||
) | ||
); | ||
break; | ||
case REMOVE_FROM_DOWNLOAD_QUEUE: | ||
setState((state) => | ||
DownloaderReducer[REMOVE_FROM_DOWNLOAD_QUEUE]( | ||
state, | ||
payload as DownloadQueueItem | ||
) | ||
); | ||
break; | ||
case START_DOWNLOAD: | ||
const item = payload as DownloadQueueItem; | ||
setState((state) => DownloaderReducer[START_DOWNLOAD](state, item)); | ||
downloadMP3(item.url) | ||
.then((response) => { | ||
if (response.status === 'success') { | ||
updateDownloadItemStatus(item, 'downloaded'); | ||
event.sender.send( | ||
responseChannel, | ||
successResponse(this, 'Downloading', response) | ||
); | ||
} else { | ||
event.sender.send( | ||
responseChannel, | ||
errorResponse(this, 'Failed to start download') | ||
); | ||
} | ||
}) | ||
.catch((error) => { | ||
updateDownloadItemStatus(item, 'error'); | ||
event.sender.send( | ||
responseChannel, | ||
errorResponse(this, 'Failed to start download') | ||
); | ||
console.log(error); | ||
}); | ||
break; | ||
case CANCEL_DOWNLOAD: | ||
setState((state) => | ||
DownloaderReducer[CANCEL_DOWNLOAD]( | ||
state, | ||
payload as DownloadQueueItem | ||
) | ||
); | ||
break; | ||
case CLEAR_DOWNLOAD_QUEUE: | ||
setState((state) => | ||
DownloaderReducer[CLEAR_DOWNLOAD_QUEUE]( | ||
state, | ||
{} as DownloadQueueItem | ||
) | ||
); | ||
break; | ||
case DOWNLOAD_FROM_TEXT_FILE: | ||
const { status, file, data } = this.handleFileDialog(); | ||
if (status === 'success' && data) { | ||
event.sender.send( | ||
responseChannel, | ||
successResponse(this, 'File selected', { file, data }) | ||
); | ||
} else { | ||
event.sender.send( | ||
responseChannel, | ||
errorResponse(this, 'Failed to start download') | ||
); | ||
} | ||
break; | ||
case LOAD_LINK_METADATA: | ||
const { url } = payload as DownloadQueueItem; | ||
getYoutubeLinkInfo(url) | ||
.then((response) => { | ||
const { videoDetails } = response; | ||
updateMetadata(url, videoDetails); | ||
event.sender.send( | ||
responseChannel, | ||
successResponse(this, 'Info Retrieved', videoDetails) | ||
); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
event.sender.send( | ||
responseChannel, | ||
errorResponse(this, 'Failed to get youtube link info') | ||
); | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
console.log(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
IpcResponse as IpcResponseI, | ||
IpcResponseArgs, | ||
IpcResponseBody, | ||
} from '../interfaces'; | ||
|
||
export default class IpcResponse implements IpcResponseI { | ||
responder: string; | ||
|
||
response: IpcResponseBody; | ||
|
||
constructor(args: IpcResponseArgs) { | ||
const { ipcChannel, status, message, data } = args; | ||
this.responder = ipcChannel.getName(); | ||
this.response = { | ||
status, | ||
message, | ||
data, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable global-require */ | ||
import { IpcRenderer } from 'electron'; | ||
import { IpcRequest, PromisedIpcResponse } from 'interfaces'; | ||
|
||
export default class IpcService { | ||
ipcRenderer?: IpcRenderer; | ||
|
||
public constructor(ipcRenderer: IpcRenderer) { | ||
this.ipcRenderer = ipcRenderer; | ||
} | ||
|
||
public send<T>(channel: string, request: IpcRequest): PromisedIpcResponse<T> { | ||
if (!this.ipcRenderer) { | ||
throw new Error(`Unable to require renderer process`); | ||
} | ||
const responseChannel = request?.responseChannel ?? `${channel}_response`; | ||
const { ipcRenderer } = this; | ||
|
||
if (!ipcRenderer) { | ||
throw new Error(`Unable to send message to channel ${channel}`); | ||
} | ||
|
||
ipcRenderer.send(channel, request); | ||
|
||
return new Promise((resolve) => { | ||
return ipcRenderer.once(responseChannel, (_event, response) => | ||
resolve(response) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as DownloaderChannel } from './DownloaderChannel'; | ||
export { default as IpcService } from './IpcService'; |
Oops, something went wrong.