update camera-roll and save the file from local url

This commit is contained in:
Reinaldo Neto 2023-07-25 13:27:33 -03:00
parent 3e75699b0b
commit 3ada875384
4 changed files with 25 additions and 20 deletions

View File

@ -22,7 +22,7 @@ import EventEmitter from '../lib/methods/helpers/events';
import { getUserSelector } from '../selectors/login';
import { TNavigation } from '../stacks/stackType';
import { useTheme } from '../theme';
import { getFilename } from '../lib/methods/handleMediaDownload';
import { LOCAL_DOCUMENT_DIRECTORY, getFilename } from '../lib/methods/handleMediaDownload';
const RenderContent = ({
setLoading,
@ -144,13 +144,13 @@ const AttachmentView = (): React.ReactElement => {
const handleSave = async () => {
const { title_link, image_url, image_type, video_url, video_type } = attachment;
const url = title_link || image_url || video_url;
// When the attachment is a video, the video_url refers to local file and the title_link to the link
const url = video_url || title_link || image_url;
if (!url) {
return;
}
const mediaAttachment = formatAttachmentUrl(url, user.id, user.token, baseUrl);
if (isAndroid) {
const rationale = {
title: I18n.t('Write_External_Permission'),
@ -165,17 +165,22 @@ const AttachmentView = (): React.ReactElement => {
setLoading(true);
try {
let filename = '';
if (image_url) {
filename = getFilename({ title: attachment.title, type: 'image', mimeType: image_type, url });
if (LOCAL_DOCUMENT_DIRECTORY && url.startsWith(LOCAL_DOCUMENT_DIRECTORY)) {
await CameraRoll.save(url, { album: 'Rocket.Chat' });
} else {
filename = getFilename({ title: attachment.title, type: 'video', mimeType: video_type, url });
const mediaAttachment = formatAttachmentUrl(url, user.id, user.token, baseUrl);
let filename = '';
if (image_url) {
filename = getFilename({ title: attachment.title, type: 'image', mimeType: image_type, url });
} else {
filename = getFilename({ title: attachment.title, type: 'video', mimeType: video_type, url });
}
const documentDir = `${RNFetchBlob.fs.dirs.DocumentDir}/`;
const path = `${documentDir + filename}`;
const file = await RNFetchBlob.config({ path }).fetch('GET', mediaAttachment);
await CameraRoll.save(path, { album: 'Rocket.Chat' });
file.flush();
}
const documentDir = `${RNFetchBlob.fs.dirs.DocumentDir}/`;
const path = `${documentDir + filename}`;
const file = await RNFetchBlob.config({ path }).fetch('GET', mediaAttachment);
await CameraRoll.save(path, { album: 'Rocket.Chat' });
file.flush();
EventEmitter.emit(LISTENER, { message: I18n.t('saved_to_gallery') });
} catch (e) {
EventEmitter.emit(LISTENER, { message: I18n.t(image_url ? 'error-save-image' : 'error-save-video') });

View File

@ -385,7 +385,7 @@ PODS:
- React-Core
- react-native-blur (4.1.0):
- React-Core
- react-native-cameraroll (5.6.0):
- react-native-cameraroll (5.7.2):
- React-Core
- react-native-cookies (6.2.1):
- React-Core
@ -944,7 +944,7 @@ SPEC CHECKSUMS:
React-logger: 3f8ebad1be1bf3299d1ab6d7f971802d7395c7ef
react-native-background-timer: 17ea5e06803401a379ebf1f20505b793ac44d0fe
react-native-blur: ba2f37268542f8a26d809f48c5162705a3261fc6
react-native-cameraroll: 755bcc628148a90a7c9cf3f817a252be3a601bc5
react-native-cameraroll: 134805127580aed23403b8c2cb1548920dd77b3a
react-native-cookies: f54fcded06bb0cda05c11d86788020b43528a26c
react-native-document-picker: f5ec1a712ca2a975c233117f044817bb8393cad4
react-native-mmkv-storage: cfb6854594cfdc5f7383a9e464bb025417d1721c
@ -1003,4 +1003,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: da36ddb728f8698fcf3e8b17d853ccedc4f8a24c
COCOAPODS: 1.12.1
COCOAPODS: 1.11.3

View File

@ -43,7 +43,7 @@
"@hookform/resolvers": "^2.9.10",
"@nozbe/watermelondb": "^0.25.5",
"@react-native-async-storage/async-storage": "^1.17.11",
"@react-native-camera-roll/camera-roll": "^5.6.0",
"@react-native-camera-roll/camera-roll": "^5.7.2",
"@react-native-clipboard/clipboard": "^1.8.5",
"@react-native-community/art": "^1.2.0",
"@react-native-community/blur": "^4.1.0",

View File

@ -4628,10 +4628,10 @@
dependencies:
merge-options "^3.0.4"
"@react-native-camera-roll/camera-roll@^5.6.0":
version "5.6.0"
resolved "https://registry.yarnpkg.com/@react-native-camera-roll/camera-roll/-/camera-roll-5.6.0.tgz#385082d57d694f3fd5ae386f8b8ce24b0969c5f9"
integrity sha512-a/GYwnBTxj1yKWB9m/qy8GzjowSocML8NbLT81wdMh0JzZYXCLze51BR2cb8JNDgRPzA9xe7KpD3j9qQOSOjag==
"@react-native-camera-roll/camera-roll@^5.7.2":
version "5.7.2"
resolved "https://registry.yarnpkg.com/@react-native-camera-roll/camera-roll/-/camera-roll-5.7.2.tgz#db11525ae26c8a61630c424aebd323a7c784a921"
integrity sha512-s8VAUG1Kvi+tEJkLHObmOJdXAL/uclnXJ/IdnJtx2fCKiWA3Ho0ln9gDQqCYHHHHu+sXk7wovsH/I2/AYy0brg==
"@react-native-clipboard/clipboard@^1.8.5":
version "1.8.5"