2023-05-07 15:45:45 +02:00
|
|
|
import { BrowserWindow, dialog } from "electron";
|
2023-05-01 23:23:43 +02:00
|
|
|
import express, { Response } from "express";
|
|
|
|
import fs from "fs";
|
2023-05-07 15:45:45 +02:00
|
|
|
import globalEvents from "./../constants/globalEvents";
|
|
|
|
import statuses from "./../constants/statuses";
|
|
|
|
import { mediaInfo } from "./mediaInfo";
|
|
|
|
import { settingsStore } from "./settings";
|
|
|
|
import { settings } from "../constants/settings";
|
2019-10-30 22:49:04 +01:00
|
|
|
|
2019-10-30 23:42:08 +01:00
|
|
|
/**
|
|
|
|
* Function to enable tidal-hifi's express api
|
|
|
|
*/
|
2023-05-01 23:23:43 +02:00
|
|
|
|
|
|
|
// expressModule.run = function (mainWindow)
|
|
|
|
export const startExpress = (mainWindow: BrowserWindow) => {
|
2019-10-30 23:42:08 +01:00
|
|
|
/**
|
|
|
|
* Shorthand to handle a fire and forget global event
|
|
|
|
* @param {*} res
|
|
|
|
* @param {*} action
|
|
|
|
*/
|
2023-05-01 23:23:43 +02:00
|
|
|
function handleGlobalEvent(res: Response, action: any) {
|
2019-10-30 22:49:04 +01:00
|
|
|
mainWindow.webContents.send("globalEvent", action);
|
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
const expressApp = express();
|
|
|
|
expressApp.get("/", (req, res) => res.send("Hello World!"));
|
2023-04-27 14:29:28 +02:00
|
|
|
expressApp.get("/current", (req, res) => res.json({ ...mediaInfo, artist: mediaInfo.artists }));
|
2019-10-30 22:49:04 +01:00
|
|
|
expressApp.get("/image", (req, res) => {
|
2023-05-01 23:23:43 +02:00
|
|
|
const stream = fs.createReadStream(mediaInfo.icon);
|
2023-04-27 14:29:28 +02:00
|
|
|
stream.on("open", function () {
|
2019-10-30 22:49:04 +01:00
|
|
|
res.set("Content-Type", "image/png");
|
|
|
|
stream.pipe(res);
|
|
|
|
});
|
2023-04-27 14:29:28 +02:00
|
|
|
stream.on("error", function () {
|
2019-10-30 22:49:04 +01:00
|
|
|
res.set("Content-Type", "text/plain");
|
|
|
|
res.status(404).end("Not found");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-07 15:45:45 +02:00
|
|
|
if (settingsStore.get(settings.playBackControl)) {
|
2019-11-03 18:52:15 +01:00
|
|
|
expressApp.get("/play", (req, res) => handleGlobalEvent(res, globalEvents.play));
|
|
|
|
expressApp.get("/pause", (req, res) => handleGlobalEvent(res, globalEvents.pause));
|
|
|
|
expressApp.get("/next", (req, res) => handleGlobalEvent(res, globalEvents.next));
|
|
|
|
expressApp.get("/previous", (req, res) => handleGlobalEvent(res, globalEvents.previous));
|
|
|
|
expressApp.get("/playpause", (req, res) => {
|
|
|
|
if (mediaInfo.status == statuses.playing) {
|
|
|
|
handleGlobalEvent(res, globalEvents.pause);
|
|
|
|
} else {
|
|
|
|
handleGlobalEvent(res, globalEvents.play);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-07 15:45:45 +02:00
|
|
|
const port = settingsStore.get<string, number>(settings.apiSettings.port);
|
2019-10-30 22:49:04 +01:00
|
|
|
|
2023-05-07 15:45:45 +02:00
|
|
|
const expressInstance = expressApp.listen(port, "127.0.0.1");
|
2023-05-01 23:23:43 +02:00
|
|
|
expressInstance.on("error", function (e: { code: string }) {
|
|
|
|
let message = e.code;
|
|
|
|
if (e.code === "EADDRINUSE") {
|
|
|
|
message = `Port ${port} in use.`;
|
|
|
|
}
|
2023-05-07 15:45:45 +02:00
|
|
|
|
2023-05-01 23:23:43 +02:00
|
|
|
dialog.showErrorBox("Api failed to start.", message);
|
|
|
|
});
|
|
|
|
};
|