# HG changeset patch # User Sebastien Jodogne # Date 1373968899 -7200 # Node ID 8c3573d28868ddcf7a77cedf6a846800ae31fda8 # Parent b05eb8708aeed4503cf1c9e4ec154327d0795892 export dicom instances to the filesystem diff -r b05eb8708aee -r 8c3573d28868 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Tue Jul 16 11:30:24 2013 +0200 +++ b/Core/Toolbox.cpp Tue Jul 16 12:01:39 2013 +0200 @@ -208,10 +208,10 @@ const std::string& path) { boost::filesystem::ifstream f; - f.open(path, std::ifstream::in | std::ios::binary); + f.open(path, std::ifstream::in | std::ifstream::binary); if (!f.good()) { - throw OrthancException("Unable to open a file"); + throw OrthancException(ErrorCode_InexistentFile); } // http://www.cplusplus.com/reference/iostream/istream/tellg/ @@ -229,6 +229,26 @@ } + void Toolbox::WriteFile(const std::string& content, + const std::string& path) + { + boost::filesystem::ofstream f; + f.open(path, std::ofstream::binary); + if (!f.good()) + { + throw OrthancException(ErrorCode_CannotWriteFile); + } + + if (content.size() != 0) + { + f.write(content.c_str(), content.size()); + } + + f.close(); + } + + + void Toolbox::RemoveFile(const std::string& path) { if (boost::filesystem::exists(path)) diff -r b05eb8708aee -r 8c3573d28868 Core/Toolbox.h --- a/Core/Toolbox.h Tue Jul 16 11:30:24 2013 +0200 +++ b/Core/Toolbox.h Tue Jul 16 12:01:39 2013 +0200 @@ -57,6 +57,9 @@ void ReadFile(std::string& content, const std::string& path); + void WriteFile(const std::string& content, + const std::string& path); + void Sleep(uint32_t seconds); void USleep(uint64_t microSeconds); diff -r b05eb8708aee -r 8c3573d28868 OrthancServer/OrthancRestApi.cpp --- a/OrthancServer/OrthancRestApi.cpp Tue Jul 16 11:30:24 2013 +0200 +++ b/OrthancServer/OrthancRestApi.cpp Tue Jul 16 12:01:39 2013 +0200 @@ -735,6 +735,21 @@ } + static void ExportInstanceFile(RestApi::PostCall& call) + { + RETRIEVE_CONTEXT(call); + + std::string publicId = call.GetUriComponent("id", ""); + + std::string dicom; + context.ReadFile(dicom, publicId, FileContentType_Dicom); + + Toolbox::WriteFile(dicom, call.GetPostBody()); + + call.GetOutput().AnswerBuffer("{}", "application/json"); + } + + template static void GetInstanceTags(RestApi::GetCall& call) { @@ -1654,6 +1669,7 @@ Register("/patients/{id}/protected", IsProtectedPatient); Register("/patients/{id}/protected", SetPatientProtection); Register("/instances/{id}/file", GetInstanceFile); + Register("/instances/{id}/export", ExportInstanceFile); Register("/instances/{id}/tags", GetInstanceTags); Register("/instances/{id}/simplified-tags", GetInstanceTags); Register("/instances/{id}/frames", ListFrames); diff -r b05eb8708aee -r 8c3573d28868 UnitTests/main.cpp --- a/UnitTests/main.cpp Tue Jul 16 11:30:24 2013 +0200 +++ b/UnitTests/main.cpp Tue Jul 16 12:01:39 2013 +0200 @@ -451,6 +451,35 @@ } +TEST(Toolbox, WriteFile) +{ + std::string path; + + { + Toolbox::TemporaryFile tmp; + path = tmp.GetPath(); + + std::string s; + s.append("Hello"); + s.push_back('\0'); + s.append("World"); + ASSERT_EQ(11u, s.size()); + + Toolbox::WriteFile(s, path.c_str()); + + std::string t; + Toolbox::ReadFile(t, path.c_str()); + + ASSERT_EQ(11u, t.size()); + ASSERT_EQ(0, t[5]); + ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size())); + } + + std::string u; + ASSERT_THROW(Toolbox::ReadFile(u, path.c_str()), OrthancException); +} + + int main(int argc, char **argv) { // Initialize Google's logging library.