Skip to content

Commit

Permalink
[FEATURE] Save video (#2063)
Browse files Browse the repository at this point in the history
* added-feature-save-video

* fix sha256

Co-authored-by: Djorkaeff Alexandre <djorkaeff.unb@gmail.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
  • Loading branch information
3 people authored Apr 30, 2020
1 parent 3f79ab7 commit 25b5c95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
'error-email-domain-blacklisted': 'The email domain is blacklisted',
'error-email-send-failed': 'Error trying to send email: {{message}}',
'error-save-image': 'Error while saving image',
'error-save-video': 'Error while saving video',
'error-field-unavailable': '{{field}} is already in use :(',
'error-file-too-large': 'File is too large',
'error-importer-not-defined': 'The importer was not defined correctly, it is missing the Import class.',
Expand Down
19 changes: 11 additions & 8 deletions app/views/AttachmentView.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class AttachmentView extends React.Component {
const attachment = navigation.getParam('attachment');
const from = navigation.getParam('from');
const handleSave = navigation.getParam('handleSave', () => {});
const { title, video_url } = attachment;
const { title } = attachment;
const options = {
title,
...themedHeader(theme),
headerRight: !video_url ? <SaveButton testID='save-image' onPress={handleSave} /> : null
headerRight: <SaveButton testID='save-image' onPress={handleSave} />
};
if (from !== 'MessagesView') {
options.gesturesEnabled = false;
Expand Down Expand Up @@ -84,8 +84,11 @@ class AttachmentView extends React.Component {
handleSave = async() => {
const { attachment } = this.state;
const { user, baseUrl } = this.props;
const { image_url, image_type } = attachment;
const img = formatAttachmentUrl(image_url, user.id, user.token, baseUrl);
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);

if (isAndroid) {
const rationale = {
Expand All @@ -100,13 +103,13 @@ class AttachmentView extends React.Component {

this.setState({ loading: true });
try {
const extension = `.${ mime.extension(image_type) || 'jpg' }`;
const file = `${ FileSystem.documentDirectory + SHA256(image_url) + extension }`;
const { uri } = await FileSystem.downloadAsync(img, file);
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);
await CameraRoll.save(uri, { album: 'Rocket.Chat' });
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
} catch (e) {
EventEmitter.emit(LISTENER, { message: I18n.t('error-save-image') });
EventEmitter.emit(LISTENER, { message: I18n.t(image_url ? 'error-save-image' : 'error-save-video') });
}
this.setState({ loading: false });
};
Expand Down

0 comments on commit 25b5c95

Please sign in to comment.