Skip to content

Commit baa5360

Browse files
committed
fix: camera roll issues
1 parent dc7d09c commit baa5360

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

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

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

35
try {
@@ -12,7 +14,11 @@ try {
1214
export const getLocalAssetUri = CameraRollDependency
1315
? async (remoteUri: string) => {
1416
try {
15-
const localUri = await CameraRollDependency.CameraRoll.save(remoteUri);
17+
let localUri = remoteUri;
18+
if (Platform.OS === 'ios') {
19+
const imageData = await CameraRollDependency.CameraRoll.iosGetImageDataById(remoteUri);
20+
localUri = imageData?.node?.image?.filepath;
21+
}
1622
return localUri;
1723
} catch {
1824
throw new Error('getLocalAssetUri Error');

package/src/components/AttachmentPicker/AttachmentPicker.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,13 @@ export const AttachmentPicker = React.forwardRef(
237237
// `id` is available for Expo MediaLibrary while Cameraroll doesn't share id therefore we use `uri`
238238
selected:
239239
selectedImages.some((image) =>
240-
image.id ? image.id === asset.id : image.uri === asset.uri,
240+
image.id
241+
? image.id === asset.id
242+
: image.uri === asset.uri || image.originalUri === asset.uri,
241243
) ||
242-
selectedFiles.some((file) => (file.id ? file.id === asset.id : file.uri === asset.uri)),
244+
selectedFiles.some((file) =>
245+
file.id ? file.id === asset.id : file.uri === asset.uri || file.originalUri === asset.uri,
246+
),
243247
selectedFiles,
244248
selectedImages,
245249
setSelectedFiles,

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ const AttachmentVideo = (props: AttachmentVideoProps) => {
5858
/* Patches video files with uri and mimetype */
5959
const patchVideoFile = async (files: File[]) => {
6060
// 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;
6162
const localAssetURI =
62-
Platform.OS === 'ios' && asset.id && getLocalAssetUri && (await getLocalAssetUri(asset.id));
63+
Platform.OS === 'ios' &&
64+
identifier &&
65+
getLocalAssetUri &&
66+
(await getLocalAssetUri(identifier));
6367
const uri = localAssetURI || asset.uri || '';
6468
// We need a mime-type to upload a video file.
6569
const mimeType = lookup(asset.name) || 'multipart/form-data';
@@ -70,6 +74,7 @@ const AttachmentVideo = (props: AttachmentVideoProps) => {
7074
id: asset.id,
7175
mimeType,
7276
name: asset.name,
77+
originalUri: asset.uri,
7378
size: asset.size,
7479
uri,
7580
},
@@ -89,7 +94,9 @@ const AttachmentVideo = (props: AttachmentVideoProps) => {
8994
if (selected) {
9095
setSelectedFiles((files) =>
9196
// `id` is available for Expo MediaLibrary while Cameraroll doesn't share id therefore we use `uri`
92-
files.filter((file) => (file.id ? file.id !== asset.id : file.uri !== asset.uri)),
97+
files.filter((file) =>
98+
file.id ? file.id !== asset.id : file.uri !== asset.uri && file.originalUri !== asset.uri,
99+
),
93100
);
94101
} else {
95102
updateSelectedFiles();
@@ -154,13 +161,18 @@ const AttachmentImage = (props: AttachmentImageProps) => {
154161
/* Patches image files with uri */
155162
const patchImageFile = async (images: Asset[]) => {
156163
// 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.
164+
const identifier = asset.id || asset.uri;
157165
const localAssetURI =
158-
Platform.OS === 'ios' && asset.id && getLocalAssetUri && (await getLocalAssetUri(asset.id));
166+
Platform.OS === 'ios' &&
167+
identifier &&
168+
getLocalAssetUri &&
169+
(await getLocalAssetUri(identifier));
159170
const uri = localAssetURI || asset.uri || '';
160171
return [
161172
...images,
162173
{
163174
...asset,
175+
originalUri: asset.uri,
164176
uri,
165177
},
166178
];
@@ -179,7 +191,11 @@ const AttachmentImage = (props: AttachmentImageProps) => {
179191
if (selected) {
180192
// `id` is available for Expo MediaLibrary while Cameraroll doesn't share id therefore we use `uri`
181193
setSelectedImages((images) =>
182-
images.filter((image) => (image.id ? image.id !== asset.id : image.uri !== asset.uri)),
194+
images.filter((image) =>
195+
image.id
196+
? image.id !== asset.id
197+
: image.uri !== asset.uri && image.originalUri !== asset.uri,
198+
),
183199
);
184200
} else {
185201
updateSelectedImages();

package/src/types/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type Asset = {
2121
uri: string;
2222
width: number;
2323
id?: string;
24+
originalUri?: string;
2425
size?: number;
2526
};
2627

@@ -29,6 +30,7 @@ export type File = {
2930
duration?: number;
3031
id?: string;
3132
mimeType?: string;
33+
originalUri?: string;
3234
size?: number;
3335
type?: FileTypes;
3436
// The uri should be of type `string`. But is `string|undefined` because the same type is used for the response from Stream's Attachment. This shall be fixed.

0 commit comments

Comments
 (0)