| 1 | ; Image upload for HexDroid |
1 | ; Image upload for HexDroid |
| 2 | ; For websites that use https://phpaste.sourceforge.io |
2 | ; For websites that use https://phpaste.sourceforge.io |
| 3 | ; |
3 | ; |
| 4 | ; /img picks an image and uploads it, then sends the link to the channel. |
4 | ; /img picks an image and uploads it, then sends the link to the channel. |
| 5 | ; /imgme does the same but shows the link only to you. |
5 | ; /imgme does the same but shows the link only to you. |
| 6 | ; |
6 | ; |
| 7 | ; Uploads through the Paste API (api.php?action=upload), which: |
7 | ; Uploads through the Paste API (api.php?action=upload), which: |
| 8 | ; - takes your API key, from Profile > API Keys on the site; sites that allow |
8 | ; - takes your API key, from Profile > API Keys on the site; sites that allow |
| 9 | ; guest uploads also take uploads without one |
9 | ; guest uploads also take uploads without one |
| 10 | ; - reads the file from the form field "file" |
10 | ; - reads the file from the form field "file" |
| 11 | ; - removes EXIF and GPS tags unless strip_exif=0 is sent |
11 | ; - removes EXIF and GPS tags unless strip_exif=0 is sent |
| 12 | ; - answers 201 with {"success":true,"upload":{"file":"https://.../img/<key>.png",...}} |
12 | ; - answers 201 with {"success":true,"upload":{"file":"https://.../img/<key>.png",...}} |
| 13 | ; or an error status with {"success":false,"error":"..."} |
13 | ; or an error status with {"success":false,"error":"..."} |
| 14 | ; |
14 | ; |
| 15 | ; SETTINGS |
15 | ; SETTINGS |
| 16 | ; %img_ep exact upload URL. Redirects are not followed, so use the final URL. |
16 | ; %img_ep exact upload URL. Redirects are not followed, so use the final URL. |
| 17 | ; %img_field form field the server reads the file from. |
17 | ; %img_field form field the server reads the file from. |
| 18 | ; %img_auth optional, your API key as a whole header line: Authorization: Bearer <key> |
18 | ; %img_auth optional, your API key as a whole header line: Authorization: Bearer <key> |
| 19 | ; (X-API-Key: <key> also works). Leave empty to upload as a guest. |
19 | ; (X-API-Key: <key> also works). Leave empty to upload as a guest. |
| 20 | ; |
20 | ; |
| 21 | ; Globals are shared by every script, so the settings carry an img_ prefix. |
21 | ; Globals are shared by every script, so the settings carry an img_ prefix. |
| 22 | ; |
22 | ; |
| 23 | ; The picker never opens on its own: HexDroid asks first, and you choose the file. The |
23 | ; The picker never opens on its own: HexDroid asks first, and you choose the file. The |
| 24 | ; script is handed a token for that one file, not a path, and can read nothing else. |
24 | ; script is handed a token for that one file, not a path, and can read nothing else. |
| 25 | 25 | ||
| 26 | on LOAD { |
26 | on LOAD { |
| 27 | set %img_ep https://paste.boxlabs.uk/api.php?action=upload |
27 | set %img_ep https://paste.boxlabs.uk/api.php?action=upload |
| 28 | set %img_field file |
28 | set %img_field file |
| 29 | set %img_auth |
29 | set %img_auth |
| 30 | } |
30 | } |
| 31 | 31 | ||
| 32 | alias img { |
32 | alias img { |
| 33 | if ($buffer == *server*) { |
33 | if ($buffer == *server*) { |
| 34 | echo $buffer *** /img needs a channel or query. Use /imgme here. |
34 | echo $buffer *** /img needs a channel or query. Use /imgme here. |
| 35 | return |
35 | return |
| 36 | } |
36 | } |
| 37 | media.pick -m image/* img_picked $buffer public |
37 | media.pick -m image/* img_picked $buffer public |
| 38 | } |
38 | } |
| 39 | 39 | ||
| 40 | alias imgme { |
40 | alias imgme { |
| 41 | media.pick -m image/* img_picked $buffer private |
41 | media.pick -m image/* img_picked $buffer private |
| 42 | } |
42 | } |
| 43 | 43 | ||
| 44 | on SIGNAL:img_picked { |
44 | on SIGNAL:img_picked { |
| 45 | ; $1 buffer, $2 public|private |
45 | ; $1 buffer, $2 public|private |
| 46 | if ($mediaok != true) { return } |
46 | if ($mediaok != true) { return } |
| 47 | toast Uploading $medianame |
47 | toast Uploading $medianame |
| 48 | if ($len(%img_auth) == 0) { |
48 | if ($len(%img_auth) == 0) { |
| 49 | media.upload -f %img_field -p strip_exif=1 %img_ep $mediatoken img_done $1 $2 |
49 | media.upload -f %img_field -p strip_exif=1 %img_ep $mediatoken img_done $1 $2 |
| 50 | } |
50 | } |
| 51 | else { |
51 | else { |
| 52 | media.upload -f %img_field -p strip_exif=1 -h %img_auth %img_ep $mediatoken img_done $1 $2 |
52 | media.upload -f %img_field -p strip_exif=1 -h %img_auth %img_ep $mediatoken img_done $1 $2 |
| 53 | } |
53 | } |
| 54 | } |
54 | } |
| 55 | 55 | ||
| 56 | on SIGNAL:img_done { |
56 | on SIGNAL:img_done { |
| 57 | ; $1 buffer, $2 public|private |
57 | ; $1 buffer, $2 public|private |
| 58 | if ($httpok != true) { |
58 | if ($httpok != true) { |
| 59 | if ($httpstatus == 0) { |
59 | if ($httpstatus == 0) { |
| 60 | echo $1 *** upload failed: $httperror |
60 | echo $1 *** upload failed: $httperror |
| 61 | return |
61 | return |
| 62 | } |
62 | } |
| 63 | if ($httpstatus == 401) { |
63 | if ($httpstatus == 401) { |
| 64 | echo $1 *** this site needs an API key: add it to img_auth in the LOAD block |
64 | echo $1 *** this site needs an API key: add it to img_auth in the LOAD block |
| 65 | return |
65 | return |
| 66 | } |
66 | } |
| 67 | set -l %err $json($httpbody, error) |
67 | set -l %err $json($httpbody, error) |
| 68 | if ($len(%err) > 0) { |
68 | if ($len(%err) > 0) { |
| 69 | echo $1 *** upload failed ($httpstatus): %err |
69 | echo $1 *** upload failed ($httpstatus): %err |
| 70 | return |
70 | return |
| 71 | } |
71 | } |
| 72 | echo $1 *** upload failed ($httpstatus) $left($trim($re_replace($httpbody, \s+, " ")), 200) |
72 | echo $1 *** upload failed ($httpstatus) $left($trim($re_replace($httpbody, \s+, " ")), 200) |
| 73 | return |
73 | return |
| 74 | } |
74 | } |
| 75 | 75 | ||
| 76 | set -l %url $trim($json($httpbody, upload.file)) |
76 | set -l %url $trim($json($httpbody, upload.file)) |
| 77 | if ($len(%url) == 0) { |
77 | if ($len(%url) == 0) { |
| 78 | echo $1 *** uploaded, but no link in the reply: $left($trim($re_replace($httpbody, \s+, " ")), 200) |
78 | echo $1 *** uploaded, but no link in the reply: $left($trim($re_replace($httpbody, \s+, " ")), 200) |
| 79 | return |
79 | return |
| 80 | } |
80 | } |
| 81 | 81 | ||
| 82 | if ($2 == public) { msg $1 %url } |
82 | if ($2 == public) { msg $1 %url } |
| 83 | else { echo $1 %url } |
83 | else { echo $1 %url } |
| 84 | } |
84 | } |
| 85 | 85 |