2023-05-15 17:16:08 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
import * as mime from 'react-native-mime-types';
|
|
|
|
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
|
2023-05-18 22:03:22 +00:00
|
|
|
export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`;
|
|
|
|
|
2023-05-18 21:36:58 +00:00
|
|
|
export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`;
|
|
|
|
|
2023-05-15 17:16:08 +00:00
|
|
|
const sanitizeString = (value: string) => sanitizeLikeString(value.substring(value.lastIndexOf('/') + 1));
|
|
|
|
|
|
|
|
const getExtension = (type: MediaTypes, mimeType?: string) => {
|
|
|
|
if (!mimeType) {
|
|
|
|
return defaultType[type];
|
|
|
|
}
|
|
|
|
// The library is returning mpag instead of mp3 for audio/mpeg
|
|
|
|
const extensionFromMime = mimeType === 'audio/mpeg' ? 'mp3' : mime.extension(mimeType);
|
|
|
|
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);
|
2023-05-18 21:36:58 +00:00
|
|
|
const folderPath = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;
|
2023-05-15 17:16:08 +00:00
|
|
|
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-15 17:20:31 +00:00
|
|
|
export const downloadMediaFile = async ({
|
|
|
|
url,
|
|
|
|
filePath,
|
|
|
|
downloadResumable
|
|
|
|
}: {
|
|
|
|
url: string;
|
|
|
|
filePath: string;
|
|
|
|
downloadResumable?: FileSystem.DownloadResumable;
|
|
|
|
}) => {
|
2023-05-15 17:16:08 +00:00
|
|
|
let uri = '';
|
|
|
|
try {
|
|
|
|
if (downloadResumable) {
|
|
|
|
const downloadFile = await downloadResumable.downloadAsync();
|
|
|
|
uri = downloadFile?.uri || '';
|
|
|
|
} else {
|
|
|
|
const downloadedFile = await FileSystem.downloadAsync(url, filePath);
|
|
|
|
uri = downloadedFile.uri;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
}
|
|
|
|
return uri;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteAllSpecificMediaFiles = async (type: MediaTypes, serverUrl: string): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const serverUrlParsed = sanitizeString(serverUrl);
|
2023-05-18 21:36:58 +00:00
|
|
|
const path = `${LOCAL_DOCUMENT_PATH}${typeString[type]}${serverUrlParsed}`;
|
2023-05-15 17:16:08 +00:00
|
|
|
await FileSystem.deleteAsync(path, { idempotent: true });
|
|
|
|
} catch (error) {
|
|
|
|
log(error);
|
|
|
|
}
|
|
|
|
};
|