Skip to content

Commit

Permalink
fix: simplify download video button logic
Browse files Browse the repository at this point in the history
  • Loading branch information
IEduStu authored Apr 30, 2024
1 parent 9e31fda commit 48198a4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
35 changes: 7 additions & 28 deletions src/routes/Player/OptionsMenu/OptionsMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { useServices } = require('stremio/services');
const Option = require('./Option');
const styles = require('./styles');

const OptionsMenu = ({ className, stream, playbackDevices, videoFilenameRef }) => {
const OptionsMenu = ({ className, stream, playbackDevices }) => {
const { t } = useTranslation();
const { core } = useServices();
const toast = useToast();
Expand Down Expand Up @@ -47,32 +47,12 @@ const OptionsMenu = ({ className, stream, playbackDevices, videoFilenameRef }) =
}
}, [streamingUrl, downloadUrl]);
const onDownloadVideoButtonClick = React.useCallback(() => {
if (streamingUrl && videoFilenameRef.current) {
if (streamingUrl) {
const parsedUrl = new URL(streamingUrl);

if (parsedUrl.pathname.endsWith(encodeURIComponent(videoFilenameRef.current))) {
window.open(parsedUrl.href, '_blank');
} else {
if (!parsedUrl.pathname.endsWith('/'))
parsedUrl.pathname += '/';

parsedUrl.pathname += encodeURIComponent(videoFilenameRef.current);

(async () => {
try {
// make sure that the streaming server supports adding the filename to the URL before opening this link
const headRes = await fetch(parsedUrl.href, { method: 'HEAD' });
if (headRes.ok)
window.open(parsedUrl.href, '_blank');
else
window.open(streamingUrl, '_blank');
} catch(err) {
window.open(streamingUrl, '_blank');
}
})();
}
} else if (streamingUrl || downloadUrl) {
window.open(streamingUrl || downloadUrl, '_blank');
parsedUrl.searchParams.set('download', '1');
window.open(parsedUrl.href, '_blank');
} else if (downloadUrl) {
window.open(downloadUrl, '_blank');
}
}, [streamingUrl, downloadUrl]);
const onExternalDeviceRequested = React.useCallback((deviceId) => {
Expand Down Expand Up @@ -147,8 +127,7 @@ const OptionsMenu = ({ className, stream, playbackDevices, videoFilenameRef }) =
OptionsMenu.propTypes = {
className: PropTypes.string,
stream: PropTypes.object,
playbackDevices: PropTypes.array,
videoFilenameRef: PropTypes.object
playbackDevices: PropTypes.array
};

module.exports = OptionsMenu;
3 changes: 0 additions & 3 deletions src/routes/Player/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const Player = ({ urlParams, queryParams }) => {
const routeFocused = useRouteFocused();
const toast = useToast();
const lastPlayTimeRef = React.useRef(null);
const videoFilenameRef = React.useRef(null);

const [casting, setCasting] = React.useState(() => {
return chromecast.active && chromecast.transport.getCastState() === cast.framework.CastState.CONNECTED;
Expand Down Expand Up @@ -393,7 +392,6 @@ const Player = ({ urlParams, queryParams }) => {

React.useEffect(() => {
videoParamsChanged(video.state.videoParams);
videoFilenameRef.current = video.state.videoParams?.filename;
}, [video.state.videoParams]);

React.useEffect(() => {
Expand Down Expand Up @@ -815,7 +813,6 @@ const Player = ({ urlParams, queryParams }) => {
className={classnames(styles['layer'], styles['menu-layer'])}
stream={player.selected.stream}
playbackDevices={streamingServer.playbackDevices !== null && streamingServer.playbackDevices.type === 'Ready' ? streamingServer.playbackDevices.content : []}
videoFilenameRef={videoFilenameRef}
/>
:
null
Expand Down

0 comments on commit 48198a4

Please sign in to comment.