Rocket.Chat.ReactNative/app/lib/methods/handleMediaDownload.ts

143 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as FileSystem from 'expo-file-system';
import * as mime from 'react-native-mime-types';
import { isEmpty } from 'lodash';
import { sanitizeLikeString } from '../database/utils';
import { store } from '../store/auxStore';
import log from './helpers/log';
export enum MediaTypes {
audio = 'audio',
image = 'image',
video = 'video'
}
const typeString = {
[MediaTypes.audio]: 'audios/',
[MediaTypes.image]: 'images/',
[MediaTypes.video]: 'videos/'
};
const defaultType = {
[MediaTypes.audio]: 'mp3',
[MediaTypes.image]: 'jpg',
[MediaTypes.video]: 'mp4'
};
const downloadQueue: { [index: string]: FileSystem.DownloadResumable } = {};
2023-05-18 22:03:22 +00:00
export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`;
export function isDownloadActive(mediaType: MediaTypes, messageId: string): boolean {
return !!downloadQueue[mediaDownloadKey(mediaType, messageId)];
}
export async function cancelDownload(mediaType: MediaTypes, messageId: string): Promise<void> {
const downloadKey = mediaDownloadKey(mediaType, messageId);
if (!isEmpty(downloadQueue[downloadKey])) {
try {
await downloadQueue[downloadKey].cancelAsync();
} catch {
// Do nothing
}
delete downloadQueue[downloadKey];
}
}
export function downloadMediaFile({
mediaType,
messageId,
downloadUrl,
path
}: {
mediaType: MediaTypes;
messageId: string;
downloadUrl: string;
path: string;
2023-05-19 14:35:36 +00:00
}): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const downloadKey = mediaDownloadKey(mediaType, messageId);
downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
const result = await downloadQueue[downloadKey].downloadAsync();
if (result?.uri) {
return resolve(result.uri);
}
2023-05-19 05:42:21 +00:00
reject();
} catch {
reject();
}
});
}
export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`;
const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1));
const getExtension = (type: MediaTypes, mimeType?: string) => {
if (!mimeType) {
return defaultType[type];
}
2023-05-19 05:42:21 +00:00
const extensionFromMime = () => {
// The library is returning mpag instead of mp3 for audio/mpeg
if (mimeType === 'audio/mpeg') {
return 'mp3';
}
2023-05-30 14:07:34 +00:00
// For older audios the server is returning the type audio/aac and we can't play it as mp3
if (mimeType === 'audio/aac') {
return 'm4a';
}
2023-05-19 05:42:21 +00:00
const extension = mime.extension(mimeType);
// The mime.extension can return false when there aren't any extension
if (!extension) {
return defaultType[type];
}
return extension;
};
return extensionFromMime();
};
const ensureDirAsync = async (dir: string, intermediates = true): Promise<void> => {
const info = await FileSystem.getInfoAsync(dir);
if (info.exists && info.isDirectory) {
return;
}
await FileSystem.makeDirectoryAsync(dir, { intermediates });
return ensureDirAsync(dir, intermediates);
};
export const searchMediaFileAsync = async ({
type,
mimeType,
messageId
}: {
type: MediaTypes;
mimeType?: string;
messageId: string;
}) => {
let file;
let filePath = '';
try {
const serverUrl = store.getState().server.server;
const serverUrlParsed = sanitizeString(serverUrl);
const folderPath = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;
const filename = `${messageId}.${getExtension(type, mimeType)}`;
filePath = `${folderPath}/${filename}`;
await ensureDirAsync(folderPath);
file = await FileSystem.getInfoAsync(filePath);
} catch (e) {
log(e);
}
return { file, filePath };
};
2023-05-30 14:07:34 +00:00
export const deleteMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => {
try {
const serverUrlParsed = sanitizeString(serverUrl);
const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;
await FileSystem.deleteAsync(path, { idempotent: true });
} catch (error) {
log(error);
}
};