From b0af9722bbdb804b7d3704be4dcee33078bcc709 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Wed, 22 Aug 2018 19:45:32 +0100
Subject: [PATCH] Integrate koel with syncthing

---
 src/freedombone-app-koel       | 65 ++++++++++++++++++++++++++++++++++
 src/freedombone-utils-webadmin |  9 +++++
 webadmin/EN/settings_koel.html |  4 +--
 webadmin/settings_koel.php     |  5 +--
 4 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/src/freedombone-app-koel b/src/freedombone-app-koel
index 3a59f2e41..88a5f7e96 100755
--- a/src/freedombone-app-koel
+++ b/src/freedombone-app-koel
@@ -51,6 +51,59 @@ koel_variables=(ONION_ONLY
                 MY_EMAIL_ADDRESS
                 MY_USERNAME)
 
+function koel_upload_from_syncthing {
+    if [ ! -d /music ]; then
+        mkdir /music
+    fi
+
+    { echo '#!/bin/bash';
+      echo '';
+      echo "syncdir=/home/$MY_USERNAME/Sync/music_upload";
+      echo '';
+      echo 'while true';
+      echo 'do';
+      echo "    if [ -d \$syncdir ]; then";
+      echo "        cd /home/$MY_USERNAME/Sync";
+      echo "        if [ ! \"\$syncdir_base\" ]; then";
+      echo "            syncdir_base=\$(ls -ld music_upload/*)";
+      echo '        fi';
+      echo '';
+      echo '        while read -r line; do';
+      echo "            curr_dir=\$(echo \"\$line\" | awk -F '/' '{print \$2}')";
+      echo "            if [ -d \$syncdir/\$curr_dir ]; then";
+      echo "                if [ ! -d /music/\$curr_dir ]; then";
+      echo "                    mv \$syncdir/\$curr_dir /music";
+      echo '                fi';
+      echo '            fi';
+      echo "        done <<< \"\$syncdir_base\"";
+      echo '';
+      echo "        syncdir_base=\$(ls -ld music_upload/*)";
+      echo '    fi';
+      echo '    sleep 10';
+      echo 'done'; } > /usr/bin/koelsync
+    chmod +x /usr/bin/koelsync
+
+    if [ ! -f /etc/systemd/system/koel_syncthing.service ]; then
+        { echo '[Unit]';
+          echo 'Description=Koel integration with syncthing';
+          echo 'After=syslog.target';
+          echo 'After=network.target';
+          echo '';
+          echo '[Service]';
+          echo 'Type=simple';
+          echo 'User=root';
+          echo "WorkingDirectory=/music";
+          echo "ExecStart=/usr/bin/koelsync";
+          echo 'Restart=on-failure';
+          echo '';
+          echo '[Install]';
+          echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/koel_syncthing.service
+        systemctl enable koel_syncthing.service
+        systemctl daemon-reload
+    fi
+    systemctl restart koel_syncthing.service
+}
+
 function koel_setting_upload {
     music_file="$1"
 
@@ -333,6 +386,8 @@ function configure_interactive_koel {
 }
 
 function upgrade_koel {
+    koel_upload_from_syncthing
+
     if grep -q "koel domain" "$COMPLETION_FILE"; then
         KOEL_DOMAIN_NAME=$(get_completion_param "koel domain")
     fi
@@ -478,6 +533,11 @@ function remove_koel {
     if [ -f /etc/systemd/system/koel.service ]; then
         rm /etc/systemd/system/koel.service
     fi
+    if [ -f /etc/systemd/system/koel_syncthing.service ]; then
+        systemctl stop koel_syncthing
+        systemctl disable koel_syncthing
+        rm /etc/systemd/system/koel_syncthing.service
+    fi
     systemctl daemon-reload
 
     function_check remove_nodejs
@@ -504,6 +564,10 @@ function remove_koel {
     sed -i '/koel/d' "$COMPLETION_FILE"
     remove_backup_database_local koel
 
+    if [ -f /usr/bin/koelsync ]; then
+        rm /usr/bin/koelsync
+    fi
+
     function_check remove_ddns_domain
     remove_ddns_domain "$KOEL_DOMAIN_NAME"
 }
@@ -800,6 +864,7 @@ function install_koel {
 
     systemctl restart mariadb
     systemctl restart nginx
+    koel_upload_from_syncthing
 
     APP_INSTALLED=1
 }
diff --git a/src/freedombone-utils-webadmin b/src/freedombone-utils-webadmin
index afcee5f29..50d1366c8 100755
--- a/src/freedombone-utils-webadmin
+++ b/src/freedombone-utils-webadmin
@@ -923,6 +923,7 @@ function install_web_admin {
       echo '  error_log /dev/null;';
       echo '';
       echo '  location ^~ /admin {';
+      echo '    client_max_body_size 200m;';
       echo "    ${basic_auth_str} \"${WEBADMIN_LOGIN_TEXT}\";";
       echo "    ${basic_auth_str}_user_file /etc/nginx/.webadminpasswd;";
       echo "    root /var/www/${local_hostname}/htdocs;";
@@ -966,6 +967,7 @@ function install_web_admin {
     { echo '  add_header Strict-Transport-Security max-age=0;';
       echo '';
       echo '  location ^~ /admin {';
+      echo '    client_max_body_size 200m;';
       echo "    ${basic_auth_str} \"${WEBADMIN_LOGIN_TEXT}\";";
       echo "    ${basic_auth_str}_user_file /etc/nginx/.webadminpasswd;";
       echo "    root /var/www/${local_hostname}/htdocs;";
@@ -1008,6 +1010,13 @@ function install_web_admin {
         mkdir -p "/var/www/${local_hostname}/htdocs/plinth"
     fi
 
+    if ! grep -q 'post_max_size = 200M' /etc/php/7.0/fpm/php.ini; then
+        sed -i 's|post_max_size.*|post_max_size = 200M|g' /etc/php/7.0/fpm/php.ini
+    fi
+    if ! grep -q 'upload_max_filesize = 200M' /etc/php/7.0/fpm/php.ini; then
+        sed -i 's|upload_max_filesize.*|upload_max_filesize = 200M|g' /etc/php/7.0/fpm/php.ini
+    fi
+
     web_admin_create_users
 
     # make list of apps which can be added
diff --git a/webadmin/EN/settings_koel.html b/webadmin/EN/settings_koel.html
index af5ea4840..00fd5f483 100644
--- a/webadmin/EN/settings_koel.html
+++ b/webadmin/EN/settings_koel.html
@@ -93,8 +93,8 @@
 
         <p class="settingtext">Upload a <b>zip</b> file containing your music</p>
 
-        <form action="settings_koel.php" method="post">
-          <input type="file" name="musicfile" accept=".zip">
+        <form action="settings_koel.php" method="post" enctype="multipart/form-data">
+          <input type="file" name="musicfile" id="musicfile" accept=".zip">
           <br><br>
           <input type="submit" name="submitcancel" value="Cancel" />
           <input type="submit" name="submitmusic" value="Upload" />
diff --git a/webadmin/settings_koel.php b/webadmin/settings_koel.php
index 032584034..5d0ed2645 100755
--- a/webadmin/settings_koel.php
+++ b/webadmin/settings_koel.php
@@ -1,11 +1,12 @@
 <?php
 
 // Koel settings menu
-
 $output_filename = "app_koel.html";
 
 if (isset($_POST['submitmusic'])) {
-    $musicfile = htmlspecialchars($_POST['musicfile']);
+    $target_dir = "uploads/";
+    $musicfile = $target_dir . basename($_FILES["musicfile"]["name"]);
+
     if(file_exists($musicfile)) {
         $settings_file = fopen(".appsettings.txt", "w") or die("Unable to write to appsettings file");
         fwrite($settings_file, "koel,upload,".$musicfile);
-- 
GitLab