mirror of
				https://github.com/Mastermindzh/tidal-hifi.git
				synced 2025-11-04 10:49:26 +01:00 
			
		
		
		
	added express endpoints, a settings service and a media info service
This commit is contained in:
		
							
								
								
									
										36
									
								
								src/scripts/express.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/scripts/express.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
const express = require("express");
 | 
			
		||||
const { mediaInfo } = require("./mediaInfo");
 | 
			
		||||
const settingsModule = require("./settings");
 | 
			
		||||
const globalEvents = require("./../constants/globalEvents");
 | 
			
		||||
const expressModule = {};
 | 
			
		||||
var fs = require("fs");
 | 
			
		||||
 | 
			
		||||
expressModule.run = function(mainWindow) {
 | 
			
		||||
  function handleGlobalEvent(res, action) {
 | 
			
		||||
    mainWindow.webContents.send("globalEvent", action);
 | 
			
		||||
    res.sendStatus(200);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const expressApp = express();
 | 
			
		||||
  expressApp.get("/", (req, res) => res.send("Hello World!"));
 | 
			
		||||
  expressApp.get("/current", (req, res) => res.json(mediaInfo));
 | 
			
		||||
  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("/image", (req, res) => {
 | 
			
		||||
    var stream = fs.createReadStream(mediaInfo.icon);
 | 
			
		||||
    stream.on("open", function() {
 | 
			
		||||
      res.set("Content-Type", "image/png");
 | 
			
		||||
      stream.pipe(res);
 | 
			
		||||
    });
 | 
			
		||||
    stream.on("error", function() {
 | 
			
		||||
      res.set("Content-Type", "text/plain");
 | 
			
		||||
      res.status(404).end("Not found");
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  expressApp.listen(settingsModule.settings.apiSettings.port, () => {});
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = expressModule;
 | 
			
		||||
							
								
								
									
										25
									
								
								src/scripts/mediaInfo.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/scripts/mediaInfo.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
const mediaInfo = {
 | 
			
		||||
  title: "",
 | 
			
		||||
  artist: "",
 | 
			
		||||
  icon: "",
 | 
			
		||||
};
 | 
			
		||||
const mediaInfoModule = {
 | 
			
		||||
  mediaInfo,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
mediaInfoModule.update = function(arg) {
 | 
			
		||||
  mediaInfo.title = propOrDefault(arg.title);
 | 
			
		||||
  mediaInfo.artist = propOrDefault(arg.message);
 | 
			
		||||
  mediaInfo.icon = propOrDefault(arg.icon);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Return the property or a default value
 | 
			
		||||
 * @param {*} prop property to check
 | 
			
		||||
 * @param {*} defaultValue defaults to ""
 | 
			
		||||
 */
 | 
			
		||||
function propOrDefault(prop, defaultValue = "") {
 | 
			
		||||
  return prop ? prop : defaultValue;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = mediaInfoModule;
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
const mediaKeysModule = {
 | 
			
		||||
  play: "MediaPlayPause",
 | 
			
		||||
  next: "MediaNextTrack",
 | 
			
		||||
  previous: "MediaPreviousTrack",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = mediaKeysModule;
 | 
			
		||||
							
								
								
									
										13
									
								
								src/scripts/settings.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/scripts/settings.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
const settings = {
 | 
			
		||||
  notifications: true,
 | 
			
		||||
  api: true,
 | 
			
		||||
  apiSettings: {
 | 
			
		||||
    port: 47836,
 | 
			
		||||
  },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const settingsModule = {
 | 
			
		||||
  settings,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = settingsModule;
 | 
			
		||||
		Reference in New Issue
	
	Block a user