Skip to content

Commit

Permalink
Add remaining WADO-RS endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
feliwir committed Feb 26, 2024
1 parent a2d5bd3 commit cd3291e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 20 deletions.
53 changes: 53 additions & 0 deletions examples/simple_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,57 @@ fn search_instances(
Ok(instances)
}

fn retrieve_study(
study_uid: &str,
) -> Result<Vec<FileDicomObject<InMemDicomObject>>, Box<dyn std::error::Error>> {
let files = get_all_data_files();
let dcm_files = files
.iter()
.filter(|file| {
let dcm = FileDicomObject::open_file(file).unwrap().into_inner();
dcm.element(tags::STUDY_INSTANCE_UID)
.unwrap()
.to_str()
.unwrap()
== study_uid
})
.map(|file| FileDicomObject::open_file(file))
.collect::<Result<Vec<_>, _>>()?;

Ok(dcm_files)
}

fn retrieve_series(
study_uid: &str,
series_uid: &str,
) -> Result<Vec<FileDicomObject<InMemDicomObject>>, Box<dyn std::error::Error>> {
let files = get_all_data_files();
let dcm_files = files
.iter()
.filter(|file| {
let dcm = FileDicomObject::open_file(file).unwrap().into_inner();
if dcm
.element(tags::STUDY_INSTANCE_UID)
.unwrap()
.to_str()
.unwrap()
!= study_uid
{
return false;
}

dcm.element(tags::SERIES_INSTANCE_UID)
.unwrap()
.to_str()
.unwrap()
== series_uid
})
.map(|file| FileDicomObject::open_file(file))
.collect::<Result<Vec<_>, _>>()?;

Ok(dcm_files)
}

fn retrieve_instance(
study_uid: &str,
series_uid: &str,
Expand Down Expand Up @@ -307,6 +358,8 @@ async fn main() -> std::io::Result<()> {
search_instances: search_instances,
search_series: search_series,
search_study: search_study,
retrieve_study: retrieve_study,
retrieve_series: retrieve_series,
retrieve_instance: retrieve_instance,
store_instances: store_instances,
}))
Expand Down
4 changes: 2 additions & 2 deletions server/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use dicom::dictionary_std::tags;

use dicom_object::InMemDicomObject;

use crate::QidoStudyQuery;

pub fn study_filter(dcm: &InMemDicomObject, query: &QidoStudyQuery) -> bool {
pub fn study_filter(_dcm: &InMemDicomObject, _query: &QidoStudyQuery) -> bool {
true
}
9 changes: 9 additions & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ pub struct DicomWebServer {
Option<&str>, // series_instance_uid
&QidoInstanceQuery,
) -> Result<Vec<InMemDicomObject>, Box<dyn std::error::Error>>,
pub retrieve_study:
fn(
&str, // study_instance_uid
) -> Result<Vec<FileDicomObject<InMemDicomObject>>, Box<dyn std::error::Error>>,
pub retrieve_series:
fn(
&str, // study_instance_uid
&str, // series_instance_uid
) -> Result<Vec<FileDicomObject<InMemDicomObject>>, Box<dyn std::error::Error>>,
pub retrieve_instance:
fn(
&str, // study_instance_uid
Expand Down
2 changes: 1 addition & 1 deletion server/src/multipart/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix_web::http::header::HeaderMap;

use std::io::{self, Read, Write};
use uuid::Uuid;

Expand Down
79 changes: 62 additions & 17 deletions server/src/wado.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
fs,
io::{Cursor, Write},
io::{Write},
};

use actix_web::{get, web, HttpResponse, Responder};
Expand All @@ -11,16 +10,72 @@ use crate::{multipart::MultipartWriter, DicomWebServer};
///
///
#[get("/studies/{study_uid}")]
pub async fn retrieve_study(_study_uid: web::Path<String>) -> impl Responder {
HttpResponse::Ok().body("retrieve_study")
pub async fn retrieve_study(
callbacks: web::Data<DicomWebServer>,
study_uid: web::Path<String>,
) -> impl Responder {
let result = (callbacks.retrieve_study)(&study_uid);

match result {
Ok(dcm_files) => {
let mut mp = MultipartWriter::new();
for dcm_file in dcm_files {
let mut data: Vec<u8> = Vec::new();

// Write the DICOM file to memory and add it to our stream
if let Err(e) = dcm_file.write_all(&mut data) {
return HttpResponse::InternalServerError().body(e.to_string());
}

if let Err(e) = mp.add(&*data, "Content-Type: application/dicom") {
return HttpResponse::InternalServerError().body(e.to_string());
}
}

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()),
}
}

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

match result {
Ok(dcm_files) => {
let mut mp = MultipartWriter::new();
for dcm_file in dcm_files {
let mut data: Vec<u8> = Vec::new();

// Write the DICOM file to memory and add it to our stream
if let Err(e) = dcm_file.write_all(&mut data) {
return HttpResponse::InternalServerError().body(e.to_string());
}

if let Err(e) = mp.add(&*data, "Content-Type: application/dicom") {
return HttpResponse::InternalServerError().body(e.to_string());
}
}

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()),
}
}

#[get("/studies/{study_uid}/series/{series_uid}/instances/{instance_uid}")]
Expand All @@ -44,16 +99,6 @@ pub async fn retrieve_instance(
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();

file.write_all(&mp.data);
}

let content_type = format!(
"multipart/related; type=application/dicom; boundary={}",
Expand Down

0 comments on commit cd3291e

Please sign in to comment.