freedombone-app-dlna 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-2016 Bob Mottram <bob@robotics.uk.to>
  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 media'
  31. dlna_variables=(SYSTEM_TYPE
  32. USB_MOUNT
  33. INSTALLED_WITHIN_DOCKER
  34. MY_USERNAME)
  35. function configure_interactive_dlna {
  36. while true
  37. do
  38. data=$(tempfile 2>/dev/null)
  39. trap "rm -f $data" 0 1 2 5 15
  40. dialog --backtitle $"Freedombone Control Panel" \
  41. --title $"Media Menu" \
  42. --radiolist $"Choose an operation:" 13 70 3 \
  43. 1 $"Attach a drive containing playable media" off \
  44. 2 $"Remove a drive containing playable media" off \
  45. 3 $"Exit" on 2> $data
  46. sel=$?
  47. case $sel in
  48. 1) break;;
  49. 255) break;;
  50. esac
  51. case $(cat $data) in
  52. 1) remove-music
  53. attach-music;;
  54. 2) remove-music;;
  55. 3) break;;
  56. esac
  57. done
  58. }
  59. function install_interactive_dlna {
  60. echo -n ''
  61. }
  62. function change_password_dlna {
  63. echo -n ''
  64. }
  65. function reconfigure_dlna {
  66. echo ''
  67. }
  68. function upgrade_dlna {
  69. echo ''
  70. }
  71. function configure_firewall_for_dlna {
  72. if grep -Fxq "configure_firewall_for_dlna" $COMPLETION_FILE; then
  73. return
  74. fi
  75. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  76. # docker does its own firewalling
  77. return
  78. fi
  79. iptables -A INPUT -p udp --dport 1900 -j ACCEPT
  80. iptables -A INPUT -p tcp --dport 8200 -j ACCEPT
  81. function_check save_firewall_settings
  82. save_firewall_settings
  83. OPEN_PORTS+=('DLNA 1900')
  84. OPEN_PORTS+=('DLNA 8200')
  85. echo 'configure_firewall_for_dlna' >> $COMPLETION_FILE
  86. }
  87. function backup_local_dlna {
  88. source_directory=/var/cache/minidlna
  89. if [ -d $source_directory ]; then
  90. dest_directory=dlna
  91. echo $"Backing up $source_directory to $dest_directory"
  92. function_check backup_directory_to_usb
  93. backup_directory_to_usb $source_directory $dest_directory
  94. echo $"Backup to $dest_directory complete"
  95. fi
  96. }
  97. function restore_local_dlna {
  98. if [ -d /var/cache/minidlna ]; then
  99. if [ -d $USB_MOUNT/backup/dlna ]; then
  100. echo $"Restoring DLNA cache"
  101. temp_restore_dir=/root/tempdlna
  102. function_check restore_directory_from_usb
  103. restore_directory_from_usb $temp_restore_dir dlna
  104. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  105. if [ ! "$?" = "0" ]; then
  106. rm -rf $temp_restore_dir
  107. function_check set_user_permissions
  108. set_user_permissions
  109. function_check backup_unmount_drive
  110. backup_unmount_drive
  111. exit 982572
  112. fi
  113. rm -rf $temp_restore_dir
  114. fi
  115. fi
  116. }
  117. function backup_remote_dlna {
  118. if [ -d /var/cache/minidlna ]; then
  119. echo $"Backing up DLNA cache"
  120. backup_directory_to_friend /var/cache/minidlna dlna
  121. echo $"Backup of DLNA cache complete"
  122. fi
  123. }
  124. function restore_remote_dlna {
  125. if [ -d /var/cache/minidlna ]; then
  126. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  127. echo $"Restoring DLNA cache"
  128. temp_restore_dir=/root/tempdlna
  129. function_check restore_directory_from_friend
  130. restore_directory_from_friend $temp_restore_dir dlna
  131. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  132. if [ ! "$?" = "0" ]; then
  133. exit 982
  134. fi
  135. rm -rf $temp_restore_dir
  136. echo $"Restore of DLNA complete"
  137. fi
  138. fi
  139. }
  140. function remove_dlna {
  141. if [[ $(app_is_installed dlna) == "0" ]]; then
  142. return
  143. fi
  144. service minidlna stop
  145. apt-get -y remove --purge minidlna
  146. if [ -f /etc/minidlna.conf ]; then
  147. rm /etc/minidlna.conf
  148. fi
  149. iptables -D INPUT -p udp --dport 1900 -j ACCEPT
  150. iptables -D INPUT -p tcp --dport 8200 -j ACCEPT
  151. function_check save_firewall_settings
  152. save_firewall_settings
  153. sed -i '/install_dlna/d' $COMPLETION_FILE
  154. }
  155. function install_dlna_main {
  156. if grep -Fxq "install_dlna_main" $COMPLETION_FILE; then
  157. return
  158. fi
  159. apt-get -y install minidlna
  160. if [ ! -f /etc/minidlna.conf ]; then
  161. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  162. exit 55
  163. fi
  164. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  165. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  166. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  167. fi
  168. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  169. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  170. fi
  171. if ! grep -q "$USB_MOUNT/Music" /etc/minidlna.conf; then
  172. echo "media_dir=A,$USB_MOUNT/Music" >> /etc/minidlna.conf
  173. fi
  174. if ! grep -q "$USB_MOUNT/Pictures" /etc/minidlna.conf; then
  175. echo "media_dir=P,$USB_MOUNT/Pictures" >> /etc/minidlna.conf
  176. fi
  177. if ! grep -q "$USB_MOUNT/Videos" /etc/minidlna.conf; then
  178. echo "media_dir=V,$USB_MOUNT/Videos" >> /etc/minidlna.conf
  179. fi
  180. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  181. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  182. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  183. else
  184. sed -i 's/#network_interface=/network_interface=$WIFI_INTERFACE/g' /etc/minidlna.conf
  185. fi
  186. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  187. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  188. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  189. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  190. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  191. service minidlna force-reload
  192. service minidlna reload
  193. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  194. if ! grep -q "max_user_watches" $COMPLETION_FILE; then
  195. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  196. fi
  197. /sbin/sysctl -p -q
  198. function_check configure_firewall_for_dlna
  199. configure_firewall_for_dlna
  200. echo 'install_dlna_main' >> $COMPLETION_FILE
  201. }
  202. function script_for_attaching_usb_drive {
  203. if grep -Fxq "script_for_attaching_usb_drive" $COMPLETION_FILE; then
  204. return
  205. fi
  206. echo '#!/bin/bash' > /usr/bin/attach-music
  207. echo 'remove-music' >> /usr/bin/attach-music
  208. echo "if [ ! -d $USB_MOUNT ]; then" >> /usr/bin/attach-music
  209. echo " mkdir $USB_MOUNT" >> /usr/bin/attach-music
  210. echo 'fi' >> /usr/bin/attach-music
  211. echo "mount /dev/sda1 $USB_MOUNT" >> /usr/bin/attach-music
  212. echo "chown root:root $USB_MOUNT" >> /usr/bin/attach-music
  213. echo "chown -R minidlna:minidlna $USB_MOUNT/*" >> /usr/bin/attach-music
  214. echo 'service minidlna restart' >> /usr/bin/attach-music
  215. echo 'minidlnad -R' >> /usr/bin/attach-music
  216. chmod +x /usr/bin/attach-music
  217. ln -s /usr/bin/attach-music /usr/bin/attach-usb
  218. ln -s /usr/bin/attach-music /usr/bin/attach-videos
  219. ln -s /usr/bin/attach-music /usr/bin/attach-pictures
  220. ln -s /usr/bin/attach-music /usr/bin/attach-media
  221. echo '#!/bin/bash' > /usr/bin/remove-music
  222. echo "if [ -d $USB_MOUNT ]; then" >> /usr/bin/remove-music
  223. echo " umount $USB_MOUNT" >> /usr/bin/remove-music
  224. echo " rm -rf $USB_MOUNT" >> /usr/bin/remove-music
  225. echo 'fi' >> /usr/bin/remove-music
  226. chmod +x /usr/bin/remove-music
  227. ln -s /usr/bin/remove-music /usr/bin/detach-music
  228. ln -s /usr/bin/remove-music /usr/bin/detach-usb
  229. ln -s /usr/bin/remove-music /usr/bin/remove-usb
  230. ln -s /usr/bin/remove-music /usr/bin/detach-media
  231. ln -s /usr/bin/remove-music /usr/bin/remove-media
  232. ln -s /usr/bin/remove-music /usr/bin/detach-videos
  233. ln -s /usr/bin/remove-music /usr/bin/remove-videos
  234. ln -s /usr/bin/remove-music /usr/bin/detach-pictures
  235. ln -s /usr/bin/remove-music /usr/bin/remove-pictures
  236. echo 'script_for_attaching_usb_drive' >> $COMPLETION_FILE
  237. }
  238. function install_dlna {
  239. install_dlna_main
  240. script_for_attaching_usb_drive
  241. }
  242. # NOTE: deliberately no exit 0