Skip to content

Commit

Permalink
Fix multipart writer
Browse files Browse the repository at this point in the history
  • Loading branch information
feliwir committed Feb 26, 2024
1 parent cf3f566 commit a2d5bd3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
14 changes: 11 additions & 3 deletions server/src/multipart/builder.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
first: bool,
}
Expand All @@ -24,7 +25,7 @@ impl MultipartWriter {
}
}

pub fn add(self: &mut Self, reader: &mut dyn Read) -> io::Result<u64> {
pub fn add(self: &mut Self, mut reader: impl Read, headers: &str) -> io::Result<u64> {
// writer for the result
let mut writer = std::io::BufWriter::new(&mut self.data);

Expand All @@ -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)
}
}
35 changes: 25 additions & 10 deletions server/src/wado.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::io::Cursor;
use std::{
fs,
io::{Cursor, Write},
};

use actix_web::{get, web, HttpResponse, Responder};

Expand All @@ -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<DicomWebServer>,
study_uid: web::Path<String>,
series_uid: web::Path<String>,
instance_uid: web::Path<String>,
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<u8> = 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()),
}
Expand Down

0 comments on commit a2d5bd3

Please sign in to comment.