Small cleanup on sendFileMessage

This commit is contained in:
Diego Mello 2024-05-17 14:31:06 -03:00
parent e540cce98c
commit 2b18d276c4
1 changed files with 10 additions and 57 deletions

View File

@ -105,47 +105,6 @@ export function sendFileMessage(
const encryptedFileInfo = await Encryption.encryptFile(rid, fileInfo); const encryptedFileInfo = await Encryption.encryptFile(rid, fileInfo);
const { encryptedFile, getContent } = encryptedFileInfo; const { encryptedFile, getContent } = encryptedFileInfo;
const formData: IFileUpload[] = [];
formData.push({
name: 'file',
type: 'file',
filename: sha256(fileInfo.name || 'fileMessage'),
uri: encryptedFile
});
if (fileInfo.description) {
formData.push({
name: 'description',
data: encryptedFileInfo.description
});
}
if (fileInfo.msg) {
formData.push({
name: 'msg',
data: fileInfo.msg
});
}
if (tmid) {
formData.push({
name: 'tmid',
data: tmid
});
}
// const { version: serverVersion } = store.getState().server;
// if (encryptedFileInfo.t === E2E_MESSAGE_TYPE && compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '6.8.0')) {
formData.push({
name: 't',
data: 'e2e'
});
// formData.push({
// name: 'e2e',
// data: encryptedFileInfo.e2e
// });
// }
const headers = { const headers = {
...RocketChatSettings.customHeaders, ...RocketChatSettings.customHeaders,
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
@ -154,34 +113,28 @@ export function sendFileMessage(
}; };
try { try {
const data = formData.map(item => { const data = [
if (item.uri) { {
return { name: 'file',
name: item.name, type: 'file',
type: item.type, filename: sha256(fileInfo.name || 'fileMessage'),
filename: item.filename, data: RNFetchBlob.wrap(decodeURI(encryptedFile))
data: RNFetchBlob.wrap(decodeURI(item.uri))
};
} }
return item; ];
});
const response = await RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data); const response = await RNFetchBlob.fetch('POST', `${server}/api/v1/rooms.media/${rid}`, headers, data);
const json = response.json(); const json = response.json();
let content; let content;
if (getContent) { if (getContent) {
content = await getContent(json.file._id, json.file.url); content = await getContent(json.file._id, json.file.url);
} }
const mediaConfirm = await fetch(`${server}/api/v1/rooms.mediaConfirm/${rid}/${json.file._id}`, { const mediaConfirm = await fetch(`${server}/api/v1/rooms.mediaConfirm/${rid}/${json.file._id}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', ...headers,
'X-Auth-Token': token, 'Content-Type': 'application/json'
'X-User-Id': id
}, },
body: JSON.stringify({ body: JSON.stringify({
msg: '', // msg: '', TODO: backwards compatibility
tmid, tmid,
description: fileInfo.description, description: fileInfo.description,
t: 'e2e', t: 'e2e',