mattermost-mobile-upstream/app/actions/remote/file.ts
Scott Bishel c05aea4a2b
Some checks are pending
ci / lint-typecheck (push) Waiting to run
ci / detox-lint-typecheck (push) Waiting to run
ci / test (push) Waiting to run
MM-69122: Add file element support to interactive dialogs (#9898)
* Implement Interactive Dialog - file upload

* update demo plugin version for e2etests

* fix and add e2etests

* review fixes
2026-10-07 14:43:26 -05:00

122 lines
4.5 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {DOWNLOAD_TIMEOUT} from '@constants/network';
import NetworkManager from '@managers/network_manager';
import {getFullErrorMessage} from '@utils/errors';
import {logDebug, logError} from '@utils/log';
import {forceLogoutIfNecessary} from './session';
import type {Client} from '@client/rest';
import type {ClientResponse, ClientResponseError} from '@mattermost/react-native-network-client';
export const downloadFile = (serverUrl: string, fileId: string, desitnation: string) => { // Let it throw and handle it accordingly
const client = NetworkManager.getClient(serverUrl);
return client.apiClient.download(client.getFileRoute(fileId), desitnation.replace('file://', ''), {timeoutInterval: DOWNLOAD_TIMEOUT});
};
export const downloadProfileImage = (serverUrl: string, userId: string, lastPictureUpdate: number, destination: string) => { // Let it throw and handle it accordingly
const client = NetworkManager.getClient(serverUrl);
return client.apiClient.download(client.getProfilePictureUrl(userId, lastPictureUpdate), destination.replace('file://', ''), {timeoutInterval: DOWNLOAD_TIMEOUT});
};
export const uploadFile = (
serverUrl: string,
file: FileInfo | ExtractedFileInfo,
channelId: string,
onProgress: (fractionCompleted: number, bytesRead?: number | null | undefined) => void = () => {/*Do Nothing*/},
onComplete: (response: ClientResponse) => void = () => {/*Do Nothing*/},
onError: (response: ClientResponseError) => void = () => {/*Do Nothing*/},
skipBytes = 0,
isBookmark = false,
) => {
try {
const client = NetworkManager.getClient(serverUrl);
return {cancel: client.uploadAttachment(file, channelId, onProgress, onComplete, onError, skipBytes, isBookmark)};
} catch (error) {
logDebug('error on uploadFile', getFullErrorMessage(error));
return {error};
}
};
export const fetchFilesInfo = async (serverUrl: string, fileIds: string[]): Promise<{files: FileInfo[]; error?: unknown}> => {
try {
const client = NetworkManager.getClient(serverUrl);
// Fetch each file independently so a single missing/deleted file
// (e.g. one cleared server-side) doesn't drop the rest.
const results = await Promise.allSettled(fileIds.map((id) => client.getFileInfo(id)));
const files: FileInfo[] = [];
results.forEach((result) => {
if (result.status === 'fulfilled' && result.value) {
files.push(result.value);
} else if (result.status === 'rejected') {
logDebug('error on fetchFilesInfo', getFullErrorMessage(result.reason));
}
});
return {files};
} catch (error) {
// Return error so callers can distinguish a hard failure from "all ids were 404d".
logError('error on fetchFilesInfo', getFullErrorMessage(error));
return {files: [], error};
}
};
export const fetchPublicLink = async (serverUrl: string, fileId: string) => {
try {
const client = NetworkManager.getClient(serverUrl);
const publicLink = await client!.getFilePublicLink(fileId);
return publicLink;
} catch (error) {
logDebug('error on fetchPublicLink', getFullErrorMessage(error));
forceLogoutIfNecessary(serverUrl, error);
return {error};
}
};
export const buildFileUrl = (serverUrl: string, fileId: string, timestamp = 0) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return '';
}
return client.getFileUrl(fileId, timestamp);
};
export const buildAbsoluteUrl = (serverUrl: string, relativePath: string) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return '';
}
return client.getAbsoluteUrl(relativePath);
};
export const buildFilePreviewUrl = (serverUrl: string, fileId: string, timestamp = 0) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return '';
}
return client.getFilePreviewUrl(fileId, timestamp);
};
export const buildFileThumbnailUrl = (serverUrl: string, fileId: string, timestamp = 0) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return '';
}
return client.getFileThumbnailUrl(fileId, timestamp);
};