add the possibility to start downloading a video, then exit the room, back again to room and cancel the video previously downloading
This commit is contained in:
parent
6c49c57046
commit
acb280fa36
|
@ -21,6 +21,7 @@ import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentU
|
||||||
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
|
import { MediaTypes, downloadMediaFile, searchMediaFileAsync } from '../../lib/methods/handleMediaDownload';
|
||||||
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
import { isAutoDownloadEnabled } from './helpers/mediaDownload/autoDownloadPreference';
|
||||||
import sharedStyles from '../../views/Styles';
|
import sharedStyles from '../../views/Styles';
|
||||||
|
import userPreferences from '../../lib/methods/userPreferences';
|
||||||
|
|
||||||
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
|
const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
|
||||||
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;
|
const isTypeSupported = (type: string) => SUPPORTED_TYPES.indexOf(type) !== -1;
|
||||||
|
@ -69,6 +70,8 @@ const DownloadIndicator = ({ handleCancelDownload }: { handleCancelDownload(): v
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const downloadResumableKey = (video: string) => `DownloadResumable${video}`;
|
||||||
|
|
||||||
const Video = React.memo(
|
const Video = React.memo(
|
||||||
({ file, showAttachment, getCustomEmoji, style, isReply, messageId }: IMessageVideo) => {
|
({ file, showAttachment, getCustomEmoji, style, isReply, messageId }: IMessageVideo) => {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
@ -81,20 +84,21 @@ const Video = React.memo(
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const handleAutoDownload = async () => {
|
const handleAutoDownload = async () => {
|
||||||
if (video) {
|
if (video) {
|
||||||
const searchImageBestQuality = await searchMediaFileAsync({
|
const searchVideoCached = await searchMediaFileAsync({
|
||||||
type: MediaTypes.video,
|
type: MediaTypes.video,
|
||||||
mimeType: file.video_type,
|
mimeType: file.video_type,
|
||||||
messageId
|
messageId
|
||||||
});
|
});
|
||||||
filePath.current = searchImageBestQuality.filePath;
|
filePath.current = searchVideoCached.filePath;
|
||||||
if (searchImageBestQuality.file?.exists) {
|
handleDownloadResumableSnapshot();
|
||||||
file.video_url = searchImageBestQuality.file.uri;
|
if (searchVideoCached.file?.exists) {
|
||||||
|
file.video_url = searchVideoCached.file.uri;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't pass the author to avoid auto-download what the user sent
|
// We don't pass the author to avoid auto-download what the user sent
|
||||||
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user });
|
const autoDownload = await isAutoDownloadEnabled('imagesPreferenceDownload', { user });
|
||||||
if (autoDownload) {
|
if (autoDownload && !downloadResumable.current) {
|
||||||
await handleDownload();
|
await handleDownload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,14 +110,33 @@ const Video = React.memo(
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleDownloadResumableSnapshot = () => {
|
||||||
|
if (video) {
|
||||||
|
const result = userPreferences.getString(downloadResumableKey(video));
|
||||||
|
if (result) {
|
||||||
|
const snapshot = JSON.parse(result);
|
||||||
|
downloadResumable.current = new FileSystem.DownloadResumable(
|
||||||
|
video,
|
||||||
|
filePath.current,
|
||||||
|
{},
|
||||||
|
() => {},
|
||||||
|
snapshot.resumeData
|
||||||
|
);
|
||||||
|
setLoading(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleDownload = async () => {
|
const handleDownload = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
downloadResumable.current = FileSystem.createDownloadResumable(video, filePath.current);
|
downloadResumable.current = FileSystem.createDownloadResumable(video, filePath.current);
|
||||||
|
userPreferences.setString(downloadResumableKey(video), JSON.stringify(downloadResumable.current.savable()));
|
||||||
const videoUri = await downloadMediaFile({
|
const videoUri = await downloadMediaFile({
|
||||||
url: video,
|
url: video,
|
||||||
filePath: filePath.current,
|
filePath: filePath.current,
|
||||||
downloadResumable: downloadResumable.current
|
downloadResumable: downloadResumable.current
|
||||||
});
|
});
|
||||||
|
userPreferences.removeItem(downloadResumableKey(video));
|
||||||
if (videoUri) {
|
if (videoUri) {
|
||||||
file.video_url = videoUri;
|
file.video_url = videoUri;
|
||||||
}
|
}
|
||||||
|
@ -136,7 +159,9 @@ const Video = React.memo(
|
||||||
|
|
||||||
const handleCancelDownload = () => {
|
const handleCancelDownload = () => {
|
||||||
if (loading && downloadResumable.current) {
|
if (loading && downloadResumable.current) {
|
||||||
return downloadResumable.current.cancelAsync();
|
downloadResumable.current.cancelAsync();
|
||||||
|
userPreferences.removeItem(downloadResumableKey(video));
|
||||||
|
return setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue