mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2025-02-01 06:52:17 +01:00
do axios requests through the main process
This commit is contained in:
parent
294701ec5f
commit
977e50faa0
8
package-lock.json
generated
8
package-lock.json
generated
@ -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",
|
||||
|
@ -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",
|
||||
|
@ -15,4 +15,6 @@ export const globalEvents = {
|
||||
toggleFavorite: "toggleFavorite",
|
||||
toggleShuffle: "toggleShuffle",
|
||||
toggleRepeat: "toggleRepeat",
|
||||
axios: "axios",
|
||||
axiosReply: "axiosReply",
|
||||
};
|
||||
|
11
src/main.ts
11
src/main.ts
@ -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);
|
||||
});
|
||||
|
@ -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";
|
||||
|
@ -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
src/scripts/downloadPreload.ts
Normal file
23
src/scripts/downloadPreload.ts
Normal 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);
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user