Skip to content

Commit

Permalink
File local options handling works
Browse files Browse the repository at this point in the history
  • Loading branch information
husudosu committed Nov 26, 2021
1 parent b743c44 commit cedd1b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

## 1.0.4

- Load file improvements, way to set http-header-fields, needed for playing data from streaming services
- Load file improvements, way to set http-header-fields, needed for playing data from streaming services.
31 changes: 20 additions & 11 deletions mpvremote/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,43 @@ mp.command_native_async(
}
);

function setFileLocalOptions(options) {
for (var key in options) {
if (options.hasOwnProperty(key)) {
mp.msg.info(key);
mp.msg.info(options[key]);

mp.set_property(key, options[key]);
}
}
}
/* For handling file-local-option
Currently storing file-local-options via file-local-options.txt
because have to get file-local-options before the file fully loaded.
*/
mp.add_hook("on_load", 50, function () {
try {
/* TODO: How to handle files
MPV prop: stream-open-filename
/*
JSON structure should be something like this:
[
{filename: "file or url", "file-local-options": {"http-header-fields": ["A: B"]}},
{filename: "file or url", "file-local-options": {"media-title": "Testt"}},
]
{
"filename1": {"http-header-fields": ["test: a", "test1: b"]},
"filename2": {"http-header-fields": ["test: a", "test1: b"]}
}
*/
mp.set_property("force-media-title", "");
var scriptDir = mp.get_script_directory();

var fileName = mp.get_property("path");
var fileLocalOptions = mp.utils.read_file(
scriptDir + "/" + "file-local-options.txt"
);
fileLocalOptions = JSON.parse(fileLocalOptions);

// Find filename in the file-local-options
for (var key in fileLocalOptions) {
if (fileLocalOptions.hasOwnProperty(key)) {
mp.set_property(key, fileLocalOptions[key]);
}
if (key === fileName) setFileLocalOptions(fileLocalOptions[key]);
}
} catch (exc) {
console.log(exc);
mp.msg.info(exc);
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpv-remote",
"version": "1.0.3",
"version": "1.0.4",
"description": "MPV remote control",
"main": "./remote.socketio.js",
"scripts": {
Expand Down
25 changes: 20 additions & 5 deletions remoteServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const UNIX_REBOOT_COMMAND = "/usr/sbin/reboot";

const { initDB, getScriptFolder } = require("./crud");

const FILE_LOCAL_OPTIONS_PATH = path.join(
getScriptFolder(),
"mpvremote",
"file-local-options.txt"
);

const filebrowser = require("./filebrowser");
const collections = require("./collections");
const { detectFileType } = require("./filebrowser");
Expand Down Expand Up @@ -345,11 +351,15 @@ app.get("/api/v1/playlist", async (req, res) => {

async function writeFileLocalOptions(options) {
await fs_async.writeFile(
path.join(getScriptFolder(), "mpvremote", "file-local-options.txt"),
FILE_LOCAL_OPTIONS_PATH,
JSON.stringify(options),
"utf-8"
);
console.log("File local options written");
}

async function readFileLocalOptions() {
const content = await fs_async.readFile(FILE_LOCAL_OPTIONS_PATH, "utf-8");
return JSON.parse(content);
}

app.post("/api/v1/playlist", async (req, res) => {
Expand All @@ -368,10 +378,13 @@ app.post("/api/v1/playlist", async (req, res) => {
}
} else {
if (req.body["file-local-options"]) {
console.log("Settin file local options");
console.log(JSON.stringify(req.body["file-local-options"]));
let fileLocalOptions = await readFileLocalOptions();
fileLocalOptions[req.body.filename] = req.body["file-local-options"];
console.log(
`File local options has been set: ${JSON.stringify(fileLocalOptions)}`
);
// Have to write cach file here
await writeFileLocalOptions(req.body["file-local-options"]);
await writeFileLocalOptions(fileLocalOptions);
}
// Clear file local-options file
// else writeFileLocalOptions({});
Expand Down Expand Up @@ -761,6 +774,8 @@ app.listen(settings.serverPort, () => {

async function main() {
try {
// Creates/clears file local options file.
writeFileLocalOptions({});
await mpv.start();

if (settings.uselocaldb) await initDB();
Expand Down

0 comments on commit cedd1b8

Please sign in to comment.