Files
mattermost-mobile/app/actions/remote/file.ts
Elias Nahum 7aa5bd0611 Update Dependencies and bug fixes (#7000)
* update dependencies

* update dependencies

* feedback review

* update @mattermost/react-native-turbo-mailer
2023-01-24 09:14:23 +02:00

101 lines
3.4 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 {forceLogoutIfNecessary} from './session';
import type {Client} from '@client/rest';
import type ClientError from '@client/rest/error';
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,
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,
) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return {error: error as ClientError};
}
return {cancel: client.uploadPostAttachment(file, channelId, onProgress, onComplete, onError, skipBytes)};
};
export const fetchPublicLink = async (serverUrl: string, fileId: string) => {
let client: Client;
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
return {error: error as ClientError};
}
try {
const publicLink = await client!.getFilePublicLink(fileId);
return publicLink;
} catch (error) {
forceLogoutIfNecessary(serverUrl, error as ClientErrorProps);
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);
};