Skip to content

Commit

Permalink
Update our application
Browse files Browse the repository at this point in the history
  • Loading branch information
fearganni committed Dec 24, 2023
1 parent 7bb7de2 commit 2b470c7
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/src/Lethalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { initialize, enable } from '@electron/remote/main';
import * as path from 'path';
import * as fs from 'fs';

import { AppUpdates, DiscordRPC } from './modules';
import { AppUpdate, DiscordRPC, IpcMain } from './controllers';

let win: BrowserWindow | null = null;
const args = process.argv.slice(1),
Expand Down Expand Up @@ -70,7 +70,8 @@ try {
setTimeout(createWindow, 400);

new DiscordRPC().connect();
new AppUpdates(win).check();
new IpcMain(win).listen();
new AppUpdate(win).check();
});

// Quit when all windows are closed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BrowserWindow } from 'electron';
import { autoUpdater, UpdateDownloadedEvent } from 'electron-updater';

export class AppUpdates {
export class AppUpdate {
win: BrowserWindow | null = null;

constructor(window: BrowserWindow | null) {
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions app/src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './appUpdate';
export * from './ipcMain';
export * from './discordRpc';
79 changes: 79 additions & 0 deletions app/src/controllers/ipc/Download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { BrowserWindow, IpcMainEvent } from 'electron';
import fs from 'fs';

Check failure on line 2 in app/src/controllers/ipc/Download.ts

View workflow job for this annotation

GitHub Actions / build (18.16)

Module '"fs"' has no default export.
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,
});
}
}
1 change: 1 addition & 0 deletions app/src/controllers/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Download';
24 changes: 24 additions & 0 deletions app/src/controllers/ipcMain.ts
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
}
}
4 changes: 4 additions & 0 deletions app/src/modules/calculateProgress.ts
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);
}
4 changes: 2 additions & 2 deletions app/src/modules/index.ts
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';
7 changes: 7 additions & 0 deletions app/src/modules/temporaryLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os from 'os';

Check failure on line 1 in app/src/modules/temporaryLocation.ts

View workflow job for this annotation

GitHub Actions / build (18.16)

Module '"os"' has no default export.
import path from 'path';

Check failure on line 2 in app/src/modules/temporaryLocation.ts

View workflow job for this annotation

GitHub Actions / build (18.16)

Module '"path"' can only be default-imported using the 'esModuleInterop' flag

// Create a temporary file location for downloaded files
export function temporaryLocation(fileName: string) {
return path.join(os.tmpdir(), fileName);
}

0 comments on commit 2b470c7

Please sign in to comment.