From cd376c5a18460ffc0819a1732a65713ad8c7e5f6 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Thu, 1 Mar 2018 21:21:49 -0800 Subject: [PATCH] start adding media uploads --- routes/_actions/media.js | 23 +++++ routes/_api/media.js | 9 ++ routes/_components/compose/ComposeBox.html | 16 ++-- routes/_components/compose/ComposeMedia.html | 94 +++++++++++++++++++ .../_components/compose/ComposeToolbar.html | 2 + routes/_utils/ajax.js | 7 +- scss/themes/_base.scss | 1 + templates/2xx.html | 4 +- 8 files changed, 146 insertions(+), 10 deletions(-) create mode 100644 routes/_actions/media.js create mode 100644 routes/_api/media.js create mode 100644 routes/_components/compose/ComposeMedia.html diff --git a/routes/_actions/media.js b/routes/_actions/media.js new file mode 100644 index 0000000..35e50cb --- /dev/null +++ b/routes/_actions/media.js @@ -0,0 +1,23 @@ +import { store } from '../_store/store' +import { uploadMedia } from '../_api/media' +import { toast } from '../_utils/toast' + +export async function doMediaUpload(file) { + let instanceName = store.get('currentInstance') + let accessToken = store.get('accessToken') + store.set({uploadingMedia: true}) + try { + let response = await uploadMedia(instanceName, accessToken, file) + let mediaToUpload = store.get('mediaToUpload') || [] + mediaToUpload.push({ + data: response, + file: file + }) + store.set({ mediaToUpload }) + } catch (e) { + console.error(e) + toast.say('Failed to upload media: ' + (e.message || '')) + } finally { + store.set({uploadingMedia: false}) + } +} \ No newline at end of file diff --git a/routes/_api/media.js b/routes/_api/media.js new file mode 100644 index 0000000..5fe7658 --- /dev/null +++ b/routes/_api/media.js @@ -0,0 +1,9 @@ +import { auth, basename } from './utils' +import { postWithTimeout } from '../_utils/ajax' + +export async function uploadMedia (instanceName, accessToken, file) { + let formData = new FormData() + formData.append('file', file) + let url = `${basename(instanceName)}/api/v1/media` + return postWithTimeout(url, formData, auth(accessToken)) +} \ No newline at end of file diff --git a/routes/_components/compose/ComposeBox.html b/routes/_components/compose/ComposeBox.html index cd931a8..938d76a 100644 --- a/routes/_components/compose/ComposeBox.html +++ b/routes/_components/compose/ComposeBox.html @@ -4,6 +4,7 @@ + + \ No newline at end of file diff --git a/routes/_components/compose/ComposeToolbar.html b/routes/_components/compose/ComposeToolbar.html index bcd7c71..88d56a2 100644 --- a/routes/_components/compose/ComposeToolbar.html +++ b/routes/_components/compose/ComposeToolbar.html @@ -29,6 +29,7 @@ import { store } from '../../_store/store' import { updateCustomEmojiForInstance } from '../../_actions/emoji' import { importDialogs } from '../../_utils/asyncModules' + import { doMediaUpload } from '../../_actions/media' export default { components: { @@ -46,6 +47,7 @@ }, onFileChange(e) { let file = e.target.files[0] + doMediaUpload(file) } } diff --git a/routes/_utils/ajax.js b/routes/_utils/ajax.js index 533430f..14bc6d2 100644 --- a/routes/_utils/ajax.js +++ b/routes/_utils/ajax.js @@ -24,11 +24,14 @@ async function _post (url, body, headers, timeout) { method: 'POST' } if (body) { - opts.headers = Object.assign(headers, { + let isFormData = body instanceof FormData + opts.headers = Object.assign(headers, isFormData ? { + 'Accept': 'application/json' + } : { 'Accept': 'application/json', 'Content-Type': 'application/json' }) - opts.body = JSON.stringify(body) + opts.body = isFormData ? body : JSON.stringify(body) } else { opts.headers = Object.assign(headers, { 'Accept': 'application/json' diff --git a/scss/themes/_base.scss b/scss/themes/_base.scss index 6c3a089..9c1edd0 100644 --- a/scss/themes/_base.scss +++ b/scss/themes/_base.scss @@ -69,4 +69,5 @@ --status-direct-background: darken($body-bg-color, 5%); --main-theme-color: $main-theme-color; --warning-color: #e01f19; + --alt-input-bg: rgba($main-bg-color, 0.7); } diff --git a/templates/2xx.html b/templates/2xx.html index 8954f53..f288e27 100644 --- a/templates/2xx.html +++ b/templates/2xx.html @@ -10,9 +10,9 @@