freedombone-app-dlna 9.1KB

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