From 55c879abc2812d77d21dc755a4245440c834f55b Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 13 Apr 2023 18:52:56 -0700 Subject: [PATCH] Always store response on filesystem --- src-tauri/src/main.rs | 36 +++++++++++++++------------- src-web/hooks/useResponseBodyBlob.ts | 6 ++--- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index ecaefff4..646edb01 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -224,25 +224,27 @@ async fn actually_send_ephemeral_request( response.url = v.url().to_string(); let body_bytes = v.bytes().await.expect("Failed to get body").to_vec(); response.content_length = Some(body_bytes.len() as i64); - if body_bytes.len() > 1000 { - let dir = app_handle.path_resolver().app_data_dir().unwrap(); - let body_path = dir.join(response.id.clone()); - let mut f = File::options() - .create(true) - .write(true) - .open(&body_path) - .expect("Failed to open file"); - f.write_all(body_bytes.as_slice()) - .expect("Failed to write to file"); - response.body_path = Some( - body_path - .to_str() - .expect("Failed to get body path") - .to_string(), - ); - } else { + let dir = app_handle.path_resolver().app_data_dir().unwrap(); + let body_path = dir.join(response.id.clone()); + let mut f = File::options() + .create(true) + .write(true) + .open(&body_path) + .expect("Failed to open file"); + f.write_all(body_bytes.as_slice()) + .expect("Failed to write to file"); + + // Also story body directly on the model, if small enough + if body_bytes.len() < 100_000 { response.body = Some(body_bytes); } + + response.body_path = Some( + body_path + .to_str() + .expect("Failed to get body path") + .to_string(), + ); response.elapsed = start.elapsed().as_millis() as i64; response = models::update_response_if_id(&response, pool) .await diff --git a/src-web/hooks/useResponseBodyBlob.ts b/src-web/hooks/useResponseBodyBlob.ts index 9cd1abd2..1c203b6c 100644 --- a/src-web/hooks/useResponseBodyBlob.ts +++ b/src-web/hooks/useResponseBodyBlob.ts @@ -2,16 +2,16 @@ import { useQuery } from '@tanstack/react-query'; import { readBinaryFile } from '@tauri-apps/api/fs'; import type { HttpResponse } from '../lib/models'; -export function useResponseBodyBlob(response: HttpResponse | null) { +export function useResponseBodyBlob(response: HttpResponse) { return useQuery({ enabled: response != null, queryKey: ['response-body-binary', response?.updatedAt], initialData: null, queryFn: async () => { - if (response?.body) { + if (response.body) { return Uint8Array.of(...response.body); } - if (response?.bodyPath) { + if (response.bodyPath) { return readBinaryFile(response.bodyPath); } return null;