Skip to content

Commit

Permalink
Add util to download binary files (#19435)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxiadlovskii authored Jun 5, 2024
1 parent be2d686 commit 3e29130
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
27 changes: 27 additions & 0 deletions graylog2-web-interface/src/logic/rest/FetchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ export class Builder {
return this;
}

blobFile(body, mimeType) {
this.body = { body: maybeStringify(body), mimeType: 'application/json' };
this.accept = mimeType;

this.responseHandler = (resp: { ok: boolean, blob: () => Blob }) => {
if (resp.ok) {
reportServerSuccess();

return resp.blob();
}

throw resp;
};

this.errorHandler = (error: Response) => onServerError(error);

return this;
}

plaintext(body) {
this.body = { body, mimeType: 'text/plain' };
this.accept = 'application/json';
Expand Down Expand Up @@ -291,3 +310,11 @@ export function fetchFile(method, url, body, mimeType = 'text/csv') {

return queuePromiseIfNotLoggedin(promise)();
}

export function fetchBlobFile(method, url, body, mimeType = 'text/csv') {
const promise = () => new Builder(method, url)
.blobFile(body, mimeType)
.build();

return queuePromiseIfNotLoggedin(promise)();
}
18 changes: 12 additions & 6 deletions graylog2-web-interface/src/util/FileDownloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/

import { fetchFile } from 'logic/rest/FetchProvider';
import { fetchBlobFile, fetchFile } from 'logic/rest/FetchProvider';
import UserNotification from 'preflight/util/UserNotification';

export const createLinkAndDownload = (href: string, fileName: string) => {
Expand All @@ -40,8 +40,14 @@ export const downloadBLOB = (contents: BlobPart, metadata: { fileName: string, c
createLinkAndDownload(href, metadata.fileName);
};

export const fetchFileWithBlob = async <Body>(method: string, url: string, body: Body | undefined, mimeType: string, fileName: string) => fetchFile(method, url, body, mimeType)
.then((result) => downloadBLOB(result, { fileName, contentType: mimeType }))
.catch((errorThrown) => {
UserNotification.error(`Downloading failed with status: ${errorThrown}`, 'Unable to download');
});
const errorHandler = (errorThrown: Error) => {
UserNotification.error(`Downloading failed with status: ${errorThrown}`, 'Unable to download');
};

export const fetchTextFile = async <Body>(method: string, url: string, body: Body | undefined, mimeType: string, fileName: string) => fetchFile(method, url, body, mimeType)
.then((result: string) => downloadBLOB(result, { fileName, contentType: mimeType }))
.catch(errorHandler);

export const fetchBinaryFile = async <Body>(method: string, url: string, body: Body | undefined, mimeType: string, fileName: string) => fetchBlobFile(method, url, body, mimeType)
.then((result: Blob) => downloadBLOB(result, { fileName, contentType: mimeType }))
.catch(errorHandler);

0 comments on commit 3e29130

Please sign in to comment.