From a2d5bd38aa76f182485817c957021ccb3fb71449 Mon Sep 17 00:00:00 2001 From: Stephan Vedder Date: Mon, 26 Feb 2024 16:35:04 +0100 Subject: [PATCH] Fix multipart writer --- server/src/multipart/builder.rs | 14 ++++++++++--- server/src/wado.rs | 35 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/server/src/multipart/builder.rs b/server/src/multipart/builder.rs index 815d7c7..f89e226 100644 --- a/server/src/multipart/builder.rs +++ b/server/src/multipart/builder.rs @@ -1,8 +1,9 @@ +use actix_web::http::header::HeaderMap; use std::io::{self, Read, Write}; use uuid::Uuid; pub struct MultipartWriter { - boundary: String, + pub boundary: String, pub data: Vec, first: bool, } @@ -24,7 +25,7 @@ impl MultipartWriter { } } - pub fn add(self: &mut Self, reader: &mut dyn Read) -> io::Result { + pub fn add(self: &mut Self, mut reader: impl Read, headers: &str) -> io::Result { // writer for the result let mut writer = std::io::BufWriter::new(&mut self.data); @@ -37,7 +38,14 @@ impl MultipartWriter { writer.write_all(self.boundary.as_bytes()).unwrap(); writer.write_all(b"\r\n").unwrap(); + // write the content type + writer.write_all(headers.as_bytes()).unwrap(); + + // write an empty line + writer.write_all(b"\r\n").unwrap(); + writer.write_all(b"\r\n").unwrap(); + // write the content - io::copy(reader, &mut writer) + io::copy(&mut reader, &mut writer) } } diff --git a/server/src/wado.rs b/server/src/wado.rs index 7907143..127277c 100644 --- a/server/src/wado.rs +++ b/server/src/wado.rs @@ -1,4 +1,7 @@ -use std::io::Cursor; +use std::{ + fs, + io::{Cursor, Write}, +}; use actix_web::{get, web, HttpResponse, Responder}; @@ -23,29 +26,41 @@ pub async fn retrieve_series( #[get("/studies/{study_uid}/series/{series_uid}/instances/{instance_uid}")] pub async fn retrieve_instance( callbacks: web::Data, - study_uid: web::Path, - series_uid: web::Path, - instance_uid: web::Path, + path: web::Path<(String, String, String)>, ) -> impl Responder { + let (study_uid, series_uid, instance_uid) = path.into_inner(); let result = (callbacks.retrieve_instance)(&study_uid, &series_uid, &instance_uid); match result { Ok(dcm_file) => { let mut mp = MultipartWriter::new(); + let mut data: Vec = Vec::new(); // Write the DICOM file to memory and add it to our stream - let mut cursor = Cursor::new(Vec::new()); - if let Err(e) = dcm_file.write_all(cursor.clone()) { + if let Err(e) = dcm_file.write_all(&mut data) { return HttpResponse::InternalServerError().body(e.to_string()); } - if let Err(e) = mp.add(&mut cursor) { + if let Err(e) = mp.add(&*data, "Content-Type: application/dicom") { return HttpResponse::InternalServerError().body(e.to_string()); } + { + let mut file = fs::OpenOptions::new() + .create(true) // To create a new file + .write(true) + // either use the ? operator or unwrap since it returns a Result + .open("test.txt") + .unwrap(); - return HttpResponse::Ok() - .content_type("multipart/related; type=application/dicom") - .body(mp.data); + file.write_all(&mp.data); + } + + let content_type = format!( + "multipart/related; type=application/dicom; boundary={}", + mp.boundary + ); + + return HttpResponse::Ok().content_type(content_type).body(mp.data); } Err(e) => return HttpResponse::InternalServerError().body(e.to_string()), }