Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove rn-fetch-blob #5669

Merged
merged 4 commits into from
May 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.RNFetchBlob.RNFetchBlob;

import com.reactnativecommunity.webview.RNCWebViewManager;

import com.dylanvann.fastimage.FastImageOkHttpUrlLoader;
Expand Down Expand Up @@ -104,8 +102,6 @@ public void setCertificate(String data, Promise promise) {
WebSocketModule.setCustomClientBuilder(new CustomClient());
// Image networking react-native layer
ReactOkHttpNetworkFetcher.setOkHttpClient(getOkHttpClient());
// RNFetchBlob networking layer
RNFetchBlob.applyCustomOkHttpClient(getOkHttpClient());
// RNCWebView onReceivedClientCertRequest
RNCWebViewManager.setCertificateAlias(data);
// FastImage Glide network layer
Expand Down
5 changes: 3 additions & 2 deletions app/containers/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const styles = StyleSheet.create({
},
text: {
fontSize: 14,
...sharedStyles.textRegular,
...sharedStyles.textAlignCenter
// jest error: TypeError: Cannot read property 'textRegular' of undefined
...sharedStyles?.textRegular,
...sharedStyles?.textAlignCenter
}
});

Expand Down
20 changes: 10 additions & 10 deletions app/containers/message/Reply.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { dequal } from 'dequal';
import moment from 'moment';
import React, { useContext, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import moment from 'moment';
import { dequal } from 'dequal';
import FastImage from 'react-native-fast-image';

import Touchable from './Touchable';
import Markdown from '../markdown';
import { IAttachment, TGetCustomEmoji } from '../../definitions';
import { themes } from '../../lib/constants';
import { fileDownloadAndPreview } from '../../lib/methods/helpers';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import openLink from '../../lib/methods/helpers/openLink';
import { TSupportedThemes, useTheme } from '../../theme';
import sharedStyles from '../../views/Styles';
import { themes } from '../../lib/constants';
import MessageContext from './Context';
import { fileDownloadAndPreview } from './helpers/fileDownload';
import { IAttachment, TGetCustomEmoji } from '../../definitions';
import RCActivityIndicator from '../ActivityIndicator';
import Markdown from '../markdown';
import Attachments from './Attachments';
import { TSupportedThemes, useTheme } from '../../theme';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import MessageContext from './Context';
import Touchable from './Touchable';
import messageStyles from './styles';

const styles = StyleSheet.create({
Expand Down
3 changes: 1 addition & 2 deletions app/containers/message/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isDownloadActive,
resumeMediaFile
} from '../../lib/methods/handleMediaDownload';
import { isIOS } from '../../lib/methods/helpers';
import { fileDownload, isIOS } from '../../lib/methods/helpers';
import EventEmitter from '../../lib/methods/helpers/events';
import { formatAttachmentUrl } from '../../lib/methods/helpers/formatAttachmentUrl';
import { useTheme } from '../../theme';
Expand All @@ -24,7 +24,6 @@ import Markdown from '../markdown';
import BlurComponent from './Components/OverlayComponent';
import MessageContext from './Context';
import Touchable from './Touchable';
import { fileDownload } from './helpers/fileDownload';
import { DEFAULT_MESSAGE_HEIGHT } from './utils';

const SUPPORTED_TYPES = ['video/quicktime', 'video/mp4', ...(isIOS ? [] : ['video/3gp', 'video/mkv'])];
Expand Down
53 changes: 0 additions & 53 deletions app/containers/message/helpers/fileDownload/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export * from './environment';
export * from './keys';
export * from './links';
export * from './localAuthentication';
export * from './localPath';
export * from './messagesStatus';
export * from './messageTypeLoad';
export * from './notifications';
Expand Down
4 changes: 0 additions & 4 deletions app/lib/constants/localPath.ts

This file was deleted.

17 changes: 8 additions & 9 deletions app/lib/methods/getServerInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import RNFetchBlob from 'rn-fetch-blob';
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
import { KJUR } from 'jsrsasign';
import moment from 'moment';
Expand Down Expand Up @@ -45,20 +44,20 @@ const verifyJWT = (jwt?: string): ISupportedVersionsData | null => {

export async function getServerInfo(server: string): Promise<TServerInfoResult> {
try {
const response = await RNFetchBlob.fetch('GET', `${server}/api/info`, {
const response = await fetch(`${server}/api/info`, {
...RocketChatSettings.customHeaders
});
try {
const jsonRes: IApiServerInfo = response.json();
if (!jsonRes?.success) {
const serverInfo: IApiServerInfo = await response.json();
if (!serverInfo?.success) {
return {
success: false,
message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') })
};
}

// Makes use of signed JWT to get supported versions
const supportedVersions = verifyJWT(jsonRes.supportedVersions?.signed);
const supportedVersions = verifyJWT(serverInfo.supportedVersions?.signed);

// if backend doesn't have supported versions or JWT is invalid, request from cloud
if (!supportedVersions) {
Expand All @@ -69,7 +68,7 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
moment(new Date()).diff(serverRecord?.supportedVersionsUpdatedAt, 'hours') <= SV_CLOUD_UPDATE_INTERVAL
) {
return {
...jsonRes,
...serverInfo,
success: true
};
}
Expand All @@ -79,7 +78,7 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
// Allows airgapped servers to use the app until enforcementStartDate
if (!cloudInfo) {
return {
...jsonRes,
...serverInfo,
success: true
};
}
Expand All @@ -88,14 +87,14 @@ export async function getServerInfo(server: string): Promise<TServerInfoResult>
const supportedVersionsCloud = verifyJWT(cloudInfo?.signed);

return {
...jsonRes,
...serverInfo,
success: true,
supportedVersions: supportedVersionsCloud
};
}

return {
...jsonRes,
...serverInfo,
success: true,
supportedVersions
};
Expand Down
33 changes: 33 additions & 0 deletions app/lib/methods/helpers/fileDownload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as FileSystem from 'expo-file-system';
import FileViewer from 'react-native-file-viewer';

import { LISTENER } from '../../../containers/Toast';
import { IAttachment } from '../../../definitions';
import i18n from '../../../i18n';
import EventEmitter from './events';

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}${fileName}`;
}
if (attachment) {
path = `${path}${attachment.title}`;
}
const file = await FileSystem.downloadAsync(url, path);
return file.uri;
};

export const fileDownloadAndPreview = async (url: string, attachment: IAttachment): Promise<void> => {
try {
const file = await fileDownload(url, attachment);
FileViewer.open(file, {
showOpenWithDialog: true,
showAppsSuggestions: true
});
} catch (e) {
EventEmitter.emit(LISTENER, { message: i18n.t('Error_Download_file') });
}
};
66 changes: 66 additions & 0 deletions app/lib/methods/helpers/fileUpload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export interface IFileUpload {
name: string;
uri?: string;
type?: string;
filename?: string;
data?: any;
}

export class Upload {
public xhr: XMLHttpRequest;
public formData: FormData;

constructor() {
this.xhr = new XMLHttpRequest();
this.formData = new FormData();
}

public setupRequest(url: string, headers: { [key: string]: string }): void {
this.xhr.open('POST', url);
Object.keys(headers).forEach(key => {
this.xhr.setRequestHeader(key, headers[key]);
});
}

public appendFile(item: IFileUpload): void {
if (item.uri) {
this.formData.append(item.name, {
uri: item.uri,
type: item.type,
name: item.filename
} as any);
} else {
this.formData.append(item.name, item.data);
}
}

public then(callback: (param: { respInfo: XMLHttpRequest }) => void): void {
this.xhr.onload = () => callback({ respInfo: this.xhr });
this.xhr.send(this.formData);
}

public catch(callback: ((this: XMLHttpRequest, ev: ProgressEvent<EventTarget>) => any) | null): void {
this.xhr.onerror = callback;
}

public uploadProgress(callback: (param: number, arg1: number) => any): void {
this.xhr.upload.onprogress = ({ total, loaded }) => callback(loaded, total);
}

public cancel(): Promise<void> {
this.xhr.abort();
return Promise.resolve();
}
}

class FileUpload {
public uploadFile(url: string, headers: { [x: string]: string }, data: IFileUpload[]) {
const upload = new Upload();
upload.setupRequest(url, headers);
data.forEach(item => upload.appendFile(item));
return upload;
}
}

const fileUpload = new FileUpload();
export default fileUpload;
60 changes: 0 additions & 60 deletions app/lib/methods/helpers/fileUpload/index.ios.ts

This file was deleted.

25 changes: 0 additions & 25 deletions app/lib/methods/helpers/fileUpload/index.ts

This file was deleted.

Loading