2018-03-02 05:21:49 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { uploadMedia } from '../_api/media'
|
|
|
|
import { toast } from '../_utils/toast'
|
2018-03-03 19:26:10 +00:00
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2018-03-02 05:21:49 +00:00
|
|
|
|
2018-03-03 18:11:32 +00:00
|
|
|
export async function doMediaUpload (realm, file) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentInstance, accessToken } = store.get()
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ uploadingMedia: true })
|
2018-03-02 05:21:49 +00:00
|
|
|
try {
|
2018-04-19 16:37:05 +00:00
|
|
|
let response = await uploadMedia(currentInstance, accessToken, file)
|
2018-03-03 22:15:50 +00:00
|
|
|
let composeMedia = store.getComposeData(realm, 'media') || []
|
|
|
|
composeMedia.push({
|
2018-03-02 05:21:49 +00:00
|
|
|
data: response,
|
2018-08-27 01:54:59 +00:00
|
|
|
file: { name: file.name },
|
|
|
|
description: ''
|
2018-03-02 05:21:49 +00:00
|
|
|
})
|
2018-03-03 22:51:48 +00:00
|
|
|
store.setComposeData(realm, {
|
2018-08-27 01:54:59 +00:00
|
|
|
media: composeMedia
|
2018-03-03 22:51:48 +00:00
|
|
|
})
|
2018-03-03 19:26:10 +00:00
|
|
|
scheduleIdleTask(() => store.save())
|
2018-03-02 05:21:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Failed to upload media: ' + (e.message || ''))
|
|
|
|
} finally {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ uploadingMedia: false })
|
2018-03-02 05:21:49 +00:00
|
|
|
}
|
2018-03-03 01:54:38 +00:00
|
|
|
}
|
2018-03-03 05:55:04 +00:00
|
|
|
|
2018-03-03 18:11:32 +00:00
|
|
|
export function deleteMedia (realm, i) {
|
2018-03-03 22:15:50 +00:00
|
|
|
let composeMedia = store.getComposeData(realm, 'media')
|
2018-08-27 01:54:59 +00:00
|
|
|
composeMedia.splice(i, 1)
|
2018-04-10 01:30:15 +00:00
|
|
|
|
2018-03-03 22:51:48 +00:00
|
|
|
store.setComposeData(realm, {
|
2018-08-27 01:54:59 +00:00
|
|
|
media: composeMedia
|
2018-03-03 22:51:48 +00:00
|
|
|
})
|
2018-03-03 19:26:10 +00:00
|
|
|
scheduleIdleTask(() => store.save())
|
2018-03-03 05:55:04 +00:00
|
|
|
}
|