]> cat aescling's git repositories - mastodon.git/commitdiff
[Glitch] Change local media attachments to perform heavy processing asynchronously
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 8 Mar 2020 22:56:18 +0000 (23:56 +0100)
committerThibaut Girka <thib@sitedethib.com>
Sun, 22 Mar 2020 15:26:26 +0000 (16:26 +0100)
Port front-end part of 9660aa4543deff41c60d131e081137f84e771499 to glitch-soc

[API] This makes use of a new media posting API (/api/v2/media), supporting
background processing of uploaded files. For Pleroma's purposes, this could
be handled the same as /api/v1/media since afaik Pleroma doesn't do any
transcoding.

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
app/javascript/flavours/glitch/actions/compose.js

index 0be74604895b46909b4866c0a86dc31bce8b5152..966d605d122a1d5ff4b1aa31ec84332d25a451db 100644 (file)
@@ -259,12 +259,31 @@ export function uploadCompose(files) {
         // Account for disparity in size of original image and resized data
         total += file.size - f.size;
 
-        return api(getState).post('/api/v1/media', data, {
+        return api(getState).post('/api/v2/media', data, {
           onUploadProgress: function({ loaded }){
             progress[i] = loaded;
             dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
           },
-        }).then(({ data }) => dispatch(uploadComposeSuccess(data, f)));
+        }).then(({ status, data }) => {
+          // If server-side processing of the media attachment has not completed yet,
+          // poll the server until it is, before showing the media attachment as uploaded
+
+          if (status === 200) {
+            dispatch(uploadComposeSuccess(data, f));
+          } else if (status === 202) {
+            const poll = () => {
+              api(getState).get(`/api/v1/media/${data.id}`).then(response => {
+                if (response.status === 200) {
+                  dispatch(uploadComposeSuccess(data, f));
+                } else if (response.status === 206) {
+                  setTimeout(() => poll(), 1000);
+                }
+              }).catch(error => dispatch(uploadComposeFail(error)));
+            };
+
+            poll();
+          }
+        });
       }).catch(error => dispatch(uploadComposeFail(error)));
     };
   };