Commit | Line | Data |
---|---|---|
8ef3a2be JC |
1 | #!/bin/bash |
2 | # DATA indices are defined by the mercuryms-send program, and are from mercuryms-sqlite | |
3 | # 0: MEDIA.ID | |
4 | # 1: MEDIA.PHONE_NUMBER | |
5 | # 2: MEDIA.URI | |
6 | while IFS='|' read -a DATA DATA_STR; do | |
7 | # Check if the phone number has an existing folder. | |
8 | # WebDAV responds to a PROPFIND with a 207 if a resource exists. | |
9 | # It responds 404 otherwise, but we will attempt to create on any other code. | |
10 | # AWK exits with inverted exit codes to use the && bash short circuit. | |
11 | 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"?> | |
12 | <d:propfind xmlns:d="DAV:"> | |
13 | <d:prop xmlns:oc="http://owncloud.org/ns"> | |
14 | <d:resourcetype/> | |
15 | </d:prop> | |
16 | </d:propfind>' | awk '/HTTP\// {if ($2 == "207") { exit 1; } else { exit 0; } }' && curl --silent \ | |
17 | --user $USER:$($PW_COMMAND) \ | |
18 | -X MKCOL "$HOST/remote.php/dav/files/$USER/${DATA[1]}" | |
19 | # Download the media we were sent from the URI. | |
20 | # Upload it to Nextcloud and respond with the ID in the database | |
21 | # so that we can record success. | |
22 | IDENTIFIER=$(echo ${DATA[2]} | awk -F/ '{print $NF}') | |
23 | curl --silent \ | |
24 | --location ${DATA[2]} \ | |
25 | | curl --silent \ | |
26 | --user $USER:$($PW_COMMAND) \ | |
27 | --upload-file - \ | |
28 | "$HOST/remote.php/dav/files/$USER/${DATA[1]}/$IDENTIFIER.jpg" && echo ${DATA[0]} received. | |
29 | done | |
30 | ||
31 | exit 0 |