freedombone-app-dlna 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_SHORT_DESCRIPTION=$'DLNA media'
  32. DLNA_DESCRIPTION=$'DLNA media'
  33. DLNA_MOBILE_APP_URL=
  34. dlna_variables=(SYSTEM_TYPE
  35. USB_MOUNT_DLNA
  36. INSTALLED_WITHIN_DOCKER
  37. MY_USERNAME)
  38. function logging_on_dlna {
  39. echo -n ''
  40. }
  41. function logging_off_dlna {
  42. echo -n ''
  43. }
  44. function configure_interactive_dlna {
  45. W=(1 $"Attach a drive containing playable media"
  46. 2 $"Remove a drive containing playable media")
  47. while true
  48. do
  49. # shellcheck disable=SC2068
  50. 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)
  51. if [ ! "$selection" ]; then
  52. break
  53. fi
  54. case $selection in
  55. 1) attach-music;;
  56. 2) remove-music;;
  57. esac
  58. done
  59. }
  60. function install_interactive_dlna {
  61. echo -n ''
  62. APP_INSTALLED=1
  63. }
  64. function reconfigure_dlna {
  65. echo ''
  66. }
  67. function upgrade_dlna {
  68. echo ''
  69. }
  70. function configure_firewall_for_dlna {
  71. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  72. return
  73. fi
  74. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  75. # docker does its own firewalling
  76. return
  77. fi
  78. firewall_add DLNA 1900 udp
  79. firewall_add DLNA 8200 tcp
  80. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  81. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  82. echo '<service-group>';
  83. echo ' <name replace-wildcards="yes">%h DLNA</name>';
  84. echo ' <service>';
  85. echo ' <type>_dlna._tcp</type>';
  86. echo " <port>8200</port>";
  87. echo ' </service>';
  88. echo ' <service>';
  89. echo ' <type>_dlna._udp</type>';
  90. echo " <port>1900</port>";
  91. echo ' </service>';
  92. echo '</service-group>'; } > /etc/avahi/services/dlna.service
  93. systemctl restart avahi-daemon
  94. mark_completed "${FUNCNAME[0]}"
  95. }
  96. function backup_local_dlna {
  97. source_directory=/var/cache/minidlna
  98. if [ -d $source_directory ]; then
  99. dest_directory=dlna
  100. function_check backup_directory_to_usb
  101. backup_directory_to_usb $source_directory $dest_directory
  102. fi
  103. }
  104. function restore_local_dlna {
  105. if [ -d /var/cache/minidlna ]; then
  106. if [ -d "$USB_MOUNT_DLNA/backup/dlna" ]; then
  107. echo $"Restoring DLNA cache"
  108. temp_restore_dir=/root/tempdlna
  109. function_check restore_directory_from_usb
  110. restore_directory_from_usb $temp_restore_dir dlna
  111. if [ -d $temp_restore_dir/var/cache/minidlna ]; then
  112. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  113. else
  114. cp -r $temp_restore_dir/* /var/cache/minidlna/
  115. fi
  116. # shellcheck disable=SC2181
  117. if [ ! "$?" = "0" ]; then
  118. rm -rf $temp_restore_dir
  119. function_check set_user_permissions
  120. set_user_permissions
  121. function_check backup_unmount_drive
  122. backup_unmount_drive
  123. exit 982572
  124. fi
  125. rm -rf $temp_restore_dir
  126. fi
  127. fi
  128. }
  129. function backup_remote_dlna {
  130. if [ -d /var/cache/minidlna ]; then
  131. backup_directory_to_friend /var/cache/minidlna dlna
  132. fi
  133. }
  134. function restore_remote_dlna {
  135. if [ -d /var/cache/minidlna ]; then
  136. if [ -d "$SERVER_DIRECTORY/backup/dlna" ]; then
  137. temp_restore_dir=/root/tempdlna
  138. function_check restore_directory_from_friend
  139. restore_directory_from_friend $temp_restore_dir dlna
  140. if [ -d $temp_restore_dir/var/cache/minidlna ]; then
  141. cp -r $temp_restore_dir/var/cache/minidlna/* /var/cache/minidlna/
  142. else
  143. cp -r $temp_restore_dir/* /var/cache/minidlna/
  144. fi
  145. # shellcheck disable=SC2181
  146. if [ ! "$?" = "0" ]; then
  147. exit 982
  148. fi
  149. rm -rf $temp_restore_dir
  150. fi
  151. fi
  152. }
  153. function remove_dlna {
  154. systemctl stop minidlna
  155. apt-get -yq remove --purge minidlna
  156. if [ -f /etc/minidlna.conf ]; then
  157. rm /etc/minidlna.conf
  158. fi
  159. rm /usr/bin/attach-music
  160. rm /usr/bin/remove-music
  161. remove_completion_param install_dlna
  162. firewall_remove 1900 udp
  163. firewall_remove 8200 tcp
  164. rm /etc/avahi/services/dlna.service
  165. systemctl restart avahi-daemon
  166. }
  167. function install_dlna_main {
  168. if [[ $(app_is_installed dlna_main) == "1" ]]; then
  169. return
  170. fi
  171. apt-get -yq install minidlna
  172. if [ ! -f /etc/minidlna.conf ]; then
  173. echo $"ERROR: minidlna does not appear to have installed. $CHECK_MESSAGE"
  174. exit 55
  175. fi
  176. if [ ! "$USB_MOUNT_DLNA" ]; then
  177. USB_MOUNT_DLNA=/mnt/dlna
  178. fi
  179. if [ ${#USB_MOUNT_DLNA} -eq 0 ]; then
  180. USB_MOUNT_DLNA=/mnt/dlna
  181. fi
  182. sed -i "s|media_dir=/var/lib/minidlna|media_dir=A,/home/$MY_USERNAME/Music|g" /etc/minidlna.conf
  183. if ! grep -q "/home/$MY_USERNAME/Pictures" /etc/minidlna.conf; then
  184. echo "media_dir=P,/home/$MY_USERNAME/Pictures" >> /etc/minidlna.conf
  185. fi
  186. if ! grep -q "/home/$MY_USERNAME/Videos" /etc/minidlna.conf; then
  187. echo "media_dir=V,/home/$MY_USERNAME/Videos" >> /etc/minidlna.conf
  188. fi
  189. if ! grep -q "$USB_MOUNT_DLNA/Music" /etc/minidlna.conf; then
  190. echo "media_dir=A,$USB_MOUNT_DLNA/Music" >> /etc/minidlna.conf
  191. fi
  192. if ! grep -q "$USB_MOUNT_DLNA/Pictures" /etc/minidlna.conf; then
  193. echo "media_dir=P,$USB_MOUNT_DLNA/Pictures" >> /etc/minidlna.conf
  194. fi
  195. if ! grep -q "$USB_MOUNT_DLNA/Videos" /etc/minidlna.conf; then
  196. echo "media_dir=V,$USB_MOUNT_DLNA/Videos" >> /etc/minidlna.conf
  197. fi
  198. sed -i 's/#root_container=./root_container=B/g' /etc/minidlna.conf
  199. if [[ $SYSTEM_TYPE != "mesh"* ]]; then
  200. if [[ $(config_param_exists WIFI_INTERFACE) == "0" ]]; then
  201. sed -i 's/#network_interface=/network_interface=eth0/g' /etc/minidlna.conf
  202. else
  203. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  204. fi
  205. else
  206. sed -i "s/#network_interface=/network_interface=$WIFI_INTERFACE/g" /etc/minidlna.conf
  207. fi
  208. sed -i "s/#friendly_name=/friendly_name=\"${PROJECT_NAME} Media\"/g" /etc/minidlna.conf
  209. sed -i 's|#db_dir=/var/cache/minidlna|db_dir=/var/cache/minidlna|g' /etc/minidlna.conf
  210. sed -i 's/#inotify=yes/inotify=yes/g' /etc/minidlna.conf
  211. sed -i 's/#notify_interval=895/notify_interval=300/g' /etc/minidlna.conf
  212. sed -i "s|#presentation_url=/|presentation_url=http://localhost:8200|g" /etc/minidlna.conf
  213. systemctl reload minidlna
  214. sed -i 's/fs.inotify.max_user_watches*/fs.inotify.max_user_watches=65536/g' /etc/sysctl.conf
  215. if ! grep -q "max_user_watches" "$COMPLETION_FILE"; then
  216. echo 'fs.inotify.max_user_watches=65536' >> /etc/sysctl.conf
  217. fi
  218. /sbin/sysctl -p -q
  219. function_check configure_firewall_for_dlna
  220. configure_firewall_for_dlna
  221. install_completed dlna_main
  222. }
  223. function script_for_attaching_usb_drive {
  224. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  225. return
  226. fi
  227. { echo '#!/bin/bash';
  228. echo "source /usr/local/bin/${PROJECT_NAME}-vars";
  229. echo "UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*";
  230. echo '';
  231. echo "for f in \$UTILS_FILES";
  232. echo 'do';
  233. echo " source \$f";
  234. echo 'done';
  235. echo '';
  236. echo 'USB_DRIVE=/dev/sda1';
  237. echo 'detect_usb_drive';
  238. echo '';
  239. echo 'remove-music';
  240. echo "if [ ! -d $USB_MOUNT_DLNA ]; then";
  241. echo " mkdir $USB_MOUNT_DLNA";
  242. echo 'fi';
  243. echo -n "mount \$USB_DRIVE ";
  244. echo "$USB_MOUNT_DLNA";
  245. echo "chown root:root $USB_MOUNT_DLNA";
  246. echo "chown -R minidlna:minidlna $USB_MOUNT_DLNA/*";
  247. echo 'systemctl restart minidlna';
  248. echo 'minidlnad -R';
  249. echo 'exit 0'; } > /usr/bin/attach-music
  250. chmod +x /usr/bin/attach-music
  251. { echo '#!/bin/bash';
  252. echo "if [ -d $USB_MOUNT_DLNA ]; then";
  253. echo " umount $USB_MOUNT_DLNA";
  254. echo " rm -rf $USB_MOUNT_DLNA";
  255. echo 'fi';
  256. echo 'exit 0'; } > /usr/bin/remove-music
  257. chmod +x /usr/bin/remove-music
  258. mark_completed "${FUNCNAME[0]}"
  259. }
  260. function install_dlna {
  261. install_dlna_main
  262. script_for_attaching_usb_drive
  263. APP_INSTALLED=1
  264. }
  265. # NOTE: deliberately no exit 0