Skip to content

Commit

Permalink
[native] Fix cropped avatar selection
Browse files Browse the repository at this point in the history
Summary:
Fixes cropping part of [[ https://linear.app/comm/issue/ENG-4053/setting-avatars-not-working-jaceks-bug-report | ENG-4053 ]].

Avoided replacing image URI with fetched asset URI when the file is in app's cache dir - this is where expo-image-picker stores cropped images.

Test Plan: Set cropped photo as an avatar - it is set correctly. Sent multimedia both with media gallery and image picker - both ways work. Checked both iOS and Android.

Reviewers: atul, ashoat, ginsu

Reviewed By: ashoat

Subscribers: tomek

Differential Revision: https://phab.comm.dev/D8739
  • Loading branch information
barthap committed Aug 6, 2023
1 parent cf4e842 commit a729baa
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions native/media/file-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import base64 from 'base-64';
import * as ExpoFileSystem from 'expo-file-system';
import * as MediaLibrary from 'expo-media-library';
import invariant from 'invariant';
import { Platform } from 'react-native';
Expand Down Expand Up @@ -57,14 +58,14 @@ async function fetchFileInfo(
const { mediaNativeID } = optionalInputs;
const steps = [];

let assetInfoPromise, newLocalURI;
let assetInfoPromise, assetURI;
const inputPath = pathFromURI(inputURI);
if (mediaNativeID && (!inputPath || optionalFields.orientation)) {
assetInfoPromise = (async () => {
const { steps: assetInfoSteps, result: assetInfoResult } =
await fetchAssetInfo(mediaNativeID);
steps.push(...assetInfoSteps);
newLocalURI = assetInfoResult.localURI;
assetURI = assetInfoResult.localURI;
return assetInfoResult;
})();
}
Expand Down Expand Up @@ -150,11 +151,18 @@ async function fetchFileInfo(
}

let finalURI = uri;
if (newLocalURI && newLocalURI !== uri) {
finalURI = newLocalURI;
// prefer asset URI, with one exception:
// if the target URI is a file in our app local cache dir, we shouldn't
// replace it because it was already preprocessed by either our media
// processing logic or cropped by expo-image-picker
const isFileInCacheDir =
uri.includes(temporaryDirectoryPath) ||
uri.includes(ExpoFileSystem.cacheDirectory);
if (assetURI && assetURI !== uri && !isFileInCacheDir) {
finalURI = assetURI;
console.log(
'fetchAssetInfo returned localURI ' +
`${newLocalURI} when we already had ${uri}`,
`${assetURI} when we already had ${uri}`,
);
}

Expand Down

0 comments on commit a729baa

Please sign in to comment.