do axios requests through the main process

This commit is contained in:
Rowanda 2024-12-12 20:35:55 -08:00
parent 294701ec5f
commit 977e50faa0
7 changed files with 49 additions and 12 deletions

8
package-lock.json generated
View File

@ -12,7 +12,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",
@ -2320,9 +2320,9 @@
}
},
"node_modules/axios": {
"version": "1.7.8",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.8.tgz",
"integrity": "sha512-Uu0wb7KNqK2t5K+YQyVCLM76prD5sRFjKHbJYCP1J7JFGEQ6nN7HWn9+04LAeiJ3ji54lgS/gZCH1oxyrf1SPw==",
"version": "1.7.9",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz",
"integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",

View File

@ -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",

View File

@ -15,4 +15,6 @@ export const globalEvents = {
toggleFavorite: "toggleFavorite",
toggleShuffle: "toggleShuffle",
toggleRepeat: "toggleRepeat",
axios: "axios",
axiosReply: "axiosReply",
};

View File

@ -24,6 +24,7 @@ import {
showSettingsWindow,
} from "./scripts/settings";
import { addTray, refreshTray } from "./scripts/tray";
import { downloadFile } from "./scripts/download";
let mainInhibitorId = -1;
initialize();
@ -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);
});

View File

@ -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";

View File

@ -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("error", reject);
})
.catch(reject);
res.data.on("error", reject);
}).catch(reject);
});
};

View File

@ -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);
});
};