Skip to content

Commit

Permalink
Start with WADO-RS
Browse files Browse the repository at this point in the history
  • Loading branch information
feliwir committed Feb 26, 2024
1 parent 1649b50 commit cf3f566
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ memchr = "2.7.1"
mime = "0.3.17"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"
uuid = { version = "1.7.0", features = ["v4"] }
43 changes: 43 additions & 0 deletions server/src/multipart/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::io::{self, Read, Write};
use uuid::Uuid;

pub struct MultipartWriter {
boundary: String,
pub data: Vec<u8>,
first: bool,
}

impl MultipartWriter {
pub fn new() -> MultipartWriter {
MultipartWriter {
boundary: format!("boundary-{}", Uuid::new_v4()),
first: true,
data: Vec::new(),
}
}

pub fn new_with_boundary(boundary: &str) -> MultipartWriter {
MultipartWriter {
boundary: boundary.to_string(),
first: true,
data: Vec::new(),
}
}

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

// write the boundary
if !self.first {
writer.write_all(b"\r\n").unwrap();
}

writer.write_all(b"--").unwrap();
writer.write_all(self.boundary.as_bytes()).unwrap();
writer.write_all(b"\r\n").unwrap();

// write the content
io::copy(reader, &mut writer)
}
}
2 changes: 2 additions & 0 deletions server/src/multipart/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod builder;
mod extractor;
mod reader;

pub use builder::*;
pub use reader::*;
34 changes: 30 additions & 4 deletions server/src/wado.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::io::Cursor;

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

use crate::{multipart::MultipartWriter, DicomWebServer};

/// WADO-RS
///
///
Expand All @@ -18,11 +22,33 @@ pub async fn retrieve_series(

#[get("/studies/{study_uid}/series/{series_uid}/instances/{instance_uid}")]
pub async fn retrieve_instance(
_study_uid: web::Path<String>,
_series_uid: web::Path<String>,
_instance_uid: web::Path<String>,
callbacks: web::Data<DicomWebServer>,
study_uid: web::Path<String>,
series_uid: web::Path<String>,
instance_uid: web::Path<String>,
) -> impl Responder {
HttpResponse::Ok().body("retrieve_instance")
let result = (callbacks.retrieve_instance)(&study_uid, &series_uid, &instance_uid);

match result {
Ok(dcm_file) => {
let mut mp = MultipartWriter::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()) {
return HttpResponse::InternalServerError().body(e.to_string());
}

if let Err(e) = mp.add(&mut cursor) {
return HttpResponse::InternalServerError().body(e.to_string());
}

return HttpResponse::Ok()
.content_type("multipart/related; type=application/dicom")
.body(mp.data);
}
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
}
}

pub fn wado_config(cfg: &mut web::ServiceConfig) {
Expand Down

0 comments on commit cf3f566

Please sign in to comment.