-
Notifications
You must be signed in to change notification settings - Fork 228
Add ZIP download endpoint #410
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
use anyhow::Error; | ||
use async_zip::tokio::write::ZipFileWriter; | ||
use async_zip::Compression; | ||
use async_zip::ZipEntryBuilder; | ||
use axum::body::Body; | ||
use axum::extract::Path; | ||
use axum::extract::State; | ||
use axum::http::header::{self, CONTENT_LENGTH, CONTENT_TYPE}; | ||
use axum::http::{HeaderValue, StatusCode}; | ||
use axum::response::{IntoResponse, Response}; | ||
use include_dir::{include_dir, Dir}; | ||
use log::error; | ||
use std::sync::Arc; | ||
use tokio::io::AsyncReadExt; | ||
use tokio::io::{copy, duplex, AsyncReadExt}; | ||
use tokio::sync::mpsc::Sender; | ||
use tokio::sync::RwLock; | ||
use tokio_util::compat::FuturesAsyncWriteCompatExt; | ||
use tokio_util::io::ReaderStream; | ||
|
||
use crate::analysis::{AnalysisCtrlMessage, AnalysisStatus}; | ||
use crate::pcap::generate_pcap_data; | ||
use crate::qmdl_store::RecordingStore; | ||
use crate::{display, DiagDeviceCtrlMessage}; | ||
|
||
|
@@ -76,3 +83,194 @@ pub async fn serve_static( | |
.unwrap(), | ||
} | ||
} | ||
|
||
pub async fn get_zip( | ||
State(state): State<Arc<ServerState>>, | ||
Path(entry_name): Path<String>, | ||
) -> Result<Response, (StatusCode, String)> { | ||
let qmdl_idx = entry_name.trim_end_matches(".zip").to_owned(); | ||
let (entry_index, qmdl_size_bytes) = { | ||
let qmdl_store = state.qmdl_store_lock.read().await; | ||
let (entry_index, entry) = qmdl_store.entry_for_name(&qmdl_idx).ok_or(( | ||
StatusCode::NOT_FOUND, | ||
format!("couldn't find entry with name {}", qmdl_idx), | ||
))?; | ||
|
||
if entry.qmdl_size_bytes == 0 { | ||
return Err(( | ||
StatusCode::SERVICE_UNAVAILABLE, | ||
"QMDL file is empty, try again in a bit!".to_string(), | ||
)); | ||
} | ||
|
||
(entry_index, entry.qmdl_size_bytes) | ||
}; | ||
|
||
let qmdl_store_lock = state.qmdl_store_lock.clone(); | ||
|
||
let (reader, writer) = duplex(8192); | ||
|
||
tokio::spawn(async move { | ||
let result: Result<(), Error> = async { | ||
let mut zip2 = ZipFileWriter::with_tokio(writer); | ||
|
||
// Add QMDL file | ||
{ | ||
let entry = | ||
ZipEntryBuilder::new(format!("{qmdl_idx}.qmdl").into(), Compression::Stored); | ||
let mut entry_writer = zip2.write_entry_stream(entry).await?.compat_write(); | ||
|
||
|
||
let mut qmdl_file = { | ||
let qmdl_store = qmdl_store_lock.read().await; | ||
qmdl_store | ||
.open_entry_qmdl(entry_index) | ||
.await? | ||
.take(qmdl_size_bytes as u64) | ||
}; | ||
|
||
copy(&mut qmdl_file, &mut entry_writer).await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooh |
||
entry_writer.into_inner().close().await?; | ||
} | ||
|
||
// Add PCAP file | ||
{ | ||
let entry = | ||
ZipEntryBuilder::new(format!("{qmdl_idx}.pcapng").into(), Compression::Stored); | ||
let mut entry_writer = zip2.write_entry_stream(entry).await?.compat_write(); | ||
|
||
let qmdl_file_for_pcap = { | ||
let qmdl_store = qmdl_store_lock.read().await; | ||
qmdl_store | ||
.open_entry_qmdl(entry_index) | ||
.await? | ||
.take(qmdl_size_bytes as u64) | ||
}; | ||
|
||
if let Err(e) = | ||
generate_pcap_data(&mut entry_writer, qmdl_file_for_pcap, qmdl_size_bytes).await | ||
{ | ||
// if we fail to generate the PCAP file, we should still continue and give the | ||
// user the QMDL. | ||
error!("Failed to generate PCAP: {:?}", e); | ||
} | ||
|
||
entry_writer.into_inner().close().await?; | ||
} | ||
|
||
zip2.close().await?; | ||
Ok(()) | ||
} | ||
.await; | ||
|
||
if let Err(e) = result { | ||
error!("Error generating ZIP file: {:?}", e); | ||
} | ||
}); | ||
|
||
let headers = [(CONTENT_TYPE, "application/zip")]; | ||
let body = Body::from_stream(ReaderStream::new(reader)); | ||
Ok((headers, body).into_response()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use async_zip::base::read::mem::ZipFileReader; | ||
use axum::extract::{Path, State}; | ||
use std::io::Cursor; | ||
use tempfile::TempDir; | ||
|
||
async fn create_test_qmdl_store() -> (TempDir, Arc<RwLock<crate::qmdl_store::RecordingStore>>) { | ||
let temp_dir = TempDir::new().unwrap(); | ||
let store_path = temp_dir.path().to_path_buf(); | ||
let store = crate::qmdl_store::RecordingStore::create(&store_path) | ||
.await | ||
.unwrap(); | ||
(temp_dir, Arc::new(RwLock::new(store))) | ||
} | ||
|
||
async fn create_test_entry_with_data( | ||
store_lock: &Arc<RwLock<crate::qmdl_store::RecordingStore>>, | ||
test_data: &[u8], | ||
) -> String { | ||
let entry_name = { | ||
let mut store = store_lock.write().await; | ||
let (mut qmdl_file, _analysis_file) = store.new_entry().await.unwrap(); | ||
|
||
if !test_data.is_empty() { | ||
use tokio::io::AsyncWriteExt; | ||
qmdl_file.write_all(test_data).await.unwrap(); | ||
qmdl_file.flush().await.unwrap(); | ||
} | ||
|
||
let current_entry = store.current_entry.unwrap(); | ||
let entry = &store.manifest.entries[current_entry]; | ||
let entry_name = entry.name.clone(); | ||
|
||
store | ||
.update_entry_qmdl_size(current_entry, test_data.len()) | ||
.await | ||
.unwrap(); | ||
entry_name | ||
}; | ||
|
||
let mut store = store_lock.write().await; | ||
store.close_current_entry().await.unwrap(); | ||
entry_name | ||
} | ||
|
||
fn create_test_server_state( | ||
store_lock: Arc<RwLock<crate::qmdl_store::RecordingStore>>, | ||
) -> Arc<ServerState> { | ||
let (tx, _rx) = tokio::sync::mpsc::channel(1); | ||
let (ui_tx, _ui_rx) = tokio::sync::mpsc::channel(1); | ||
let (analysis_tx, _analysis_rx) = tokio::sync::mpsc::channel(1); | ||
|
||
let analysis_status = { | ||
let store = store_lock.try_read().unwrap(); | ||
crate::analysis::AnalysisStatus::new(&*store) | ||
}; | ||
|
||
Arc::new(ServerState { | ||
qmdl_store_lock: store_lock, | ||
diag_device_ctrl_sender: tx, | ||
ui_update_sender: ui_tx, | ||
analysis_status_lock: Arc::new(RwLock::new(analysis_status)), | ||
analysis_sender: analysis_tx, | ||
debug_mode: true, | ||
}) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_zip_success() { | ||
let (_temp_dir, store_lock) = create_test_qmdl_store().await; | ||
let test_qmdl_data = vec![0x7E, 0x00, 0x00, 0x00, 0x10, 0x00, 0x7E]; | ||
let entry_name = create_test_entry_with_data(&store_lock, &test_qmdl_data).await; | ||
let state = create_test_server_state(store_lock); | ||
|
||
let result = get_zip(State(state), Path(entry_name.clone())).await; | ||
|
||
assert!(result.is_ok()); | ||
let response = result.unwrap(); | ||
|
||
let headers = response.headers(); | ||
assert_eq!(headers.get("content-type").unwrap(), "application/zip"); | ||
|
||
let body = response.into_body(); | ||
let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap(); | ||
|
||
let zip_reader = ZipFileReader::new(body_bytes.to_vec()).await.unwrap(); | ||
|
||
let filenames = zip_reader | ||
.file() | ||
.entries() | ||
.iter() | ||
.map(|entry| entry.filename().as_str().unwrap().to_owned()) | ||
.collect::<Vec<String>>(); | ||
|
||
assert_eq!( | ||
filenames, | ||
vec![format!("{entry_name}.qmdl"), format!("{entry_name}.pcapng"),] | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why
zip2
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leftover from the porting effort from
zip
toasync-zip
. renamed to justzip