2018-03-01 21:21:49 -08:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { uploadMedia } from '../_api/media'
|
2018-12-22 15:37:51 -08:00
|
|
|
import { toast } from '../_components/toast/toast'
|
2018-03-03 11:26:10 -08:00
|
|
|
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
|
2019-08-25 21:48:59 -07:00
|
|
|
import { mediaUploadFileCache } from '../_utils/mediaUploadFileCache'
|
2018-03-01 21:21:49 -08:00
|
|
|
|
2018-03-03 10:11:32 -08:00
|
|
|
export async function doMediaUpload (realm, file) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const { currentInstance, accessToken } = store.get()
|
2018-08-29 21:42:57 -07:00
|
|
|
store.set({ uploadingMedia: true })
|
2018-03-01 21:21:49 -08:00
|
|
|
try {
|
2019-08-03 13:49:37 -07:00
|
|
|
const response = await uploadMedia(currentInstance, accessToken, file)
|
|
|
|
const composeMedia = store.getComposeData(realm, 'media') || []
|
2018-12-15 02:06:12 -08:00
|
|
|
if (composeMedia.length === 4) {
|
|
|
|
throw new Error('Only 4 media max are allowed')
|
|
|
|
}
|
2019-08-25 22:24:37 -07:00
|
|
|
mediaUploadFileCache.set(response.url, file)
|
2018-03-03 14:15:50 -08:00
|
|
|
composeMedia.push({
|
2018-03-01 21:21:49 -08:00
|
|
|
data: response,
|
2018-08-26 18:54:59 -07:00
|
|
|
file: { name: file.name },
|
|
|
|
description: ''
|
2018-03-01 21:21:49 -08:00
|
|
|
})
|
2018-03-03 14:51:48 -08:00
|
|
|
store.setComposeData(realm, {
|
2018-08-26 18:54:59 -07:00
|
|
|
media: composeMedia
|
2018-03-03 14:51:48 -08:00
|
|
|
})
|
2018-03-03 11:26:10 -08:00
|
|
|
scheduleIdleTask(() => store.save())
|
2018-03-01 21:21:49 -08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say('Failed to upload media: ' + (e.message || ''))
|
|
|
|
} finally {
|
2018-08-29 21:42:57 -07:00
|
|
|
store.set({ uploadingMedia: false })
|
2018-03-01 21:21:49 -08:00
|
|
|
}
|
2018-03-02 17:54:38 -08:00
|
|
|
}
|
2018-03-02 21:55:04 -08:00
|
|
|
|
2018-03-03 10:11:32 -08:00
|
|
|
export function deleteMedia (realm, i) {
|
2019-08-03 13:49:37 -07:00
|
|
|
const composeMedia = store.getComposeData(realm, 'media')
|
2018-08-26 18:54:59 -07:00
|
|
|
composeMedia.splice(i, 1)
|
2018-04-09 18:30:15 -07:00
|
|
|
|
2018-03-03 14:51:48 -08:00
|
|
|
store.setComposeData(realm, {
|
2018-08-26 18:54:59 -07:00
|
|
|
media: composeMedia
|
2018-03-03 14:51:48 -08:00
|
|
|
})
|
2019-09-17 00:19:59 -07:00
|
|
|
if (!composeMedia.length) {
|
|
|
|
const contentWarningShown = store.getComposeData(realm, 'contentWarningShown')
|
|
|
|
const contentWarning = store.getComposeData(realm, 'contentWarning')
|
|
|
|
store.setComposeData(realm, {
|
|
|
|
sensitive: contentWarningShown && contentWarning // reset sensitive if the last media is deleted
|
|
|
|
})
|
|
|
|
}
|
2018-03-03 11:26:10 -08:00
|
|
|
scheduleIdleTask(() => store.save())
|
2018-03-02 21:55:04 -08:00
|
|
|
}
|