From e64861a7760cfe34596e193a6a3ed2ef39b25892 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 9 Jan 2020 11:04:07 +0100 Subject: [PATCH] widget/reply: load file upload progress indicator Extend image upload script for any user form, with global support for text/plain post interface (copied from Lijtweg upload controller designed in v2.6-167-gd6338eb73e (2018-11-04) [hijack file input to show progress]). --- error.inc.php | 1 + progress.js | 9 ++------- upload.inc.php | 2 ++ widget/reply.php | 11 ++++++++++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/error.inc.php b/error.inc.php index a9669c4..4dbdbe4 100644 --- a/error.inc.php +++ b/error.inc.php @@ -11,6 +11,7 @@ function abort($body, string $status = NULL) exit; } } + header('Content-Type: text/plain; charset=utf-8'); print "$body\n"; exit; } diff --git a/progress.js b/progress.js index b93d61f..e59422d 100644 --- a/progress.js +++ b/progress.js @@ -46,12 +46,7 @@ function trackupload(input) { ajax.open('POST', input.form.action); ajax.setRequestHeader('Accept', 'text/plain') - - var form = new FormData(); - for (let file of input.files) { - form.append('image[]', file); - } - ajax.send(form); + ajax.send(new FormData(input.form)); var cancel = document.createElement('BUTTON'); cancel.textContent = 'Afbreken'; @@ -62,7 +57,7 @@ function trackupload(input) { document.addEventListener('DOMContentLoaded', e => { for (let row of document.forms[0].elements) { - if (row.name == 'image[]') { + if (row.type == 'file') { row.form.onsubmit = () => { trackupload(row); return false; diff --git a/upload.inc.php b/upload.inc.php index 6175fe2..f79600f 100644 --- a/upload.inc.php +++ b/upload.inc.php @@ -1,4 +1,6 @@ api = $_SERVER['HTTP_ACCEPT'] == 'text/plain'; + function userupload($input, $target = NULL, $filename = NULL) { switch ($input['error']) { diff --git a/widget/reply.php b/widget/reply.php index d305fc7..1f6d0e2 100644 --- a/widget/reply.php +++ b/widget/reply.php @@ -60,9 +60,16 @@ if ($_POST) { $Issue = $updated; } } + + if ($Page->api) { + abort("/{$Page->link}", '200 reply success'); + } $_POST['reply'] = NULL; } catch (Exception $e) { + if ($Page->api) { + abort(ucfirst($e->getMessage()), '500 reply error'); + } print "

Antwoord niet opgeslagen: {$e->getMessage()}.

\n\n"; } } @@ -139,7 +146,9 @@ if ($User->login) { '' ); print ''."\n"; - print "\n"; + print ""; + print ''; + print "\n"; } print "\n\n"; -- 2.30.0