Rocket.Chat.ReactNative/app/containers/message/Video.tsx

232 lines
6.2 KiB
TypeScript
Raw Normal View History

import React, { useContext, useEffect, useRef, useState } from 'react';
import { StyleProp, StyleSheet, TextStyle, View, Text } from 'react-native';
import { dequal } from 'dequal';
2023-07-04 03:35:31 +00:00
import * as VideoThumbnails from 'expo-video-thumbnails';
import FastImage from 'react-native-fast-image';
2023-07-04 03:35:31 +00:00
import messageStyles from './styles';
import Touchable from './Touchable';
import Markdown from '../markdown';
import { isIOS } from '../../lib/methods/helpers';
import { themes } from '../../lib/constants';
import MessageContext from './Context';
import { fileDownload } from './helpers/fileDownload';
import EventEmitter from '../../lib/methods/helpers/events';
import { LISTENER } from '../Toast';
import I18n from '../../i18n';
import { IAttachment } from '../../definitions/IAttachment';
import { TGetCustomEmoji } from '../../definitions/IEmoji';
import { useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
2023-07-04 03:35:31 +00:00
import { cancelDownload, downloadMediaFile, isDownloadActive, getMediaCache } from '../../lib/methods/handleMediaDownload';
2023-06-07 21:15:37 +00:00
import { fetchAutoDownloadEnabled } from '../../lib/methods/autoDownloadPreference';
import sharedStyles from '../../views/Styles';
2023-07-04 03:35:31 +00:00
import BlurComponent from './Components/BlurComponent';
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;
const styles = StyleSheet.create({
button: {
flex: 1,
borderRadius: 4,
height: 150,
marginBottom: 6,
alignItems: 'center',
justifyContent: 'center'
},
cancelContainer: {
position: 'absolute',
top: 8,
right: 8
},
text: {
...sharedStyles.textRegular,
fontSize: 12
2023-07-04 03:35:31 +00:00
},
thumbnailImage: {
borderRadius: 4,
width: '100%',
height: '100%'
}
});
interface IMessageVideo {
file: IAttachment;
showAttachment?: (file: IAttachment) => void;
getCustomEmoji: TGetCustomEmoji;
2022-03-21 20:44:06 +00:00
style?: StyleProp<TextStyle>[];
isReply?: boolean;
}
2023-07-04 03:35:31 +00:00
const CancelIndicator = () => {
const { colors } = useTheme();
2023-07-04 03:35:31 +00:00
return (
<View style={styles.cancelContainer}>
<Text style={[styles.text, { color: colors.auxiliaryText }]}>{I18n.t('Cancel')}</Text>
</View>
);
};
const Thumbnail = ({ loading, video, cached }: { loading: boolean; video: string; cached: boolean }) => {
const [thumbnailImage, setThumbnailImage] = useState('');
useEffect(() => {
const generateThumbnail = async () => {
try {
const { uri } = await VideoThumbnails.getThumbnailAsync(video, {
time: 1
});
setThumbnailImage(uri);
} catch (e) {
// do nothing
}
};
generateThumbnail();
}, []);
return (
<>
2023-07-04 03:35:31 +00:00
{thumbnailImage ? <FastImage style={styles.thumbnailImage} source={{ uri: thumbnailImage }} /> : null}
<BlurComponent iconName={cached ? 'play-filled' : 'arrow-down-circle'} loading={loading} style={styles.button} />
{loading ? <CancelIndicator /> : null}
</>
);
};
const Video = React.memo(
2023-06-07 21:15:37 +00:00
({ file, showAttachment, getCustomEmoji, style, isReply }: IMessageVideo) => {
const [videoCached, setVideoCached] = useState(file);
2023-07-04 03:35:31 +00:00
const [loading, setLoading] = useState(true);
const [cached, setCached] = useState(false);
const { baseUrl, user } = useContext(MessageContext);
const { theme } = useTheme();
const filePath = useRef('');
const video = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl);
useEffect(() => {
2023-06-07 21:15:37 +00:00
const handleVideoSearchAndDownload = async () => {
if (video) {
const cachedVideoResult = await getMediaCache({
2023-06-07 21:15:37 +00:00
type: 'video',
mimeType: file.video_type,
2023-06-07 21:15:37 +00:00
urlToCache: video
});
filePath.current = cachedVideoResult.filePath;
const downloadActive = isDownloadActive(video);
if (cachedVideoResult.file?.exists) {
2023-06-07 21:15:37 +00:00
setVideoCached(prev => ({
...prev,
video_url: cachedVideoResult.file?.uri
}));
2023-07-04 03:35:31 +00:00
setLoading(false);
2023-07-04 03:54:25 +00:00
setCached(true);
if (downloadActive) {
cancelDownload(video);
}
return;
}
2023-07-04 03:35:31 +00:00
if (isReply) {
2023-07-04 03:54:25 +00:00
setLoading(false);
2023-07-04 03:35:31 +00:00
return;
}
2023-06-07 21:15:37 +00:00
await handleAutoDownload();
}
};
2023-06-07 21:15:37 +00:00
handleVideoSearchAndDownload();
}, []);
if (!baseUrl) {
return null;
}
2023-06-07 21:15:37 +00:00
const handleAutoDownload = async () => {
const isAutoDownloadEnabled = fetchAutoDownloadEnabled('videoPreferenceDownload');
2023-06-07 21:15:37 +00:00
if (isAutoDownloadEnabled) {
await handleDownload();
}
};
const handleDownload = async () => {
setLoading(true);
2023-05-19 14:35:36 +00:00
try {
const videoUri = await downloadMediaFile({
downloadUrl: video,
path: filePath.current
});
2023-06-07 21:15:37 +00:00
setVideoCached(prev => ({
...prev,
2023-05-19 14:35:36 +00:00
video_url: videoUri
}));
2023-07-04 03:35:31 +00:00
setCached(true);
2023-07-04 03:54:25 +00:00
} catch {
setCached(false);
2023-05-19 14:35:36 +00:00
} finally {
setLoading(false);
}
};
const onPress = async () => {
2023-07-04 03:35:31 +00:00
if (file.video_type && cached && isTypeSupported(file.video_type) && showAttachment) {
showAttachment(videoCached);
return;
}
if (!loading) {
handleDownload();
return;
}
if (loading) {
handleCancelDownload();
return;
}
if (!isIOS && file.video_url) {
await downloadVideoToGallery(video);
return;
}
EventEmitter.emit(LISTENER, { message: I18n.t('Unsupported_format') });
};
const handleCancelDownload = () => {
if (loading) {
cancelDownload(video);
2023-07-04 03:35:31 +00:00
setLoading(false);
}
};
const downloadVideoToGallery = async (uri: string) => {
setLoading(true);
const fileDownloaded = await fileDownload(uri, file);
setLoading(false);
if (fileDownloaded) {
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
return;
}
EventEmitter.emit(LISTENER, { message: I18n.t('error-save-video') });
};
return (
<>
<Markdown
msg={file.description}
username={user.username}
getCustomEmoji={getCustomEmoji}
2022-03-21 20:44:06 +00:00
style={[isReply && style]}
theme={theme}
/>
2022-03-21 20:44:06 +00:00
<Touchable
disabled={isReply}
onPress={onPress}
2023-07-04 03:35:31 +00:00
style={[styles.button, messageStyles.mustWrapBlur, { backgroundColor: themes[theme].videoBackground }]}
2023-06-07 21:15:37 +00:00
background={Touchable.Ripple(themes[theme].bannerBackground)}
>
2023-07-04 03:35:31 +00:00
<Thumbnail loading={loading} video={video} cached={cached} />
2022-03-21 20:44:06 +00:00
</Touchable>
</>
);
},
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
);
export default Video;