Skip to content

Commit 096dde1

Browse files
committed
fix: properly resolve videos too
1 parent e30decf commit 096dde1

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

package/expo-package/src/optionalDependencies/getLocalAssetUri.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Platform } from 'react-native';
2+
13
let MediaLibrary;
24

35
try {
@@ -12,10 +14,14 @@ if (!MediaLibrary) {
1214
);
1315
}
1416

17+
// TODO: The API is different for Expo and RN CLI. We should unify it.
1518
export const getLocalAssetUri = async (assetId: string): Promise<string | undefined> => {
1619
try {
17-
const { localUri } = await MediaLibrary.getAssetInfoAsync(assetId);
18-
return localUri;
20+
if (Platform.OS === 'ios') {
21+
const { localUri } = await MediaLibrary.getAssetInfoAsync(assetId);
22+
return localUri;
23+
}
24+
return null;
1925
} catch {
2026
throw new Error('getLocalAssetUri Error');
2127
}

package/expo-package/src/optionalDependencies/getPhotos.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Platform } from 'react-native';
2+
13
let MediaLibrary;
24

35
try {
@@ -13,6 +15,8 @@ if (!MediaLibrary) {
1315
}
1416
import type { Asset } from 'stream-chat-react-native-core';
1517

18+
import { getLocalAssetUri } from './getLocalAssetUri';
19+
1620
type ReturnType = {
1721
assets: Array<Omit<Asset, 'source'> & { source: 'picker' }>;
1822
endCursor: string | undefined;
@@ -40,9 +44,10 @@ export const getPhotos = MediaLibrary
4044
mediaType: [MediaLibrary.MediaType.photo, MediaLibrary.MediaType.video],
4145
sortBy: [MediaLibrary.SortBy.modificationTime],
4246
});
47+
console.log(results.assets.length);
4348
const assets = await Promise.all(
4449
results.assets.map(async (asset) => {
45-
const assetInfo = await MediaLibrary.getAssetInfoAsync(asset.id);
50+
const localUri = await getLocalAssetUri(asset.id);
4651
return {
4752
duration: asset.duration * 1000,
4853
height: asset.height,
@@ -51,11 +56,12 @@ export const getPhotos = MediaLibrary
5156
originalUri: asset.uri,
5257
source: 'picker' as const,
5358
type: asset.mediaType,
54-
uri: assetInfo.localUri,
59+
uri: localUri || asset.uri,
5560
width: asset.width,
5661
};
5762
}),
5863
);
64+
console.log('ISE: ', assets);
5965

6066
const hasNextPage = results.hasNextPage;
6167
const endCursor = results.endCursor;

package/src/components/AttachmentPicker/components/AttachmentPickerItem.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import { Alert, ImageBackground, Platform, StyleSheet, Text, View } from 'react-native';
3+
import { Alert, ImageBackground, StyleSheet, Text, View } from 'react-native';
44

55
import { TouchableOpacity } from '@gorhom/bottom-sheet';
66
import { lookup } from 'mime-types';
@@ -10,7 +10,6 @@ import { useTheme } from '../../../contexts/themeContext/ThemeContext';
1010
import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext';
1111
import { useViewport } from '../../../hooks/useViewport';
1212
import { Recorder } from '../../../icons';
13-
import { getLocalAssetUri } from '../../../native';
1413
import type { Asset, File } from '../../../types/types';
1514
import { getDurationLabelFromDuration } from '../../../utils/utils';
1615

@@ -56,15 +55,7 @@ const AttachmentVideo = (props: AttachmentVideoProps) => {
5655
const size = vw(100) / (numberOfAttachmentPickerImageColumns || 3) - 2;
5756

5857
/* Patches video files with uri and mimetype */
59-
const patchVideoFile = async (files: File[]) => {
60-
// For the case of Expo CLI where you need to fetch the file uri from file id. Here it is only done for iOS since for android the file.uri is fine.
61-
const identifier = asset.id || asset.uri;
62-
const localAssetURI =
63-
Platform.OS === 'ios' &&
64-
identifier &&
65-
getLocalAssetUri &&
66-
(await getLocalAssetUri(identifier));
67-
const uri = localAssetURI || asset.uri || '';
58+
const patchVideoFile = (files: File[]) => {
6859
// We need a mime-type to upload a video file.
6960
const mimeType = lookup(asset.name) || 'multipart/form-data';
7061
return [
@@ -74,19 +65,19 @@ const AttachmentVideo = (props: AttachmentVideoProps) => {
7465
id: asset.id,
7566
mimeType,
7667
name: asset.name,
77-
originalUri: asset.uri,
68+
originalUri: asset.originalUri,
7869
size: asset.size,
79-
uri,
70+
uri: asset.uri,
8071
},
8172
];
8273
};
8374

84-
const updateSelectedFiles = async () => {
75+
const updateSelectedFiles = () => {
8576
if (numberOfUploads >= maxNumberOfFiles) {
8677
Alert.alert(t('Maximum number of files reached'));
8778
return;
8879
}
89-
const files = await patchVideoFile(selectedFiles);
80+
const files = patchVideoFile(selectedFiles);
9081
setSelectedFiles(files);
9182
};
9283

0 commit comments

Comments
 (0)