generated from maximegris/angular-electron
-
-
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
10 changed files
with
124 additions
and
5 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
2 changes: 1 addition & 1 deletion
2
app/src/modules/AppUpdates.ts → app/src/controllers/appUpdate.ts
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
File renamed without changes.
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,3 @@ | ||
export * from './appUpdate'; | ||
export * from './ipcMain'; | ||
export * from './discordRpc'; |
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,79 @@ | ||
import { BrowserWindow, IpcMainEvent } from 'electron'; | ||
import fs from 'fs'; | ||
import axios from 'axios'; | ||
|
||
import { calculateProgress, temporaryLocation } from '../../modules'; | ||
|
||
export async function DownloadFile( | ||
event: IpcMainEvent, | ||
fileUrl: string, | ||
savePath: string, | ||
win: BrowserWindow | null | ||
) { | ||
// Create a temporary location for our saved files | ||
const tmpLoc = temporaryLocation(savePath); | ||
|
||
try { | ||
// Download the file | ||
const file = await axios({ | ||
method: 'get', | ||
url: fileUrl, | ||
responseType: 'stream', | ||
}); | ||
|
||
// Write the file into the temporary location | ||
const write = fs.createWriteStream(tmpLoc); | ||
file.data.pipe(write); | ||
|
||
// Keep track of download progress | ||
let receivedBytes = 0; | ||
file.data.on('data', (chunk: any[]) => { | ||
// How many bytes have we gotten so far? | ||
receivedBytes += chunk.length; | ||
|
||
// Calculate the progress as a percentage | ||
const progress = calculateProgress( | ||
receivedBytes, | ||
parseInt(file.headers['Content-Length'] as string) | ||
); | ||
|
||
// Set the browser downloading state | ||
win?.setProgressBar(progress / 100); | ||
|
||
// Send updates to the client | ||
event.sender.send('download-progress', { | ||
fileUrl, | ||
savePath, | ||
progress, | ||
finished: false, | ||
}); | ||
}); | ||
|
||
// Once the file is complete | ||
write.on('finish', () => { | ||
event.sender.send('download-progress', { | ||
fileUrl, | ||
savePath, | ||
progress: 100, | ||
finished: true, | ||
}); | ||
}); | ||
|
||
// Handle any stream errors | ||
write.on('error', (err) => { | ||
event.sender.send('download-error', { | ||
fileUrl, | ||
savePath, | ||
error: err.message, | ||
}); | ||
|
||
fs.unlink(tmpLoc, () => {}); // Delete the temporary file on error | ||
}); | ||
} catch (error: any) { | ||
event.sender.send('download-error', { | ||
fileUrl, | ||
savePath, | ||
error: error.message, | ||
}); | ||
} | ||
} |
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 @@ | ||
export * from './Download'; |
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,24 @@ | ||
import { BrowserWindow, ipcMain } from 'electron'; | ||
|
||
import { DownloadFile } from './ipc/Download'; | ||
|
||
export class IpcMain { | ||
win: BrowserWindow | null = null; | ||
|
||
constructor(window: BrowserWindow | null) { | ||
this.win = window; | ||
} | ||
|
||
listen(): void { | ||
// Create a download file listener | ||
ipcMain.on( | ||
'download-file', | ||
async (event, fileUrl, savePath) => | ||
await DownloadFile(event, fileUrl, savePath, this.win) | ||
); | ||
|
||
// Create the extract file listener | ||
|
||
// Create the update file listener | ||
} | ||
} |
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,4 @@ | ||
// Calculate a percentage of how much progress is left. | ||
export function calculateProgress(receivedBytes: number, totalBytes: number) { | ||
return Math.round((receivedBytes / totalBytes) * 100); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './AppUpdates'; | ||
export * from './DiscordRPC'; | ||
export * from './temporaryLocation'; | ||
export * from './calculateProgress'; |
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,7 @@ | ||
import os from 'os'; | ||
import path from 'path'; | ||
|
||
// Create a temporary file location for downloaded files | ||
export function temporaryLocation(fileName: string) { | ||
return path.join(os.tmpdir(), fileName); | ||
} |