Skip to content

Commit

Permalink
🛠️ fix(components/BaseAnchorForAttachmentsOnly): handle local files c…
Browse files Browse the repository at this point in the history
…orrectly

Update BaseAnchorForAttachmentsOnly to handle local files by checking their path
before appending an auth token. This resolves issues with local file URLs causing
console errors when attempting to download or display attachments. Use `FileUtils`
to determine if the file is local and adjust the URL accordingly.
  • Loading branch information
kidroca committed Oct 30, 2024
1 parent a37409d commit e562270
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as Browser from '@libs/Browser';
import fileDownload from '@libs/fileDownload';
import * as ReportUtils from '@libs/ReportUtils';
import * as Download from '@userActions/Download';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type AnchorForAttachmentsOnlyProps from './types';
Expand All @@ -23,7 +24,7 @@ type BaseAnchorForAttachmentsOnlyProps = AnchorForAttachmentsOnlyProps & {
};

function BaseAnchorForAttachmentsOnly({style, source = '', displayName = '', onPressIn, onPressOut, isDeleted}: BaseAnchorForAttachmentsOnlyProps) {
const sourceURLWithAuth = addEncryptedAuthTokenToURL(source);
const finalSourceURL = FileUtils.isLocalFile(source) ? source : addEncryptedAuthTokenToURL(source);
const sourceID = (source.match(CONST.REGEX.ATTACHMENT_ID) ?? [])[1];

const [download] = useOnyx(`${ONYXKEYS.COLLECTION.DOWNLOAD}${sourceID}`);
Expand All @@ -43,7 +44,7 @@ function BaseAnchorForAttachmentsOnly({style, source = '', displayName = '', onP
return;
}
Download.setDownload(sourceID, true);
fileDownload(sourceURLWithAuth, displayName, '', Browser.isMobileSafari()).then(() => Download.setDownload(sourceID, false));
fileDownload(finalSourceURL, displayName, '', Browser.isMobileSafari()).then(() => Download.setDownload(sourceID, false));
}}
onPressIn={onPressIn}
onPressOut={onPressOut}
Expand All @@ -58,7 +59,7 @@ function BaseAnchorForAttachmentsOnly({style, source = '', displayName = '', onP
role={CONST.ROLE.BUTTON}
>
<AttachmentView
source={sourceURLWithAuth}
source={finalSourceURL}
file={{name: displayName}}
shouldShowDownloadIcon={!!sourceID && !isOffline}
shouldShowLoadingSpinnerIcon={isDownloading}
Expand Down

0 comments on commit e562270

Please sign in to comment.