Rework downloads and some ipc stuff

This commit is contained in:
john liddell
2020-10-19 22:32:23 -05:00
parent 0c4bf61da8
commit 550b84c4fd
5 changed files with 113 additions and 80 deletions

View File

@@ -1,10 +1,10 @@
import { BrowserWindow, ipcMain, shell } from "electron";
import * as fs from "fs";
import * as fs from "fs-extra";
import * as async from "async";
import * as path from "path";
import * as admZip from "adm-zip";
import { ncp } from "ncp";
import * as rimraf from "rimraf";
import { readdir } from "fs";
import { readDirRecursive, readFile } from "./file.utils";
import {
@@ -133,7 +133,7 @@ export class IpcHandler {
console.log(LIST_DIRECTORIES_CHANNEL, arg);
const response: ValueResponse<string[]> = { value: [] };
fs.readdir(arg.value, { withFileTypes: true }, (err, files) => {
readdir(arg.value, { withFileTypes: true }, (err, files) => {
if (err) {
response.error = err;
} else {
@@ -247,25 +247,20 @@ export class IpcHandler {
});
});
ipcMain.on(COPY_DIRECTORY_CHANNEL, (evt, arg: CopyDirectoryRequest) => {
ipcMain.handle(COPY_DIRECTORY_CHANNEL, async (evt, arg: CopyDirectoryRequest) => {
console.log("Copy Dir", arg);
const response: IpcResponse = {};
await fs.ensureDir(arg.destinationPath);
await fs.copy(arg.sourcePath, arg.destinationPath, { recursive: true });
ncp(arg.sourcePath, arg.destinationPath, (err) => {
if (err) {
console.error(err);
response.error = err[0];
}
setTimeout(() => evt.reply(arg.responseKey, response), 500);
});
return arg.destinationPath;
});
ipcMain.on(DELETE_DIRECTORY_CHANNEL, (evt, arg: DeleteDirectoryRequest) => {
console.log("Delete Dir", arg);
const response: IpcResponse = {};
rimraf(arg.sourcePath, (err) => {
fs.remove(arg.sourcePath, (err) => {
if (err) {
console.error(err);
response.error = err;

View File

@@ -26,6 +26,8 @@ import { WindowState } from "./src/common/models/window-state";
import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";
import { IpcHandler } from "./ipc-events";
import * as fs from 'fs';
import axios from 'axios';
const isMac = process.platform === "darwin";
const isWin = process.platform === "win32";
@@ -47,49 +49,49 @@ autoUpdater.on("update-downloaded", () => {
const appMenuTemplate: Array<MenuItemConstructorOptions | MenuItem> = isMac
? [
{
label: app.name,
submenu: [{ role: "quit" }],
},
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "selectAll" },
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn", accelerator: "CommandOrControl+=" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
]
{
label: app.name,
submenu: [{ role: "quit" }],
},
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "selectAll" },
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn", accelerator: "CommandOrControl+=" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
]
: [
{
label: "View",
submenu: [
{ role: "resetZoom" },
{ role: "zoomIn", accelerator: "CommandOrControl+=" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
];
{
label: "View",
submenu: [
{ role: "resetZoom" },
{ role: "zoomIn", accelerator: "CommandOrControl+=" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
];
const appMenu = Menu.buildFromTemplate(appMenuTemplate);
Menu.setApplicationMenu(appMenu);
@@ -176,9 +178,9 @@ function windowStateManager(
windowState.x >= display.bounds.x &&
windowState.y >= display.bounds.y &&
windowState.x + windowState.width <=
display.bounds.x + display.bounds.width &&
display.bounds.x + display.bounds.width &&
windowState.y + windowState.height <=
display.bounds.y + display.bounds.height
display.bounds.y + display.bounds.height
);
});
@@ -395,21 +397,45 @@ try {
ipcMain.on(DOWNLOAD_FILE_CHANNEL, async (evt, arg: DownloadRequest) => {
try {
const download = await electronDl.download(win, arg.url, {
directory: arg.outputFolder,
onProgress: (progress) => {
const progressStatus: DownloadStatus = {
type: DownloadStatusType.Progress,
progress: parseFloat((progress.percent * 100.0).toFixed(2)),
};
const savePath = path.join(arg.outputFolder, './octocat.zip');
win.webContents.send(arg.responseKey, progressStatus);
},
const { data, headers } = await axios({
url: arg.url,
method: 'GET',
responseType: 'stream'
});
const totalLength = headers['content-length'];
console.log('Starting download');
data.on('data', (chunk) => {
console.log('DLPROG', arg.responseKey);
})
const writer = fs.createWriteStream(savePath);
data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
// const download = await electronDl.download(win, arg.url, {
// directory: arg.outputFolder,
// onProgress: (progress) => {
// console.log('DLPROG', arg.responseKey, progress);
// const progressStatus: DownloadStatus = {
// type: DownloadStatusType.Progress,
// progress: parseFloat((progress.percent * 100.0).toFixed(2)),
// };
// win.webContents.send(arg.responseKey, progressStatus);
// },
// });
const status: DownloadStatus = {
type: DownloadStatusType.Complete,
savePath: download.getSavePath(),
savePath
};
win.webContents.send(arg.responseKey, status);
} catch (err) {

View File

@@ -65,7 +65,6 @@
"@types/jasmine": "3.5.14",
"@types/jasminewd2": "2.0.8",
"@types/mocha": "8.0.3",
"@types/ncp": "2.0.4",
"@types/node": "12.12.62",
"@types/opossum": "4.1.1",
"@types/rimraf": "3.0.0",
@@ -112,16 +111,17 @@
"@types/lodash": "4.14.161",
"adm-zip": "0.4.16",
"async": "3.2.0",
"axios": "0.20.0",
"compare-versions": "3.6.0",
"conf": "7.1.2",
"electron-dl": "3.0.2",
"electron-log": "4.2.4",
"electron-store": "6.0.0",
"electron-updater": "4.3.5",
"fs-extra": "9.0.1",
"globrex": "0.1.2",
"lodash": "4.17.20",
"mat-progress-buttons": "9.1.1",
"ncp": "2.0.0",
"node-cache": "5.1.2",
"node-disk-info": "1.1.0",
"opossum": "5.0.1",

View File

@@ -344,6 +344,10 @@ export class AddonService {
);
try {
// If the backup dir exists for some reason, kill it.
console.log("DELETE BKUP", unzipBackupLocation);
await this._fileService.deleteIfExists(unzipBackupLocation);
// If the user already has the addon installed, create a temporary backup
if (await this._fileService.pathExists(unzipLocation)) {
console.log("BACKING UP", unzipLocation);
@@ -359,10 +363,8 @@ export class AddonService {
await this._fileService.copyDirectory(unzippedFilePath, unzipLocation);
// If the copy succeeds, delete the backup
if (fs.existsSync(unzipBackupLocation)) {
console.log("DELETE BKUP", unzipLocation);
await this._fileService.deleteDirectory(unzipBackupLocation);
}
console.log("DELETE BKUP", unzipBackupLocation);
await this._fileService.deleteIfExists(unzipBackupLocation);
} catch (err) {
console.error(`Failed to copy addon directory ${unzipLocation}`);
console.error(err);

View File

@@ -35,7 +35,7 @@ import { IpcResponse } from "common/models/ipc-response";
providedIn: "root",
})
export class FileService {
constructor(private _electronService: ElectronService) {}
constructor(private _electronService: ElectronService) { }
public async getAssetFilePath(fileName: string) {
return await this._electronService.sendIpcValueMessage<string, string>(
@@ -130,12 +130,16 @@ export class FileService {
responseKey: uuidv4(),
};
const response = await this._electronService.sendIPCMessage<
CopyDirectoryRequest,
IpcResponse
>(COPY_DIRECTORY_CHANNEL, request);
const result = await this._electronService.ipcRenderer.invoke(COPY_DIRECTORY_CHANNEL, request);
console.log('RES', result);
return destinationPath;
// const response = await this._electronService.sendIPCMessage<
// CopyDirectoryRequest,
// IpcResponse
// >(COPY_DIRECTORY_CHANNEL, request);
// return destinationPath;
}
public renameDirectory(sourcePath: string, destinationPath: string) {
@@ -159,6 +163,12 @@ export class FileService {
});
}
public async deleteIfExists(filePath: string) {
if (await this.pathExists(filePath)) {
await this.deleteDirectory(filePath);
}
}
public readFile(sourcePath: string): Promise<string> {
return new Promise((resolve, reject) => {
const eventHandler = (_evt: any, arg: ReadFileResponse) => {