freedombone-app-dlna 9.3KB

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