# HG changeset patch # User Christophe Lincoln # Date 1487882915 -3600 # Node ID 7572f9f6a6494d7cedf40fecb94d165a23cc485f # Parent f758d6f609be02ffffa36162d518decee9908c09 Add mediabox cmdline tool and main CGI script diff -r f758d6f609be -r 7572f9f6a649 index.cgi --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/index.cgi Thu Feb 23 21:48:35 2017 +0100 @@ -0,0 +1,281 @@ +#!/bin/sh +# +# CGI/SHell MediaBox using HTML5 features. Coded with lightness and +# elegence in mind. +# +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License +# +. /usr/lib/slitaz/httphelper.sh +header + +# +# Internal variables +# +version="beta" +cache="$PWD/cache" +script="$SCRIPT_NAME" + +# Set $home - Cache user ID and source configs +if [ "$(GET home)" ]; then + echo $(GET home) > ${cache}/home + rm -f ${cache}/audio ${cache}/videos +fi +home=$(cat $cache/home) +config="$home/.config/mediabox" +if [ -f "${config}/mediabox.conf" ]; then + . ${config}/mediabox.conf +else + gettext "Missing config file:"; echo " $config"; exit 1 +fi + +# i18n +export LANG=${LOCALE} LC_ALL=${LOCALE} +. /usr/bin/gettext.sh +export TEXTDOMAIN='mediabox' + +# +# Functions +# + +# Usage: html_header "Page Title" +html_header() { + cat lib/header.html | sed s"/%TITLE%/$1/" + cat << EOT +
+

$1

+EOT +} + +html_footer() { + cat << EOT +
+ ♥ +
+ + +EOT +} + +# Header navigation +nav_menu() { + cat << EOT + +
+EOT +} + +# Page navigation +nav_page() { + cat << EOT + +
+ +
+EOT +} + +# Find and list audio/videos files. +find_audio() { + [ ! -f "${cache}/audio" ] && find "${MUSIC}" \ + -regex '.*\.\(mp3\|ogg\|wav\)' > ${cache}/audio + cat ${cache}/audio +} +find_videos() { + find "${VIDEOS}" -regex '.*\.\(mp4\|ogv\|avi\)' > ${cache}/videos + cat ${cache}/videos +} + +list_audio() { + count="$(wc -l ${cache}/audio | cut -d " " -f 1)" + echo "" +} + +list_videos() { + count="$(wc -l ${cache}/videos | cut -d " " -f 1)" + cat << EOT +" +} + +list_radio() { + if [ ! -f "$config/radio.list" ]; then + cp lib/radio.list ${config} && chmod 0666 ${config}/radio.list + fi + echo "" +} + +list_playlists() { + echo "" +} + +# HTML5 audio/video attributes: autoplay loop controls preload="auto" +# +# Usage: audio_player [/path/audio.ogg|http://url] +audio_player() { + case "$1" in + http://*) + source="$1" title="$2" ;; + *) + filepath="$1" + filename="$(basename "$1")" + title="$(gettext "No track playing")" + [ "$1" ] && title="${filename%.*}" + # We need to get file via http url + source="cache/play/$filename" + rm -rf ${cache}/play && mkdir ${cache}/play + [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename" ;; + esac + cat << EOT +
+
$title
+ +
+EOT +} + +playlist_player() { + playlist="$1" +} + +# Usage: video_player "/path/video.mp4" +video_player() { + filepath="$1" + filename="$(basename "$1")" + title="$(gettext "No video playing")" + [ "$1" ] && title="${filename%.*}" + # We need to get file via http url + rm -rf ${cache}/play && mkdir ${cache}/play + [ "$filename" ] && ln -s "$filepath" "$cache/play/$filename" + cat << EOT +
+ +
$title
+
+EOT +} + +# +# Media Box Tools +# + +case " $(GET) " in + *\ music\ *) + html_header "$(gettext "Music")" + nav_menu + audio_player "$(GET play)" + list_audio + html_footer ;; + + *\ videos\ *) + html_header "$(gettext "Videos")" + nav_menu + video_player "$(GET play)" + list_videos + html_footer ;; + + *\ playlists\ *) + html_header "$(gettext "Playlists")" + nav_menu + playlist_player "$(GET play)" "$(GET info)" + list_playlists + html_footer ;; + + *\ radio\ *) + html_header "$(gettext "Radio")" + nav_menu + audio_player "$(GET play)" "$(GET info)" + list_radio + html_footer ;; + + *\ settings\ *) + html_header "$(gettext "Settings")" + nav_menu + cat << EOT +
+
+Version     : $version
+Cache       : $(du -sh $cache | cut -d "	" -f 1)
+Language    : $LOCALE
+Config      : $config
+Music       : $MUSIC
+Videos      : $VIDEOS
+EOT
+		echo -n "Tools       : "
+		# Only small nd light tools!
+		for tool in mediainfo normalize sox
+		do
+			if [ -x "/usr/bin/$tool" ]; then
+				echo -n "$tool "
+			fi
+		done
+		cat << EOT
+
+
+$(cat README)
+
+EOT + # End of
+ echo "
" + html_footer ;; + + *) + # Home page + html_header "MediaBox" + nav_page + html_footer ;; +esac && exit 0 diff -r f758d6f609be -r 7572f9f6a649 mediabox --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mediabox Thu Feb 23 21:48:35 2017 +0100 @@ -0,0 +1,84 @@ +#!/bin/sh +# +# Cmdline tool to run, debug and config CGI/SHell MediaBox +# +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License +# + +config="$HOME/.config/mediabox" +playlists="$config/playlists" +#url="http://localhost/cgi-bin/mediabox/index.cgi" +url="http://localhost/~pankso/cgi-bin/mediabox/index.cgi" + +# Internationalization +. /usr/bin/gettext.sh +export TEXTDOMAIN='mediabox' + +help() { + cat << EOT +$(gettext "MediaBox cmdline tool") + +$(gettext "Usage:") $(basename $0) [file|command] [file] +$(gettext "Commands:") + add-playlist Add a playlist into MediaBox database + +$(gettext "Examples:") + $(basename $0) path/to/Music/audio.ogg + $(basename $0) add-playlist path/to/playlist.m3u + +EOT +} + +init() { + install -m 0777 -d ${config} + install -m 0777 -d ${playlists} + cat > ${config}/mediabox.conf << EOT +# CGI/SHell MediaBox configuration file + +LOCALE="$(locale | grep LANG= | cut -d "=" -f 2)" + +MUSIC="$HOME/Music" +VIDEOS="$HOME/Videos" + +EOT + echo "favorites.list|Favorites" > ${config}/playlists.list + touch ${playlists}/favorites.m3u + chmod 0666 \ + ${config}/mediabox.conf \ + ${config}/playlists.list \ + ${playlists}/favorites.m3u +} + +# +# Handle commands|files|urls +# +case "$1" in + + add-playlist) + [ ! -f "$2" ] && help && exit 0 + file=$(basename "$2") + count=$(wc -l "$2" | cut -d ' ' -f 1) + gettext "Adding playlist:"; echo -n " "; basename "$2" + gettext "Playlist tracks:"; echo " $count" + cp -f "$2" ${playlists} || exit 1 + cat >> ${config}/playlists.list << EOT +$file|$(echo ${file%.m3u}) +EOT + ;; + + *.m3u) + tazweb "${url}?playlists&play=$1" & ;; + + *.avi) + tazweb "${url}?videos&play=$1" & ;; + + *.mp3|*.ogg|*.wav) + tazweb "${url}?music&play=$1" & ;; + + "") + [ -f "$config/mediabox.conf" ] || init + tazweb --notoolbar "${url}?home=$HOME" & ;; + + *) help ;; + +esac && exit 0