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.
(cherry picked from commit 2509631)
  • Loading branch information
deetergp authored and OSBotify committed Feb 26, 2024
1 parent 07c47ba commit e462e07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/components/VideoPlayerContexts/VideoPopoverMenuContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import React, {useCallback, useContext, useMemo, useState} from 'react';
import _ from 'underscore';
import * as Expensicons from '@components/Icon/Expensicons';
import useLocalize from '@hooks/useLocalize';
<<<<<<< HEAD
=======
import useNetwork from '@hooks/useNetwork';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
>>>>>>> 2509631b (Merge pull request #37163 from Krishna2323/krishna2323/issue/37092)
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]);

Expand All @@ -24,11 +28,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(
() => [
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 e462e07

Please sign in to comment.