freedombone-app-dlna 9.2KB

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