Skip to content

Commit

Permalink
Merge pull request #37163 from Krishna2323/krishna2323/issue/37092
Browse files Browse the repository at this point in the history
fix: Video - Invalid file is downloaded when downloading video.
  • Loading branch information
deetergp authored Feb 26, 2024
2 parents 6cd008e + 4e54e81 commit 2509631
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/components/VideoPlayerContexts/VideoPopoverMenuContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import _ from 'underscore';
import * as Expensicons from '@components/Icon/Expensicons';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import fileDownload from '@libs/fileDownload';
import * as Url from '@libs/Url';
import CONST from '@src/CONST';
import {usePlaybackContext} from './PlaybackContext';

const VideoPopoverMenuContext = React.createContext(null);

function VideoPopoverMenuContextProvider({children}) {
const {currentVideoPlayerRef} = usePlaybackContext();
const {currentVideoPlayerRef, currentlyPlayingURL} = usePlaybackContext();
const {translate} = useLocalize();
const [currentPlaybackSpeed, setCurrentPlaybackSpeed] = useState(CONST.VIDEO_PLAYER.PLAYBACK_SPEEDS[2]);
const {isOffline} = useNetwork();
Expand All @@ -26,11 +26,9 @@ function VideoPopoverMenuContextProvider({children}) {
);

const downloadAttachment = useCallback(() => {
currentVideoPlayerRef.current.getStatusAsync().then((status) => {
const sourceURI = `/${Url.getPathFromURL(status.uri)}`;
fileDownload(sourceURI);
});
}, [currentVideoPlayerRef]);
const sourceURI = currentlyPlayingURL.startsWith('blob:') || currentlyPlayingURL.startsWith('file:') ? currentlyPlayingURL : addEncryptedAuthTokenToURL(currentlyPlayingURL);
fileDownload(sourceURI);
}, [currentlyPlayingURL]);

const menuItems = useMemo(() => {
const items = [];
Expand Down
8 changes: 4 additions & 4 deletions src/libs/fileDownload/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ function showCameraPermissionsAlert() {
* Extracts a filename from a given URL and sanitizes it for file system usage.
*
* This function takes a URL as input and performs the following operations:
* 1. Extracts the last segment of the URL, which could be a file name, a path segment,
* or a query string parameter.
* 1. Extracts the last segment of the URL.
* 2. Decodes the extracted segment from URL encoding to a plain string for better readability.
* 3. Replaces any characters in the decoded string that are illegal in file names
* with underscores.
*/
function getFileName(url: string): string {
const fileName = url.split(/[#?/]/).pop() ?? '';
const fileName = url.split('/').pop()?.split('?')[0].split('#')[0] ?? '';

if (!fileName) {
Log.warn('[FileUtils] Could not get attachment name', {url});
}
Expand All @@ -111,7 +111,7 @@ function getFileType(fileUrl: string): string | undefined {
return;
}

const fileName = fileUrl.split('/').pop()?.split('?')[0].split('#')[0];
const fileName = getFileName(fileUrl);

if (!fileName) {
return;
Expand Down

0 comments on commit 2509631

Please sign in to comment.