freedombone-app-dlna 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # DLNA application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim media'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=0
  33. dlna_variables=(SYSTEM_TYPE
  34. USB_MOUNT_DLNA
  35. INSTALLED_WITHIN_DOCKER
  36. MY_USERNAME)
  37. function logging_on_dlna {
  38. echo -n ''
  39. }
  40. function logging_off_dlna {
  41. echo -n ''
  42. }
  43. function configure_interactive_dlna {
  44. while true
  45. do
  46. data=$(mktemp 2>/dev/null)
  47. dialog --backtitle $"Freedombone Control Panel" \
  48. --title $"Media Menu" \
  49. --radiolist $"Choose an operation:" 13 70 3 \
  50. 1 $"Attach a drive containing playable media" off \
  51. 2 $"Remove a drive containing playable media" off \
  52. 3 $"Exit" on 2> "$data"
  53. sel=$?
  54. case $sel in
  55. 1) rm -f "$data"
  56. break;;
  57. 255) rm -f "$data"
  58. break;;
  59. esac
  60. case $(cat "$data") in
  61. 1) attach-music;;
  62. 2) remove-music;;
  63. 3) rm -f "$data"
  64. break;;
  65. esac
  66. rm -f "$data"
  67. done
  68. }
  69. function install_interactive_dlna {
  70. echo -n ''
  71. APP_INSTALLED=1
  72. }
  73. function reconfigure_dlna {
  74. echo ''
  75. }
  76. function upgrade_dlna {
  77. echo ''
  78. }
  79. function configure_firewall_for_dlna {
  80. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  81. return
  82. fi
  83. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  84. # docker does its own firewalling
  85. return
  86. fi
  87. firewall_add DLNA 1900 udp
  88. firewall_add DLNA 8200 tcp
  89. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  90. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  91. echo '<service-group>';
  92. echo ' <name replace-wildcards="yes">%h DLNA</name>';
  93. echo ' <service>';
  94. echo ' <type>_dlna._tcp</type>';
  95. echo " <port>8200</port>";
  96. echo ' </service>';
  97. echo ' <service>';
  98. echo ' <type>_dlna._udp</type>';
  99. echo " <port>1900</port>";
  100. echo ' </service>';
  101. echo '</service-group>'; } > /etc/avahi/services/dlna.service
  102. systemctl restart avahi-daemon
  103. mark_completed "${FUNCNAME[0]}"
  104. }
  105. function backup_local_dlna {
  106. source_directory=/var/cache/minidlna
  107. if [ -d $source_directory ]; then
  108. dest_directory=dlna
  109. function_check backup_directory_to_usb
  110. backup_directory_to_usb $source_directory $dest_directory
  111. fi
  112. }
  113. function restore_local_dlna {
  114. if [ -d /var/cache/minidlna ]; then
  115. if [ -d "$USB_MOUNT_DLNA/backup/dlna" ]; then
  116. echo $"Restoring DLNA cache"
  117. temp_restore_dir=/root/tempdlna
  118. function_check restore_directory_from_usb
  119. restore_directory_from_usb $temp_restore_dir dlna
  120. if [ -d $temp_restore_dir/var/cache/minidlna ]; then
  121. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  122. else
  123. cp -r $temp_restore_dir/* /var/cache/minidlna/
  124. fi
  125. # shellcheck disable=SC2181
  126. if [ ! "$?" = "0" ]; then
  127. rm -rf $temp_restore_dir
  128. function_check set_user_permissions
  129. set_user_permissions
  130. function_check backup_unmount_drive
  131. backup_unmount_drive
  132. exit 982572
  133. fi
  134. rm -rf $temp_restore_dir
  135. fi
  136. fi
  137. }
  138. function backup_remote_dlna {
  139. if [ -d /var/cache/minidlna ]; then
  140. backup_directory_to_friend /var/cache/minidlna dlna
  141. fi
  142. }
  143. function restore_remote_dlna {
  144. if [ -d /var/cache/minidlna ]; then
  145. if [ -d "$SERVER_DIRECTORY/backup/dlna" ]; then
  146. temp_restore_dir=/root/tempdlna
  147. function_check restore_directory_from_friend
  148. restore_directory_from_friend $temp_restore_dir dlna
  149. if [ -d $temp_restore_dir/var/cache/minidlna ]; then
  150. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  151. else
  152. cp -r $temp_restore_dir/* /var/cache/minidlna/
  153. fi
  154. # shellcheck disable=SC2181
  155. if [ ! "$?" = "0" ]; then
  156. exit 982
  157. fi
  158. rm -rf $temp_restore_dir
  159. fi
  160. fi
  161. }
  162. function remove_dlna {
  163. systemctl stop minidlna
  164. apt-get -yq remove --purge minidlna
  165. if [ -f /etc/minidlna.conf ]; then
  166. rm /etc/minidlna.conf
  167. fi
  168. rm /usr/bin/attach-music
  169. rm /usr/bin/remove-music
  170. remove_completion_param install_dlna
  171. firewall_remove 1900 udp
  172. firewall_remove 8200 tcp
  173. rm /etc/avahi/services/dlna.service
  174. systemctl restart avahi-daemon
  175. }
  176. function install_dlna_main {
  177. if [[ $(app_is_installed dlna_main) == "1" ]]; then
  178. return
  179. fi
  180. apt-get -yq install minidlna
  181. if [ ! -f /etc/minidlna.conf ]; then
  182. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  183. exit 55
  184. fi
  185. if [ ! "$USB_MOUNT_DLNA" ]; then
  186. USB_MOUNT_DLNA=/mnt/dlna
  187. fi
  188. if [ ${#USB_MOUNT_DLNA} -eq 0 ]; then
  189. USB_MOUNT_DLNA=/mnt/dlna
  190. fi
  191. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  192. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  193. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  194. fi
  195. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  196. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  197. fi
  198. if ! grep -q "$USB_MOUNT_DLNA/Music" /etc/minidlna.conf; then
  199. echo "media_dir=A,$USB_MOUNT_DLNA/Music" >> /etc/minidlna.conf
  200. fi
  201. if ! grep -q "$USB_MOUNT_DLNA/Pictures" /etc/minidlna.conf; then
  202. echo "media_dir=P,$USB_MOUNT_DLNA/Pictures" >> /etc/minidlna.conf
  203. fi
  204. if ! grep -q "$USB_MOUNT_DLNA/Videos" /etc/minidlna.conf; then
  205. echo "media_dir=V,$USB_MOUNT_DLNA/Videos" >> /etc/minidlna.conf
  206. fi
  207. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  208. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  209. if [[ $(config_param_exists WIFI_INTERFACE) == "0" ]]; then
  210. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  211. else
  212. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  213. fi
  214. else
  215. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  216. fi
  217. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  218. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  219. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  220. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  221. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  222. systemctl reload minidlna
  223. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  224. if ! grep -q "max_user_watches" "$COMPLETION_FILE"; then
  225. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  226. fi
  227. /sbin/sysctl -p -q
  228. function_check configure_firewall_for_dlna
  229. configure_firewall_for_dlna
  230. install_completed dlna_main
  231. }
  232. function script_for_attaching_usb_drive {
  233. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  234. return
  235. fi
  236. { echo '#!/bin/bash';
  237. echo "source /usr/local/bin/${PROJECT_NAME}-vars";
  238. echo "UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*";
  239. echo '';
  240. echo "for f in \$UTILS_FILES";
  241. echo 'do';
  242. echo " source \$f";
  243. echo 'done';
  244. echo '';
  245. echo 'USB_DRIVE=/dev/sda1';
  246. echo 'detect_usb_drive';
  247. echo '';
  248. echo 'remove-music';
  249. echo "if [ ! -d $USB_MOUNT_DLNA ]; then";
  250. echo " mkdir $USB_MOUNT_DLNA";
  251. echo 'fi';
  252. echo -n "mount \$USB_DRIVE ";
  253. echo "$USB_MOUNT_DLNA";
  254. echo "chown root:root $USB_MOUNT_DLNA";
  255. echo "chown -R minidlna:minidlna $USB_MOUNT_DLNA/*";
  256. echo 'systemctl restart minidlna';
  257. echo 'minidlnad -R';
  258. echo 'exit 0'; } > /usr/bin/attach-music
  259. chmod +x /usr/bin/attach-music
  260. { echo '#!/bin/bash';
  261. echo "if [ -d $USB_MOUNT_DLNA ]; then";
  262. echo " umount $USB_MOUNT_DLNA";
  263. echo " rm -rf $USB_MOUNT_DLNA";
  264. echo 'fi';
  265. echo 'exit 0'; } > /usr/bin/remove-music
  266. chmod +x /usr/bin/remove-music
  267. mark_completed "${FUNCNAME[0]}"
  268. }
  269. function install_dlna {
  270. install_dlna_main
  271. script_for_attaching_usb_drive
  272. APP_INSTALLED=1
  273. }
  274. # NOTE: deliberately no exit 0