From: Christian Weiske Date: Sat, 28 Nov 2015 13:27:36 +0000 (+0100) Subject: add single file mode for children X-Git-Url: https://git.cweiske.de/noxon-gateway.git/commitdiff_plain/17a8cebbb823df5940d09134999ccc4c08b208ea add single file mode for children --- diff --git a/src/mediatomb.php b/src/mediatomb.php index e582a46..f5c4977 100644 --- a/src/mediatomb.php +++ b/src/mediatomb.php @@ -1,44 +1,124 @@ getMessage()); + return; + } +} - $path = substr($fullPath, strlen($prefix)); - $container = $smt->getContainerByPath($path); - $listItems = array(); - addPreviousItem($listItems, $fullPath); +function mediatombBrowse(Services_MediaTomb $smt, $fullPath, $prefix) +{ + global $mediatomb; + + $path = substr($fullPath, strlen($prefix)); + $container = $smt->getContainerByPath($path); + $listItems = array(); + addPreviousItem($listItems, $fullPath); - foreach ($container->getContainers() as $subContainer) { + $it = $container->getItemIterator(false); + $it->rewind(); + $hasFiles = $it->valid(); + if ($hasFiles && is_array($mediatomb['singleFileDirectories'])) { + $enableSingle = false; + foreach ($mediatomb['singleFileDirectories'] as $dir) { + if (substr($fullPath, 0, strlen($dir)) == $dir) { + $enableSingle = true; + } + } + if ($enableSingle) { $listItems[] = getDirItem( - $subContainer->title, - pathEncode($fullPath . $subContainer->title) . '/' + 'Einzeln', + '.mt-single/' . $path ); } + } + + foreach ($container->getContainers() as $subContainer) { + $listItems[] = getDirItem( + $subContainer->title, + pathEncode($fullPath . $subContainer->title) . '/' + ); + } + + foreach ($container->getItemIterator(false) as $item) { + mediatombAddFile($listItems, $item); + } + + sendListItems($listItems); +} + +function mediatombAddFile(&$listItems, $item) +{ + global $host1; + + $di = $item->getDetailedItem(); + $itemUrl = $item->url; + if ($di->mimetype !== 'audio/mpeg') { + //noxon iRadio cube does not want to play .ogg files + $itemUrl = $host1 . 'transcode-nocache.php' + . '?url=' . urlencode($itemUrl); + } + $listItems[] = getEpisodeItem( + $item->title, + $itemUrl, + '', + 'MP3' + ); +} + +/** + * Single file mode - shows directories that only have a single file in them. + * Each audio file gets its own virtual directory, containing only the + * audio file itself. + * + * Useful children who want to listen a single story before sleeping, + * but the noxon's auto switch-off timer is not exactly at the end of + * the story. So the next story starts already, and the kid complains + * that it wanted to listen to that as well... + */ +function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix) +{ + $path = substr($fullPath, strlen($prefix)); + + $parts = explode('/', $path); + $fileMode = false; + if (substr(end($parts), 0, 5) == 'file-') { + $fileMode = true; + $fileTitle = substr(end($parts), 5); + $path = substr($path, 0, -strlen($fileTitle) - 5); + } + + $container = $smt->getContainerByPath($path); + $listItems = array(); + if ($fileMode) { + //show single file to play + addPreviousItem($listItems, $fullPath); + $item = $smt->getSingleItem($container, $fileTitle, false); + mediatombAddFile($listItems, $item); + } else { + addPreviousItem($listItems, 'internetradio/' . $path . '/dummy'); + + //browse directory foreach ($container->getItemIterator(false) as $item) { - $di = $item->getDetailedItem(); - $itemUrl = $item->url; - if ($di->mimetype !== 'audio/mpeg') { - //noxon iRadio cube does not want to play .ogg files - $itemUrl = $host1 . 'transcode-nocache.php' - . '?url=' . urlencode($itemUrl); - } - $listItems[] = getEpisodeItem( + $listItems[] = getDirItem( $item->title, - $itemUrl, - '', - 'MP3' + $fullPath . 'file-' . $item->title ); } - } catch (Exception $e) { - sendMessage('Mediatomb error: ' . $e->getMessage()); - return; } sendListItems($listItems); diff --git a/var/.mt-single/.keep b/var/.mt-single/.keep new file mode 100644 index 0000000..e69de29 diff --git a/www/index.php b/www/index.php index 02dd4e8..d9b53f1 100644 --- a/www/index.php +++ b/www/index.php @@ -37,7 +37,11 @@ function handleRequest($path) if (substr($path, 0, 14) == 'internetradio/') { require_once 'mediatomb.php'; - handleRequestMediatomb($path, 'internetradio/'); + handleMediatomb('browse', $path, 'internetradio/'); + return; + } else if (substr($path, 0, 11) == '.mt-single/') { + require_once 'mediatomb.php'; + handleMediatomb('single', $path, '.mt-single/'); return; }