tooot/src/screens/Compose/Root/Footer/addAttachment.ts

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-01-04 14:55:34 +01:00
import * as Crypto from 'expo-crypto'
2020-12-30 00:56:25 +01:00
import { ImageInfo } from 'expo-image-picker/build/ImagePicker.types'
import * as VideoThumbnails from 'expo-video-thumbnails'
import { Dispatch } from 'react'
2021-05-09 21:59:03 +02:00
import { Alert } from 'react-native'
2021-01-19 01:13:45 +01:00
import { ComposeAction } from '../../utils/types'
2021-01-13 01:03:46 +01:00
import { ActionSheetOptions } from '@expo/react-native-action-sheet'
2021-01-19 01:13:45 +01:00
import i18next from 'i18next'
2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
2021-05-09 21:59:03 +02:00
import mediaSelector from '@components/mediaSelector'
2020-12-30 00:56:25 +01:00
export interface Props {
composeDispatch: Dispatch<ComposeAction>
2021-01-14 00:43:35 +01:00
showActionSheetWithOptions: (
options: ActionSheetOptions,
callback: (i: number) => void
) => void
2020-12-30 00:56:25 +01:00
}
2021-01-14 00:43:35 +01:00
const addAttachment = async ({
composeDispatch,
showActionSheetWithOptions
}: Props): Promise<any> => {
2021-05-09 21:59:03 +02:00
const uploader = async (imageInfo: ImageInfo) => {
2021-01-04 14:55:34 +01:00
const hash = await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
2021-05-09 21:59:03 +02:00
imageInfo.uri + Math.random()
2021-01-04 14:55:34 +01:00
)
2021-01-14 00:43:35 +01:00
let attachmentType: string
2021-05-09 21:59:03 +02:00
switch (imageInfo.type) {
2020-12-30 00:56:25 +01:00
case 'image':
2021-05-09 21:59:03 +02:00
attachmentType = `image/${imageInfo.uri.split('.')[1]}`
2020-12-30 00:56:25 +01:00
composeDispatch({
type: 'attachment/upload/start',
payload: {
2021-05-09 21:59:03 +02:00
local: { ...imageInfo, local_thumbnail: imageInfo.uri, hash },
2020-12-30 00:56:25 +01:00
uploading: true
}
})
break
case 'video':
2021-05-09 21:59:03 +02:00
attachmentType = `video/${imageInfo.uri.split('.')[1]}`
VideoThumbnails.getThumbnailAsync(imageInfo.uri)
2020-12-30 00:56:25 +01:00
.then(({ uri }) =>
composeDispatch({
type: 'attachment/upload/start',
payload: {
2021-05-09 21:59:03 +02:00
local: { ...imageInfo, local_thumbnail: uri, hash },
2020-12-30 00:56:25 +01:00
uploading: true
}
})
)
.catch(() =>
composeDispatch({
type: 'attachment/upload/start',
payload: {
2021-05-09 21:59:03 +02:00
local: { ...imageInfo, hash },
2020-12-30 00:56:25 +01:00
uploading: true
}
})
)
break
default:
2021-01-14 00:43:35 +01:00
attachmentType = 'unknown'
2020-12-30 00:56:25 +01:00
composeDispatch({
type: 'attachment/upload/start',
payload: {
2021-05-09 21:59:03 +02:00
local: { ...imageInfo, hash },
2020-12-30 00:56:25 +01:00
uploading: true
}
})
break
}
2021-01-19 01:13:45 +01:00
const uploadFailed = () => {
composeDispatch({
type: 'attachment/upload/fail',
payload: hash
})
Alert.alert(
i18next.t(
2021-03-28 23:31:10 +02:00
'screenCompose:content.root.actions.attachment.failed.alert.title'
2021-01-19 01:13:45 +01:00
),
undefined,
[
{
text: i18next.t(
2021-03-28 23:31:10 +02:00
'screenCompose:content.root.actions.attachment.failed.alert.button'
2021-01-19 01:13:45 +01:00
),
onPress: () => {}
}
]
)
}
2020-12-30 00:56:25 +01:00
const formData = new FormData()
formData.append('file', {
// @ts-ignore
2021-05-09 21:59:03 +02:00
uri: imageInfo.uri,
2021-01-14 00:43:35 +01:00
name: attachmentType,
type: attachmentType
2020-12-30 00:56:25 +01:00
})
2021-02-20 19:12:44 +01:00
return apiInstance<Mastodon.Attachment>({
2020-12-30 00:56:25 +01:00
method: 'post',
url: 'media',
body: formData
})
2021-01-07 19:13:09 +01:00
.then(res => {
2021-02-11 01:33:31 +01:00
if (res.body.id) {
2020-12-30 00:56:25 +01:00
composeDispatch({
type: 'attachment/upload/end',
2021-05-09 21:59:03 +02:00
payload: { remote: res.body, local: imageInfo }
2020-12-30 00:56:25 +01:00
})
} else {
2021-01-19 01:13:45 +01:00
uploadFailed()
2020-12-30 00:56:25 +01:00
}
})
.catch(() => {
2021-01-19 01:13:45 +01:00
uploadFailed()
2020-12-30 00:56:25 +01:00
})
}
2021-05-13 02:03:24 +02:00
const result = await mediaSelector({ showActionSheetWithOptions })
await uploader(result)
2020-12-30 00:56:25 +01:00
}
export default addAttachment