2019-12-18 21:13:11 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { StyleSheet, View, PermissionsAndroid } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import CameraRoll from '@react-native-community/cameraroll';
|
|
|
|
import * as mime from 'react-native-mime-types';
|
|
|
|
import { FileSystem } from 'react-native-unimodules';
|
|
|
|
import { Video } from 'expo-av';
|
|
|
|
import SHA256 from 'js-sha256';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { withSafeAreaInsets } from 'react-native-safe-area-context';
|
2019-12-18 21:13:11 +00:00
|
|
|
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
import { withTheme } from '../theme';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { ImageViewer } from '../presentation/ImageViewer';
|
2019-12-18 21:13:11 +00:00
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { formatAttachmentUrl } from '../lib/utils';
|
|
|
|
import RCActivityIndicator from '../containers/ActivityIndicator';
|
|
|
|
import { SaveButton, CloseModalButton } from '../containers/HeaderButton';
|
|
|
|
import { isAndroid } from '../utils/deviceInfo';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2020-06-26 20:22:56 +00:00
|
|
|
import { withDimensions } from '../dimensions';
|
|
|
|
import { getHeaderHeight } from '../containers/Header';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-18 21:13:11 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class AttachmentView extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-12-18 21:13:11 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
baseUrl: PropTypes.string,
|
2020-06-26 20:22:56 +00:00
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
insets: PropTypes.object,
|
2019-12-18 21:13:11 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-06-15 14:00:46 +00:00
|
|
|
const attachment = props.route.params?.attachment;
|
2019-12-18 21:13:11 +00:00
|
|
|
this.state = { attachment, loading: true };
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2019-12-18 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { navigation } = this.props;
|
2020-06-15 14:00:46 +00:00
|
|
|
this.unsubscribeBlur = navigation.addListener('blur', () => {
|
2020-02-28 16:18:45 +00:00
|
|
|
if (this.videoRef && this.videoRef.stopAsync) {
|
|
|
|
this.videoRef.stopAsync();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-06-15 14:00:46 +00:00
|
|
|
if (this.unsubscribeBlur) {
|
|
|
|
this.unsubscribeBlur();
|
2020-02-28 16:18:45 +00:00
|
|
|
}
|
2019-12-18 21:13:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
2020-06-26 20:22:56 +00:00
|
|
|
const { route, navigation, theme } = this.props;
|
2020-06-15 14:00:46 +00:00
|
|
|
const attachment = route.params?.attachment;
|
|
|
|
const { title } = attachment;
|
|
|
|
const options = {
|
2020-06-26 20:22:56 +00:00
|
|
|
headerLeft: () => <CloseModalButton testID='close-attachment-view' navigation={navigation} buttonStyle={{ color: themes[theme].previewTintColor }} />,
|
2020-06-15 14:00:46 +00:00
|
|
|
title: decodeURI(title),
|
2020-06-26 20:22:56 +00:00
|
|
|
headerRight: () => <SaveButton testID='save-image' onPress={this.handleSave} buttonStyle={{ color: themes[theme].previewTintColor }} />,
|
|
|
|
headerBackground: () => <View style={{ flex: 1, backgroundColor: themes[theme].previewBackground }} />,
|
|
|
|
headerTintColor: themes[theme].previewTintColor,
|
|
|
|
headerTitleStyle: { color: themes[theme].previewTintColor }
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
|
|
|
navigation.setOptions(options);
|
|
|
|
}
|
|
|
|
|
2020-02-28 16:18:45 +00:00
|
|
|
getVideoRef = ref => this.videoRef = ref;
|
|
|
|
|
2019-12-18 21:13:11 +00:00
|
|
|
handleSave = async() => {
|
|
|
|
const { attachment } = this.state;
|
|
|
|
const { user, baseUrl } = this.props;
|
2020-04-30 18:08:48 +00:00
|
|
|
const {
|
|
|
|
image_url, image_type, video_url, video_type
|
|
|
|
} = attachment;
|
|
|
|
const url = image_url || video_url;
|
|
|
|
const mediaAttachment = formatAttachmentUrl(url, user.id, user.token, baseUrl);
|
2019-12-18 21:13:11 +00:00
|
|
|
|
|
|
|
if (isAndroid) {
|
|
|
|
const rationale = {
|
|
|
|
title: I18n.t('Write_External_Permission'),
|
|
|
|
message: I18n.t('Write_External_Permission_Message')
|
|
|
|
};
|
|
|
|
const result = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, rationale);
|
|
|
|
if (!(result || result === PermissionsAndroid.RESULTS.GRANTED)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
try {
|
2020-04-30 18:08:48 +00:00
|
|
|
const extension = image_url ? `.${ mime.extension(image_type) || 'jpg' }` : `.${ mime.extension(video_type) || 'mp4' }`;
|
|
|
|
const file = `${ FileSystem.documentDirectory + SHA256(url) + extension }`;
|
|
|
|
const { uri } = await FileSystem.downloadAsync(mediaAttachment, file);
|
2019-12-18 21:13:11 +00:00
|
|
|
await CameraRoll.save(uri, { album: 'Rocket.Chat' });
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
|
|
|
|
} catch (e) {
|
2020-04-30 18:08:48 +00:00
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t(image_url ? 'error-save-image' : 'error-save-video') });
|
2019-12-18 21:13:11 +00:00
|
|
|
}
|
|
|
|
this.setState({ loading: false });
|
|
|
|
};
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
renderImage = (uri) => {
|
|
|
|
const {
|
|
|
|
theme, width, height, insets
|
|
|
|
} = this.props;
|
|
|
|
const headerHeight = getHeaderHeight(width > height);
|
|
|
|
return (
|
|
|
|
<ImageViewer
|
|
|
|
uri={uri}
|
|
|
|
onLoadEnd={() => this.setState({ loading: false })}
|
|
|
|
theme={theme}
|
|
|
|
width={width}
|
|
|
|
height={height - insets.top - insets.bottom - headerHeight}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-12-18 21:13:11 +00:00
|
|
|
|
|
|
|
renderVideo = uri => (
|
|
|
|
<Video
|
|
|
|
source={{ uri }}
|
|
|
|
rate={1.0}
|
|
|
|
volume={1.0}
|
|
|
|
isMuted={false}
|
|
|
|
resizeMode={Video.RESIZE_MODE_CONTAIN}
|
|
|
|
shouldPlay
|
|
|
|
isLooping={false}
|
|
|
|
style={styles.container}
|
|
|
|
useNativeControls
|
|
|
|
onLoad={() => this.setState({ loading: false })}
|
|
|
|
onError={console.log}
|
2020-02-28 16:18:45 +00:00
|
|
|
ref={this.getVideoRef}
|
2019-12-18 21:13:11 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { loading, attachment } = this.state;
|
|
|
|
const { theme, user, baseUrl } = this.props;
|
|
|
|
let content = null;
|
|
|
|
|
|
|
|
if (attachment && attachment.image_url) {
|
|
|
|
const uri = formatAttachmentUrl(attachment.image_url, user.id, user.token, baseUrl);
|
|
|
|
content = this.renderImage(encodeURI(uri));
|
|
|
|
} else if (attachment && attachment.video_url) {
|
|
|
|
const uri = formatAttachmentUrl(attachment.video_url, user.id, user.token, baseUrl);
|
|
|
|
content = this.renderVideo(encodeURI(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].backgroundColor }]}>
|
2020-06-26 20:22:56 +00:00
|
|
|
<StatusBar barStyle='light-content' backgroundColor={themes[theme].previewBackground} />
|
2019-12-18 21:13:11 +00:00
|
|
|
{content}
|
|
|
|
{loading ? <RCActivityIndicator absolute size='large' theme={theme} /> : null}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state)
|
2019-12-18 21:13:11 +00:00
|
|
|
});
|
|
|
|
|
2020-06-26 20:22:56 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(withDimensions(withSafeAreaInsets(AttachmentView))));
|