freedombone-app-dlna 9.2KB

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