Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve webplayer UX #623

Merged
merged 1 commit into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 0 additions & 61 deletions htdocs/ajax.loadControls.php

This file was deleted.

52 changes: 0 additions & 52 deletions htdocs/ajax.loadCover.php

This file was deleted.

89 changes: 0 additions & 89 deletions htdocs/ajax.loadPlaylist.php

This file was deleted.

18 changes: 0 additions & 18 deletions htdocs/ajax.loadTime.php

This file was deleted.

15 changes: 0 additions & 15 deletions htdocs/ajax.loadVolume.php

This file was deleted.

29 changes: 29 additions & 0 deletions htdocs/api/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

function execAndEcho($command) {
$output = execScript($command);
echo(implode('\n', $output));
}

function execScript($command) {
$absoluteCommand = realpath(dirname(__FILE__) .'/../../scripts') ."/{$command}";
return execSuccessfully($absoluteCommand);
}

function execScriptWithoutCheck($command) {
$absoluteCommand = realpath(dirname(__FILE__) .'/../../scripts') ."/{$command}";
exec($absoluteCommand);
}

function execSuccessfully($command) {
exec($command, $output, $rc);
if ($rc != 0) {
$formattedOutput = implode('\n', $output);
echo "Execution failed\nCommand: {$command}\nOutput: {$formattedOutput}\nRC: .${rc}";
http_response_code(500);
exit();
}
return $output;
}

?>
30 changes: 30 additions & 0 deletions htdocs/api/cover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Returns the cover of the currently played folder.
*/

$Audio_Folders_Path = trim(file_get_contents('../../settings/Audio_Folders_Path'));
$Latest_Folder_Played = trim(file_get_contents('../../settings/Latest_Folder_Played'));

$spover = $Audio_Folders_Path."/../../settings/cover.jpg";
$ocover = $Audio_Folders_Path."/".$Latest_Folder_Played."/cover.jpg";
$nocover = "../_assets/img/No_Cover.jpg";

if(file_exists($Audio_Folders_Path.'/'.$Latest_Folder_Played.'/cover.jpg')) {
$filename = $ocover;
} elseif (file_exists($Audio_Folders_Path.'/'.$Latest_Folder_Played.'/spotify.txt')) {
$filename = $spover;
} else {
$filename = $nocover;
}

header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($filename));
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$fp = fopen($filename, 'rb');
fpassthru($fp);

?>
13 changes: 13 additions & 0 deletions htdocs/api/latest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Returns the latest played file, folder and playlist.
*/

$result = array();
$result['folder'] = trim(file_get_contents('../../settings/Latest_Folder_Played'));
$result['file'] = trim(file_get_contents('../../settings/Latest_Played_File'));
$result['playlist'] = trim(file_get_contents('../../settings/Latest_Playlist_Played'));
echo json_encode($result, JSON_PRETTY_PRINT);

header('Content-Type: application/json');
?>
81 changes: 81 additions & 0 deletions htdocs/api/player.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/***
* Allows to control the player by sending a command via PUT like 'play' or 'pause'.
* Retrieves information about the player by sending a GET request.
*/
include 'common.php';

if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
handlePut();
} else if ($_SERVER['REQUEST_METHOD'] === 'GET') {
handleGet();
} else {
http_response_code(405);
}

function handlePut() {
$body = file_get_contents('php://input');
$json = json_decode(trim($body), TRUE);
$inputCommand = $json['command'];
if ($inputCommand != null) {
$controlsCommand = determineCommand($inputCommand);
$execCommand = "playout_controls.sh {$controlsCommand}";
execScript($execCommand);
} else {
echo "Body is missing command";
http_response_code(400);
}
}

function handleGet() {
$statusCommand = "echo 'status\ncurrentsong\nclose' | nc -w 1 localhost 6600";
$execResult = execSuccessfully($statusCommand);
$result = array();
forEach($execResult as $value) {
$exploded = explode(' ', $value);
if (count($exploded) == 2) {
$result[substr(trim($exploded[0]), 0, -1)] = $exploded[1];
}
}
header('Content-Type: application/json');
echo json_encode($result);
}

function determineCommand($body) {
switch ($body) {
case 'play':
return '-c=playerplay';
case 'next':
return '-c=playernext';
case 'prev':
return '-c=playerprev';
case 'replay':
return '-c=playerreplay -v=playlist';
case 'pause':
return '-c=playerpause -v=single';
case 'repeat':
return '-c=playerprev -c=playerrepeat -v=playlist';
case 'single':
return '-c=playerprev -c=playerrepeat -v=single';
case 'repeatoff':
return '-c=playerprev -c=playerrepeat -v=off';
case 'seekBack':
return '-c=playerprev -c=playerseek -v=-15';
case 'seekAhead':
return '-c=playerprev -c=playerseek -v=+15';
case 'stop':
return '-c=playerstop';
case 'mute':
return'-c=mute';
case 'volumeup':
return'-c=volumeup';
case 'volumedown':
return'-c=volumedown';
}
echo "Unknown command {$body}";
http_response_code(400);
exit;
}

?>
Loading