Add a socket service that downloads images and puts them in NextCloud
authorJacob Casper <dev@jacobcasper.com>
Sun, 5 Apr 2020 06:28:01 +0000 (01:28 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sun, 5 Apr 2020 09:06:19 +0000 (04:06 -0500)
listener.sh [new file with mode: 0755]
mercuryms-listen.socket [new file with mode: 0644]
mercuryms-listen@.service [new file with mode: 0644]
setup.sh

diff --git a/listener.sh b/listener.sh
new file mode 100755 (executable)
index 0000000..182842e
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/bash
+# DATA indices are defined by the mercuryms-send program, and are from mercuryms-sqlite
+# 0: MEDIA.ID
+# 1: MEDIA.PHONE_NUMBER
+# 2: MEDIA.URI
+while IFS='|' read -a DATA DATA_STR; do
+    # Check if the phone number has an existing folder.
+    # WebDAV responds to a PROPFIND with a 207 if a resource exists.
+    # It responds 404 otherwise, but we will attempt to create on any other code.
+    # AWK exits with inverted exit codes to use the && bash short circuit.
+    curl --silent --include --user $USER:$($PW_COMMAND) "$HOST/remote.php/dav/files/$USER/${DATA[1]}" -X PROPFIND --data '<?xml version="1.0" encoding="UTF-8"?>
+ <d:propfind xmlns:d="DAV:">
+   <d:prop xmlns:oc="http://owncloud.org/ns">
+     <d:resourcetype/>
+   </d:prop>
+ </d:propfind>' | awk '/HTTP\// {if ($2 == "207") { exit 1; } else { exit 0; } }' && curl --silent \
+            --user $USER:$($PW_COMMAND) \
+            -X MKCOL "$HOST/remote.php/dav/files/$USER/${DATA[1]}"
+    # Download the media we were sent from the URI.
+    # Upload it to Nextcloud and respond with the ID in the database
+    # so that we can record success.
+    IDENTIFIER=$(echo ${DATA[2]} | awk -F/ '{print $NF}')
+    curl --silent \
+         --location ${DATA[2]} \
+    | curl --silent \
+           --user $USER:$($PW_COMMAND) \
+           --upload-file - \
+           "$HOST/remote.php/dav/files/$USER/${DATA[1]}/$IDENTIFIER.jpg" && echo ${DATA[0]} received.
+done
+
+exit 0
diff --git a/mercuryms-listen.socket b/mercuryms-listen.socket
new file mode 100644 (file)
index 0000000..21aa912
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=MercuryMS Listen Socket
+PartOf=mercuryms-listen@.service
+
+[Socket]
+ListenStream=9099
+Accept=yes
+
+[Install]
+WantedBy=sockets.target
diff --git a/mercuryms-listen@.service b/mercuryms-listen@.service
new file mode 100644 (file)
index 0000000..75a959c
--- /dev/null
@@ -0,0 +1,21 @@
+[Unit]
+Description=MercuryMS Listen Service
+After=network.target mercuryms-listen.socket
+Requires=mercuryms-listen.socket
+
+[Service]
+Type=oneshot
+User=mercuryms
+Group=mercuryms
+WorkingDirectory=/usr/share/mercuryms
+ExecStart=/opt/mercuryms/listener.sh
+StandardInput=socket
+StandardOutput=socket
+Environment="USER=admin"
+Environment="HOST=localhost:9092"
+# If overriding, should output to stdout similarly to printf
+Environment="PW_COMMAND=printf admin"
+
+
+[Install]
+WantedBy=multi-user.target
index f1a3474..999d449 100755 (executable)
--- a/setup.sh
+++ b/setup.sh
@@ -7,3 +7,5 @@ chown mercuryms:mercuryms /usr/share/mercuryms
 chmod 700 /usr/share/mercuryms
 sqlite3 /usr/share/mercuryms/mercuryms.sqlite "$(cat migrations/*)"
 chown mercuryms:mercuryms /usr/share/mercuryms/mercuryms.sqlite
+cp mercuryms.service /etc/systemd/system/
+cp mercuryms-listen* /etc/systemd/system/