Skip to content

Commit

Permalink
do axios requests through the main process
Browse files Browse the repository at this point in the history
  • Loading branch information
r0wanda committed Dec 13, 2024
1 parent 294701e commit 977e50f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@electron/remote": "^2.1.2",
"@types/swagger-jsdoc": "^6.0.4",
"@xhayper/discord-rpc": "^1.2.0",
"axios": "^1.7.8",
"axios": "^1.7.9",
"cors": "^2.8.5",
"electron-store": "^8.2.0",
"express": "^4.21.2",
Expand Down
2 changes: 2 additions & 0 deletions src/constants/globalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export const globalEvents = {
toggleFavorite: "toggleFavorite",
toggleShuffle: "toggleShuffle",
toggleRepeat: "toggleRepeat",
axios: "axios",
axiosReply: "axiosReply",
};
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
showSettingsWindow,
} from "./scripts/settings";
import { addTray, refreshTray } from "./scripts/tray";
import { downloadFile } from "./scripts/download";
let mainInhibitorId = -1;

initialize();
Expand Down Expand Up @@ -250,6 +251,16 @@ ipcMain.on(globalEvents.error, (event) => {
console.log(event);
});

ipcMain.on(globalEvents.axios, (event, fileUrl: string, targetPath: string) => {
const download = downloadFile(fileUrl, targetPath);
download.then(() => {
event.reply(globalEvents.axiosReply, fileUrl, targetPath, false);
}).catch(() => {
// don't send error information, it isn't used anyways
event.reply(globalEvents.axiosReply, fileUrl, targetPath, true);
});
});

ipcMain.handle(globalEvents.getUniversalLink, async (event, url) => {
return SharingService.getUniversalLink(url);
});
Expand Down
2 changes: 1 addition & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { convertDurationToSeconds } from "./features/time/parse";
import { MediaInfo } from "./models/mediaInfo";
import { MediaStatus } from "./models/mediaStatus";
import { RepeatState } from "./models/repeatState";
import { downloadFile } from "./scripts/download";
import { downloadFile } from "./scripts/downloadPreload";
import { addHotkey } from "./scripts/hotkeys";
import { ObjectToDotNotation } from "./scripts/objectUtilities";
import { settingsStore } from "./scripts/settings";
Expand Down
13 changes: 7 additions & 6 deletions src/scripts/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export const downloadFile = function (fileUrl: string, targetPath: string) {
.get(fileUrl, {
responseType: "stream",
})
.then((req) => {
.then((res) => {
const out = fs.createWriteStream(targetPath);

req.data.pipe(out);
res.data.pipe(out);

out.on("finish", resolve);
out.on("finish", resolve);

out.on("error", reject);
})
.catch(reject);
out.on("error", reject);

res.data.on("error", reject);
}).catch(reject);
});
};
23 changes: 23 additions & 0 deletions src/scripts/downloadPreload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ipcRenderer } from "electron";
import { globalEvents } from "../constants/globalEvents";

/**
* download and save a file (renderer version)
* @param {string} fileUrl url to download
* @param {string} targetPath path to save it at
*/
export const downloadFile = function (fileUrl: string, targetPath: string) {
return new Promise<void>((resolve, reject) => {
const handler = (event: Electron.IpcRendererEvent, newFileUrl: string, newTargetPath: string, error: boolean) => {
// it's possible for 2 requests to be running at the same time, so make sure it is the right one
// if there is 2 requests with the same fileUrl and targetPath, then it doesn't matter which one we intercept because the data is the same
if (fileUrl === newFileUrl && targetPath === newTargetPath) {
ipcRenderer.removeListener(globalEvents.axiosReply, handler);
if (error) reject();
else resolve();
}
}
ipcRenderer.on(globalEvents.axiosReply, handler);
ipcRenderer.send(globalEvents.axios, fileUrl, targetPath);
});
};

0 comments on commit 977e50f

Please sign in to comment.