From 38fb45bb2f42db6ecc3f97ebf33252a53858c6d5 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Thu, 29 Mar 2018 13:05:19 +0900 Subject: [PATCH] refs #118 Upload images when user select an image --- .../TimelineSpace/Modals/NewToot.vue | 31 +++++++++++++---- .../store/TimelineSpace/Modals/NewToot.js | 33 ++++++++++++++++++- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/renderer/components/TimelineSpace/Modals/NewToot.vue b/src/renderer/components/TimelineSpace/Modals/NewToot.vue index 7e41f7a8..88cf8206 100644 --- a/src/renderer/components/TimelineSpace/Modals/NewToot.vue +++ b/src/renderer/components/TimelineSpace/Modals/NewToot.vue @@ -12,11 +12,11 @@ @@ -28,8 +28,7 @@ export default { name: 'new-toot', data () { return { - ctrlPressed: false, - attachedFiles: [] + ctrlPressed: false } }, computed: { @@ -40,7 +39,9 @@ export default { } else { return null } - } + }, + attachedMedias: state => state.TimelineSpace.Modals.NewToot.attachedMedias, + blockSubmit: state => state.TimelineSpace.Modals.NewToot.blockSubmit }), newTootModal: { get () { @@ -118,8 +119,24 @@ export default { selectImage () { this.$refs.image.click() }, - uploadImage (e) { - console.log(e.target.files) + updateImage (e) { + if (e.target.files.item(0) === null || e.target.files.item(0) === undefined) { + return + } + if (!e.target.files.item(0).type.includes('image')) { + this.$message({ + message: 'You can only attach images', + type: 'error' + }) + return + } + this.$store.dispatch('TimelineSpace/Modals/NewToot/uploadImage', e.target.files.item(0)) + .catch(() => { + this.$message({ + message: 'Could not attach the file', + type: 'error' + }) + }) } } } diff --git a/src/renderer/store/TimelineSpace/Modals/NewToot.js b/src/renderer/store/TimelineSpace/Modals/NewToot.js index ab16a442..bf3db4a8 100644 --- a/src/renderer/store/TimelineSpace/Modals/NewToot.js +++ b/src/renderer/store/TimelineSpace/Modals/NewToot.js @@ -1,11 +1,14 @@ import Mastodon from 'mastodon-api' +import fs from 'fs' const NewToot = { namespaced: true, state: { modalOpen: false, status: '', - replyToMessage: null + replyToMessage: null, + blockSubmit: false, + attachedMedias: [] }, mutations: { changeModal (state, value) { @@ -16,6 +19,12 @@ const NewToot = { }, updateStatus (state, status) { state.status = status + }, + changeBlockSubmit (state, value) { + state.blockSubmit = value + }, + appendAttachedMedias (state, media) { + state.attachedMedias = state.attachedMedias.concat([media]) } }, actions: { @@ -47,6 +56,27 @@ const NewToot = { commit('setReplyTo', null) } commit('changeModal', value) + }, + uploadImage ({ state, commit, rootState }, image) { + return new Promise((resolve, reject) => { + commit('changeBlockSubmit', true) + if (rootState.TimelineSpace.account.accessToken === undefined || rootState.TimelineSpace.account.accessToken === null) { + return reject(new AuthenticationError()) + } + const client = new Mastodon( + { + access_token: rootState.TimelineSpace.account.accessToken, + api_url: rootState.TimelineSpace.account.baseURL + '/api/v1' + } + ) + client.post('/media', { file: fs.createReadStream(image.path) }, (err, data, res) => { + commit('changeBlockSubmit', false) + if (err) return reject(err) + if (data.type !== 'image') reject(new UnknownTypeError()) + commit('appendAttachedMedias', data) + resolve(res) + }) + }) } } } @@ -54,3 +84,4 @@ const NewToot = { export default NewToot class AuthenticationError {} +class UnknownTypeError {}