2023-05-16 02:10:14 +00:00
|
|
|
import React, { useContext, useLayoutEffect, useRef, useState } from 'react';
|
|
|
|
import { StyleProp, StyleSheet, TextStyle, View, Text } from 'react-native';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { dequal } from 'dequal';
|
2023-05-16 02:10:14 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
import Touchable from './Touchable';
|
|
|
|
import Markdown from '../markdown';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isIOS } from '../../lib/methods/helpers';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import MessageContext from './Context';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { fileDownload } from './helpers/fileDownload';
|
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2021-11-16 15:59:58 +00:00
|
|
|
import { LISTENER } from '../Toast';
|
|
|
|
import I18n from '../../i18n';
|
2022-01-11 13:51:48 +00:00
|
|
|
import { IAttachment } from '../../definitions/IAttachment';
|
2021-11-16 15:59:58 +00:00
|
|
|
import RCActivityIndicator from '../ActivityIndicator';
|
2022-02-17 15:27:01 +00:00
|
|
|
import { TGetCustomEmoji } from '../../definitions/IEmoji';
|
2022-04-01 21:52:38 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
|
2023-05-16 02:10:14 +00:00
|
|
|
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
|
|
|
|
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
|
2022-04-01 21:52:38 +00:00
|
|
|
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
flex: 1,
|
|
|
|
borderRadius: 4,
|
|
|
|
height: 150,
|
|
|
|
marginBottom: 6,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center'
|
2023-05-16 02:10:14 +00:00
|
|
|
},
|
|
|
|
cancelContainer: {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 8,
|
|
|
|
right: 8
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 12
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IMessageVideo {
|
2022-01-11 13:51:48 +00:00
|
|
|
file: IAttachment;
|
2022-04-01 21:52:38 +00:00
|
|
|
showAttachment?: (file: IAttachment) => void;
|
2022-02-17 15:27:01 +00:00
|
|
|
getCustomEmoji: TGetCustomEmoji;
|
2022-03-21 20:44:06 +00:00
|
|
|
style?: StyleProp<TextStyle>[];
|
|
|
|
isReply?: boolean;
|
2023-05-16 02:10:14 +00:00
|
|
|
messageId: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 02:10:14 +00:00
|
|
|
const DownloadIndicator = ({ handleCancelDownload }: { handleCancelDownload(): void }) => {
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<View style={styles.cancelContainer}>
|
|
|
|
<Touchable background={Touchable.Ripple(colors.bannerBackground)} onPress={handleCancelDownload}>
|
|
|
|
<Text style={[styles.text, { color: colors.auxiliaryText }]}>{I18n.t('Cancel')}</Text>
|
|
|
|
</Touchable>
|
|
|
|
</View>
|
|
|
|
<RCActivityIndicator />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const Video = React.memo(
|
2023-05-16 02:10:14 +00:00
|
|
|
({ file, showAttachment, getCustomEmoji, style, isReply, messageId }: IMessageVideo) => {
|
2021-11-16 15:59:58 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2023-05-16 02:10:14 +00:00
|
|
|
const { baseUrl, user } = useContext(MessageContext);
|
2022-04-01 21:52:38 +00:00
|
|
|
const { theme } = useTheme();
|
2023-05-16 02:10:14 +00:00
|
|
|
const filePath = useRef('');
|
|
|
|
const downloadResumable = useRef<FileSystem.DownloadResumable | null>(null);
|
|
|
|
const video = formatAttachmentUrl(file.video_url, user.id, user.token, baseUrl);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
const handleAutoDownload = async () => {
|
|
|
|
if (video) {
|
|
|
|
const searchImageBestQuality = await searchMediaFileAsync({
|
|
|
|
type: MediaTypes.video,
|
|
|
|
mimeType: file.video_type,
|
|
|
|
messageId
|
|
|
|
});
|
|
|
|
filePath.current = searchImageBestQuality.filePath;
|
|
|
|
if (searchImageBestQuality.file?.exists) {
|
|
|
|
file.video_url = searchImageBestQuality.file.uri;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't pass the author to avoid auto-download what the user sent
|
|
|
|
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user });
|
|
|
|
if (autoDownload) {
|
|
|
|
await handleDownload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
handleAutoDownload();
|
|
|
|
}, []);
|
2021-11-16 15:59:58 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (!baseUrl) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-01 21:52:38 +00:00
|
|
|
|
2023-05-16 02:10:14 +00:00
|
|
|
const handleDownload = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
downloadResumable.current = FileSystem.createDownloadResumable(video, filePath.current);
|
|
|
|
const videoUri = await downloadMediaFile({
|
|
|
|
url: video,
|
|
|
|
filePath: filePath.current,
|
|
|
|
downloadResumable: downloadResumable.current
|
|
|
|
});
|
|
|
|
if (videoUri) {
|
|
|
|
file.video_url = videoUri;
|
|
|
|
}
|
|
|
|
setLoading(false);
|
|
|
|
};
|
|
|
|
|
2021-11-16 15:59:58 +00:00
|
|
|
const onPress = async () => {
|
2022-04-01 21:52:38 +00:00
|
|
|
if (file.video_type && isTypeSupported(file.video_type) && showAttachment) {
|
2023-05-16 02:10:14 +00:00
|
|
|
// Keep the video downloading while showing the video buffering
|
|
|
|
handleDownload();
|
2021-09-13 20:41:05 +00:00
|
|
|
return showAttachment(file);
|
|
|
|
}
|
2021-11-16 15:59:58 +00:00
|
|
|
|
2022-02-07 18:44:04 +00:00
|
|
|
if (!isIOS && file.video_url) {
|
2023-05-16 02:10:14 +00:00
|
|
|
await downloadVideo(video);
|
2021-11-16 15:59:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Unsupported_format') });
|
|
|
|
};
|
|
|
|
|
2023-05-16 02:10:14 +00:00
|
|
|
const handleCancelDownload = () => {
|
|
|
|
if (loading && downloadResumable.current) {
|
|
|
|
return downloadResumable.current.cancelAsync();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-16 15:59:58 +00:00
|
|
|
const downloadVideo = 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') });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Markdown
|
|
|
|
msg={file.description}
|
|
|
|
username={user.username}
|
|
|
|
getCustomEmoji={getCustomEmoji}
|
2022-03-21 20:44:06 +00:00
|
|
|
style={[isReply && style]}
|
2022-07-04 18:10:14 +00:00
|
|
|
theme={theme}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
2022-03-21 20:44:06 +00:00
|
|
|
<Touchable
|
|
|
|
disabled={isReply}
|
|
|
|
onPress={onPress}
|
|
|
|
style={[styles.button, { backgroundColor: themes[theme].videoBackground }]}
|
2022-08-08 21:02:08 +00:00
|
|
|
background={Touchable.Ripple(themes[theme].bannerBackground)}
|
|
|
|
>
|
2023-05-16 02:10:14 +00:00
|
|
|
{loading ? (
|
|
|
|
<DownloadIndicator handleCancelDownload={handleCancelDownload} />
|
|
|
|
) : (
|
|
|
|
<CustomIcon name='play-filled' size={54} color={themes[theme].buttonText} />
|
|
|
|
)}
|
2022-03-21 20:44:06 +00:00
|
|
|
</Touchable>
|
2021-09-13 20:41:05 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
2022-04-01 21:52:38 +00:00
|
|
|
(prevProps, nextProps) => dequal(prevProps.file, nextProps.file)
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export default Video;
|