widget/reply: load file upload progress indicator
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 9 Jan 2020 10:04:07 +0000 (11:04 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 1 Jan 2021 17:30:40 +0000 (18:30 +0100)
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
progress.js
upload.inc.php
widget/reply.php

index a9669c4fc85d60152e4768fd107aac403b0f1c8d..4dbdbe45604dcb7ff43556f86dc07b9c43e2c657 100644 (file)
@@ -11,6 +11,7 @@ function abort($body, string $status = NULL)
                        exit;
                }
        }
+       header('Content-Type: text/plain; charset=utf-8');
        print "$body\n";
        exit;
 }
index b93d61fe13236f9e59ca224bfd28998b2bc073ac..e59422de841e879c2c176475e2896796c6d3c7c8 100644 (file)
@@ -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;
index 6175fe20a685b1b1df707ccba8e74f4bad2564f1..f79600f892de3ee018377e1ab9adf460b3ce3646 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+$Page->api = $_SERVER['HTTP_ACCEPT'] == 'text/plain';
+
 function userupload($input, $target = NULL, $filename = NULL)
 {
        switch ($input['error']) {
index d305fc7c946e4abac1a8fcc761a0cb185bea6931..1f6d0e2ff0352f709ff0223fdd455c1d3962eded 100644 (file)
@@ -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 "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
        }
 }
@@ -139,7 +146,9 @@ if ($User->login) {
                ''
        );
        print '<input type="submit" value="Plaatsen" />'."\n";
-       print "</form></li>\n";
+       print "</form>";
+       print '<script src="/upload/progress.js"></script>';
+       print "</li>\n";
 }
 
 print "</ul>\n\n";