Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/containers/message/hooks/useMediaAutoDownload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getFileType = (file: IAttachment): MediaTypes | null => {
}
if (file.audio_url) {
return 'audio';
}
}
return null;
};

Expand Down Expand Up @@ -109,7 +109,9 @@ export const useMediaAutoDownload = ({

const tryAutoDownload = async () => {
const isCurrentUserAuthor = author?._id === user.id;
const isAutoDownloadEnabled = fetchAutoDownloadEnabled(`${fileType}PreferenceDownload`);
// Map 'other' file type to 'image' for preference purposes
const preferenceType = fileType === 'other' ? 'image' : fileType;
const isAutoDownloadEnabled = fetchAutoDownloadEnabled(`${preferenceType}PreferenceDownload`);
if (isAutoDownloadEnabled || isCurrentUserAuthor) {
await download();
} else {
Expand Down
4 changes: 3 additions & 1 deletion app/definitions/IAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export interface IAttachment {
translations?: IAttachmentTranslations;
e2e?: E2EType;
encryption?: TAttachmentEncryption;
format?: string;
format?: string;
other_type?: string;
other_url?: string;
hashes?: {
sha256: string;
};
Expand Down
2 changes: 2 additions & 0 deletions app/lib/constants/mediaAutoDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export type MediaDownloadOption = 'never' | 'wifi_mobile_data' | 'wifi';
export const IMAGE_PREFERENCE_DOWNLOAD = 'imagePreferenceDownload';
export const VIDEO_PREFERENCE_DOWNLOAD = 'videoPreferenceDownload';
export const AUDIO_PREFERENCE_DOWNLOAD = 'audioPreferenceDownload';


23 changes: 20 additions & 3 deletions app/lib/methods/handleMediaDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import database from '../database';
import { getThreadById } from '../database/services/Thread';
import { headers } from './helpers/fetch';

export type MediaTypes = 'audio' | 'image' | 'video';
export type MediaTypes = 'audio' | 'image' | 'video' | 'other';
export type TDownloadState = 'to-download' | 'loading' | 'downloaded';
const defaultType = {
audio: 'mp3',
image: 'jpg',
video: 'mp4'
video: 'mp4',
other: 'bin'
};
export const LOCAL_DOCUMENT_DIRECTORY = FileSystem.documentDirectory;

Expand Down Expand Up @@ -68,11 +69,26 @@ export const getFilename = ({
return `${filenameFromUrl}.${extension}`;
};

const getExtension = (type: MediaTypes, mimeType?: string, url?: string) => {
export const getExtension = (type: MediaTypes, mimeType?: string, url?: string) => {
// support url with gif extension and mimetype undefined, ex.: using the app tenor and giphy.
if (url?.split('.').pop() === 'gif') {
return 'gif';
}

// support url with doc extension
if (url?.split('.').pop() === 'doc') {
return 'doc';
}

// support url with docx extension
if (url?.split('.').pop() === 'docx') {
return 'docx';
}

// support url with pdf extension
if (url?.split('.').pop() === 'pdf') {
return 'pdf';
}
if (!mimeType) {
return defaultType[type];
}
Expand All @@ -97,6 +113,7 @@ const getExtension = (type: MediaTypes, mimeType?: string, url?: string) => {
if (!extension) {
return defaultType[type];
}

return extension;
};

Expand Down
36 changes: 24 additions & 12 deletions app/lib/methods/helpers/fileDownload.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
import * as FileSystem from 'expo-file-system';

import FileViewer from 'react-native-file-viewer';

import { LISTENER } from '../../../containers/Toast';
import { type IAttachment } from '../../../definitions';
import i18n from '../../../i18n';
import EventEmitter from './events';
import { Encryption } from '../../encryption';
import { sanitizeFileName } from '../handleMediaDownload';
import { downloadMediaFile, getMediaCache } from '../handleMediaDownload';

export const getLocalFilePathFromFile = (localPath: string, attachment: IAttachment): string => `${localPath}${attachment.title}`;

export const fileDownload = async (url: string, attachment?: IAttachment, fileName?: string): Promise<string> => {
let path = `${FileSystem.documentDirectory}`;
if (fileName) {
path = `${path}${sanitizeFileName(fileName)}`;
}
if (attachment?.title) {
path = `${path}${sanitizeFileName(attachment.title)}`;
export const fileDownload = async (url: string, attachment?: IAttachment, fileName?: string, messageId?: string): Promise<string> => {
// let path = `${FileSystem.documentDirectory}`;
// if (fileName) {
// path = `${path}${sanitizeFileName(fileName)}`;
// }
// if (attachment?.title) {
// path = `${path}${sanitizeFileName(attachment.title)}`;
// }

const cache = await getMediaCache({ type: 'other' as const, mimeType: attachment?.format, urlToCache: url });

if (cache?.exists) {
return cache.uri;
}
const file = await FileSystem.downloadAsync(url, path);
return file.uri;
const option = {
messageId: messageId || url,
type: 'other' as const,
downloadUrl: url
};

const uri = await downloadMediaFile(option);
return uri;
};

export const fileDownloadAndPreview = async (url: string, attachment: IAttachment, messageId: string): Promise<void> => {
try {
let file = url;
// If url starts with file://, we assume it's a local file and we don't download/decrypt it
if (!file.startsWith('file://')) {
file = await fileDownload(file, attachment);
file = await fileDownload(file, attachment, undefined, messageId);

if (attachment.encryption) {
if (!attachment.hashes?.sha256) {
Expand Down