mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Fixed #108
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
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 } from 'react-native'
|
||||
@ -8,6 +7,7 @@ import { ActionSheetOptions } from '@expo/react-native-action-sheet'
|
||||
import i18next from 'i18next'
|
||||
import apiInstance from '@api/instance'
|
||||
import mediaSelector from '@components/mediaSelector'
|
||||
import { ImageOrVideo } from 'react-native-image-crop-picker'
|
||||
|
||||
export interface Props {
|
||||
composeDispatch: Dispatch<ComposeAction>
|
||||
@ -19,39 +19,38 @@ export interface Props {
|
||||
|
||||
export const uploadAttachment = async ({
|
||||
composeDispatch,
|
||||
imageInfo
|
||||
media
|
||||
}: {
|
||||
composeDispatch: Dispatch<ComposeAction>
|
||||
imageInfo: Pick<ImageInfo, 'type' | 'uri' | 'width' | 'height'>
|
||||
media: Pick<ImageOrVideo, 'path' | 'mime' | 'width' | 'height'>
|
||||
}) => {
|
||||
const hash = await Crypto.digestStringAsync(
|
||||
Crypto.CryptoDigestAlgorithm.SHA256,
|
||||
imageInfo.uri + Math.random()
|
||||
media.path + Math.random()
|
||||
)
|
||||
|
||||
let attachmentType: string
|
||||
|
||||
switch (imageInfo.type) {
|
||||
switch (media.mime.split('/')[0]) {
|
||||
case 'image':
|
||||
console.log('uri', imageInfo.uri)
|
||||
attachmentType = `image/${imageInfo.uri.split('.')[1]}`
|
||||
attachmentType = `image/${media.path.split('.')[1]}`
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...imageInfo, local_thumbnail: imageInfo.uri, hash },
|
||||
local: { ...media, type: 'image', local_thumbnail: media.path, hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
break
|
||||
case 'video':
|
||||
attachmentType = `video/${imageInfo.uri.split('.')[1]}`
|
||||
VideoThumbnails.getThumbnailAsync(imageInfo.uri)
|
||||
attachmentType = `video/${media.path.split('.')[1]}`
|
||||
VideoThumbnails.getThumbnailAsync(media.path)
|
||||
.then(({ uri, width, height }) =>
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: {
|
||||
...imageInfo,
|
||||
...media,
|
||||
type: 'video',
|
||||
local_thumbnail: uri,
|
||||
hash,
|
||||
width,
|
||||
@ -65,7 +64,7 @@ export const uploadAttachment = async ({
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...imageInfo, hash },
|
||||
local: { ...media, type: 'video', hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
@ -76,7 +75,7 @@ export const uploadAttachment = async ({
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/start',
|
||||
payload: {
|
||||
local: { ...imageInfo, hash },
|
||||
local: { ...media, type: 'unknown', hash },
|
||||
uploading: true
|
||||
}
|
||||
})
|
||||
@ -106,14 +105,14 @@ export const uploadAttachment = async ({
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', {
|
||||
// @ts-ignore
|
||||
uri: imageInfo.uri,
|
||||
uri: `file://${media.path}`,
|
||||
name: attachmentType,
|
||||
type: attachmentType
|
||||
})
|
||||
} as any)
|
||||
|
||||
return apiInstance<Mastodon.Attachment>({
|
||||
method: 'post',
|
||||
version: 'v2',
|
||||
url: 'media',
|
||||
body: formData
|
||||
})
|
||||
@ -121,7 +120,7 @@ export const uploadAttachment = async ({
|
||||
if (res.body.id) {
|
||||
composeDispatch({
|
||||
type: 'attachment/upload/end',
|
||||
payload: { remote: res.body, local: imageInfo }
|
||||
payload: { remote: res.body, local: media }
|
||||
})
|
||||
} else {
|
||||
uploadFailed()
|
||||
@ -136,8 +135,13 @@ const chooseAndUploadAttachment = async ({
|
||||
composeDispatch,
|
||||
showActionSheetWithOptions
|
||||
}: Props): Promise<any> => {
|
||||
const result = await mediaSelector({ showActionSheetWithOptions })
|
||||
await uploadAttachment({ composeDispatch, imageInfo: result })
|
||||
const result = await mediaSelector({
|
||||
indicateMaximum: true,
|
||||
showActionSheetWithOptions
|
||||
})
|
||||
for (const media of result) {
|
||||
uploadAttachment({ composeDispatch, media })
|
||||
}
|
||||
}
|
||||
|
||||
export default chooseAndUploadAttachment
|
||||
|
@ -83,9 +83,9 @@ const ComposeTextInput: React.FC = () => {
|
||||
if (nativeEvent.linkUri) {
|
||||
uploadAttachment({
|
||||
composeDispatch,
|
||||
imageInfo: {
|
||||
uri: nativeEvent.linkUri,
|
||||
type: 'image',
|
||||
media: {
|
||||
path: nativeEvent.linkUri,
|
||||
mime: 'image/gif',
|
||||
width: 100,
|
||||
height: 100
|
||||
}
|
||||
|
8
src/screens/Compose/utils/types.d.ts
vendored
8
src/screens/Compose/utils/types.d.ts
vendored
@ -1,6 +1,12 @@
|
||||
import { ImageOrVideo } from 'react-native-image-crop-picker'
|
||||
|
||||
export type ExtendedAttachment = {
|
||||
remote?: Mastodon.Attachment
|
||||
local?: App.IImageInfo & { local_thumbnail?: string; hash?: string }
|
||||
local?: Pick<ImageOrVideo, 'path' | 'width' | 'height'> & {
|
||||
type: 'image' | 'video' | 'unknown'
|
||||
local_thumbnail?: string
|
||||
hash?: string
|
||||
}
|
||||
uploading?: boolean
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user