mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Partially fixed #113
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import * as ImagePicker from 'expo-image-picker'
|
||||
import * as Crypto from 'expo-crypto'
|
||||
import { ImageInfo } from 'expo-image-picker/build/ImagePicker.types'
|
||||
import * as VideoThumbnails from 'expo-video-thumbnails'
|
||||
import { Dispatch } from 'react'
|
||||
import { Alert, Linking } from 'react-native'
|
||||
import { Alert } from 'react-native'
|
||||
import { ComposeAction } from '../../utils/types'
|
||||
import { ActionSheetOptions } from '@expo/react-native-action-sheet'
|
||||
import i18next from 'i18next'
|
||||
import analytics from '@components/analytics'
|
||||
import apiInstance from '@api/instance'
|
||||
import mediaSelector from '@components/mediaSelector'
|
||||
|
||||
export interface Props {
|
||||
composeDispatch: Dispatch<ComposeAction>
|
||||
@@ -22,35 +21,33 @@ const addAttachment = async ({
|
||||
composeDispatch,
|
||||
showActionSheetWithOptions
|
||||
}: Props): Promise<any> => {
|
||||
const uploadAttachment = async (result: ImageInfo) => {
|
||||
const uploader = async (imageInfo: ImageInfo) => {
|
||||
const hash = await Crypto.digestStringAsync(
|
||||
Crypto.CryptoDigestAlgorithm.SHA256,
|
||||
result.uri + Math.random()
|
||||
imageInfo.uri + Math.random()
|
||||
)
|
||||
|
||||
let attachmentType: string
|
||||
// https://github.com/expo/expo/issues/11214
|
||||
const attachmentUri = result.uri.replace('file:/data', 'file:///data')
|
||||
|
||||
switch (result.type) {
|
||||
switch (imageInfo.type) {
|
||||
case 'image':
|
||||
attachmentType = `image/${attachmentUri.split('.')[1]}`
|
||||
attachmentType = `image/${imageInfo.uri.split('.')[1]}`
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...result, local_thumbnail: attachmentUri, hash },
|
||||
local: { ...imageInfo, local_thumbnail: imageInfo.uri, hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
break
|
||||
case 'video':
|
||||
attachmentType = `video/${attachmentUri.split('.')[1]}`
|
||||
VideoThumbnails.getThumbnailAsync(attachmentUri)
|
||||
attachmentType = `video/${imageInfo.uri.split('.')[1]}`
|
||||
VideoThumbnails.getThumbnailAsync(imageInfo.uri)
|
||||
.then(({ uri }) =>
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...result, local_thumbnail: uri, hash },
|
||||
local: { ...imageInfo, local_thumbnail: uri, hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
@@ -59,7 +56,7 @@ const addAttachment = async ({
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...result, hash },
|
||||
local: { ...imageInfo, hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
@@ -70,7 +67,7 @@ const addAttachment = async ({
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...result, hash },
|
||||
local: { ...imageInfo, hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
@@ -101,7 +98,7 @@ const addAttachment = async ({
|
||||
const formData = new FormData()
|
||||
formData.append('file', {
|
||||
// @ts-ignore
|
||||
uri: attachmentUri,
|
||||
uri: imageInfo.uri,
|
||||
name: attachmentType,
|
||||
type: attachmentType
|
||||
})
|
||||
@@ -115,7 +112,7 @@ const addAttachment = async ({
|
||||
if (res.body.id) {
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/end',
|
||||
payload: { remote: res.body, local: result }
|
||||
payload: { remote: res.body, local: imageInfo }
|
||||
})
|
||||
} else {
|
||||
uploadFailed()
|
||||
@@ -126,119 +123,7 @@ const addAttachment = async ({
|
||||
})
|
||||
}
|
||||
|
||||
showActionSheetWithOptions(
|
||||
{
|
||||
options: [
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.options.library'
|
||||
),
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.options.photo'
|
||||
),
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.options.cancel'
|
||||
)
|
||||
],
|
||||
cancelButtonIndex: 2
|
||||
},
|
||||
async buttonIndex => {
|
||||
if (buttonIndex === 0) {
|
||||
const {
|
||||
status
|
||||
} = await ImagePicker.requestMediaLibraryPermissionsAsync()
|
||||
if (status !== 'granted') {
|
||||
Alert.alert(
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.library.alert.title'
|
||||
),
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.library.alert.message'
|
||||
),
|
||||
[
|
||||
{
|
||||
text: i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.library.alert.buttons.cancel'
|
||||
),
|
||||
style: 'cancel',
|
||||
onPress: () => {
|
||||
analytics('compose_addattachment_medialibrary_nopermission', {
|
||||
action: 'cancel'
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
text: i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.library.alert.buttons.settings'
|
||||
),
|
||||
style: 'default',
|
||||
onPress: () => {
|
||||
analytics('compose_addattachment_medialibrary_nopermission', {
|
||||
action: 'settings'
|
||||
})
|
||||
Linking.openURL('app-settings:')
|
||||
}
|
||||
}
|
||||
]
|
||||
)
|
||||
} else {
|
||||
const result = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
||||
exif: false
|
||||
})
|
||||
|
||||
if (!result.cancelled) {
|
||||
uploadAttachment(result)
|
||||
}
|
||||
}
|
||||
} else if (buttonIndex === 1) {
|
||||
const { status } = await ImagePicker.requestCameraPermissionsAsync()
|
||||
if (status !== 'granted') {
|
||||
Alert.alert(
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.photo.alert.title'
|
||||
),
|
||||
i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.photo.alert.message'
|
||||
),
|
||||
[
|
||||
{
|
||||
text: i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.photo.alert.buttons.cancel'
|
||||
),
|
||||
style: 'cancel',
|
||||
onPress: () => {
|
||||
analytics('compose_addattachment_camera_nopermission', {
|
||||
action: 'cancel'
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
text: i18next.t(
|
||||
'screenCompose:content.root.actions.attachment.actions.photo.alert.buttons.settings'
|
||||
),
|
||||
style: 'default',
|
||||
onPress: () => {
|
||||
analytics('compose_addattachment_camera_nopermission', {
|
||||
action: 'settings'
|
||||
})
|
||||
Linking.openURL('app-settings:')
|
||||
}
|
||||
}
|
||||
]
|
||||
)
|
||||
} else {
|
||||
const result = await ImagePicker.launchCameraAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.All,
|
||||
exif: false
|
||||
})
|
||||
|
||||
if (!result.cancelled) {
|
||||
uploadAttachment(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
mediaSelector({ uploader, showActionSheetWithOptions })
|
||||
}
|
||||
|
||||
export default addAttachment
|
||||
|
Reference in New Issue
Block a user