# HG changeset patch # User Sebastien Jodogne # Date 1354265129 -3600 # Node ID 8a26a8e85edf00b5f2ae87a8100cedb28c9fc78a # Parent 03aa59ecf6d811a2291db60d28a63002f9c12a16 refactoring to read files diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/OrthancRestApi.h --- a/OrthancServer/OrthancRestApi.h Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/OrthancRestApi.h Fri Nov 30 09:45:29 2012 +0100 @@ -35,6 +35,7 @@ #include "../Core/HttpServer/HttpHandler.h" #include "ServerIndex.h" #include "DicomProtocol/DicomUserConnection.h" +#include "../Core/FileStorage.h" #include diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/OrthancRestApi2.cpp --- a/OrthancServer/OrthancRestApi2.cpp Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/OrthancRestApi2.cpp Fri Nov 30 09:45:29 2012 +0100 @@ -45,7 +45,8 @@ #define RETRIEVE_CONTEXT(call) \ - OrthancRestApi2& contextApi = dynamic_cast(call.GetContext()); \ + OrthancRestApi2& contextApi = \ + dynamic_cast(call.GetContext()); \ ServerContext& context = contextApi.GetContext() @@ -150,19 +151,8 @@ { RETRIEVE_CONTEXT(call); - CompressionType compressionType; - std::string fileUuid; std::string publicId = call.GetUriComponent("id", ""); - - if (context.GetIndex().GetFile(fileUuid, compressionType, publicId, AttachedFileType_Dicom)) - { - assert(compressionType == CompressionType_None); - - FilesystemHttpSender sender(context.GetFileStorage(), fileUuid); - sender.SetDownloadFilename(fileUuid + ".dcm"); - sender.SetContentType("application/dicom"); - call.GetOutput().AnswerFile(sender); - } + context.AnswerFile(call.GetOutput(), publicId, AttachedFileType_Dicom); } @@ -171,27 +161,20 @@ { RETRIEVE_CONTEXT(call); - CompressionType compressionType; - std::string fileUuid; std::string publicId = call.GetUriComponent("id", ""); - - if (context.GetIndex().GetFile(fileUuid, compressionType, publicId, AttachedFileType_Json)) - { - assert(compressionType == CompressionType_None); - - Json::Value full; - ReadJson(full, context.GetFileStorage(), fileUuid); + + Json::Value full; + context.ReadJson(full, publicId); - if (simplify) - { - Json::Value simplified; - SimplifyTags(simplified, full); - call.GetOutput().AnswerJson(simplified); - } - else - { - call.GetOutput().AnswerJson(full); - } + if (simplify) + { + Json::Value simplified; + SimplifyTags(simplified, full); + call.GetOutput().AnswerJson(simplified); + } + else + { + call.GetOutput().AnswerJson(full); } } @@ -259,7 +242,12 @@ } catch (OrthancException& e) { - //if (e.GetErrorCode() == ErrorCode_NotImplemented) + if (e.GetErrorCode() == ErrorCode_ParameterOutOfRange) + { + // The frame number is out of the range for this DICOM + // instance, the resource is not existent + } + else { std::string root = ""; for (size_t i = 1; i < call.GetFullUri().size(); i++) diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerContext.cpp --- a/OrthancServer/ServerContext.cpp Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerContext.cpp Fri Nov 30 09:45:29 2012 +0100 @@ -32,13 +32,15 @@ #include "ServerContext.h" +#include "../Core/HttpServer/FilesystemHttpSender.h" + #include namespace Orthanc { ServerContext::ServerContext(const boost::filesystem::path& path) : storage_(path.string()), - index_(storage_, path.string()) + index_(*this, path.string()) { } @@ -75,4 +77,47 @@ return status; } + + + void ServerContext::AnswerFile(RestApiOutput& output, + const std::string& instancePublicId, + AttachedFileType content) + { + CompressionType compressionType; + std::string fileUuid; + + if (index_.GetFile(fileUuid, compressionType, + instancePublicId, AttachedFileType_Dicom)) + { + assert(compressionType == CompressionType_None); + + FilesystemHttpSender sender(storage_, fileUuid); + sender.SetDownloadFilename(fileUuid + ".dcm"); + sender.SetContentType("application/dicom"); + output.AnswerFile(sender); + } + } + + + void ServerContext::ReadJson(Json::Value& result, + const std::string& instancePublicId) + { + CompressionType compressionType; + std::string fileUuid; + if (!index_.GetFile(fileUuid, compressionType, instancePublicId, AttachedFileType_Json)) + { + throw OrthancException(ErrorCode_InternalError); + } + + assert(compressionType == CompressionType_None); + + std::string s; + storage_.ReadFile(s, fileUuid); + + Json::Reader reader; + if (!reader.parse(s, result)) + { + throw OrthancException("Corrupted JSON file"); + } + } } diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerContext.h --- a/OrthancServer/ServerContext.h Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerContext.h Fri Nov 30 09:45:29 2012 +0100 @@ -34,6 +34,7 @@ #include "ServerIndex.h" #include "../Core/FileStorage.h" +#include "../Core/RestApi/RestApiOutput.h" namespace Orthanc { @@ -51,15 +52,28 @@ return index_; } + // TODO REMOVE THIS, SINCE IT IS NOT PROTECTED BY MUTEXES FileStorage& GetFileStorage() { return storage_; } + void RemoveFile(const std::string& fileUuid) + { + storage_.Remove(fileUuid); + } + StoreStatus Store(const char* dicomFile, size_t dicomSize, const DicomMap& dicomSummary, const Json::Value& dicomJson, const std::string& remoteAet); + + void AnswerFile(RestApiOutput& output, + const std::string& instancePublicId, + AttachedFileType content); + + void ReadJson(Json::Value& result, + const std::string& instancePublicId); }; } diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerIndex.cpp --- a/OrthancServer/ServerIndex.cpp Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerIndex.cpp Fri Nov 30 09:45:29 2012 +0100 @@ -32,8 +32,6 @@ #include "ServerIndex.h" -using namespace Orthanc; - #ifndef NOMINMAX #define NOMINMAX #endif @@ -44,6 +42,7 @@ #include "../Core/DicomFormat/DicomArray.h" #include "../Core/SQLite/Transaction.h" #include "FromDcmtkBridge.h" +#include "ServerContext.h" #include #include @@ -56,14 +55,14 @@ class ServerIndexListener : public IServerIndexListener { private: - FileStorage& fileStorage_; + ServerContext& context_; bool hasRemainingLevel_; ResourceType remainingType_; std::string remainingPublicId_; public: - ServerIndexListener(FileStorage& fileStorage) : - fileStorage_(fileStorage), + ServerIndexListener(ServerContext& context) : + context_(context), hasRemainingLevel_(false) { assert(ResourceType_Patient < ResourceType_Study && @@ -100,7 +99,7 @@ virtual void SignalFileDeleted(const std::string& fileUuid) { assert(Toolbox::IsUuid(fileUuid)); - fileStorage_.Remove(fileUuid); + context_.RemoveFile(fileUuid); } bool HasRemainingLevel() const @@ -180,10 +179,10 @@ } - ServerIndex::ServerIndex(FileStorage& fileStorage, + ServerIndex::ServerIndex(ServerContext& context, const std::string& dbPath) : mutex_() { - listener_.reset(new Internals::ServerIndexListener(fileStorage)); + listener_.reset(new Internals::ServerIndexListener(context)); if (dbPath == ":memory:") { diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerIndex.h --- a/OrthancServer/ServerIndex.h Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerIndex.h Fri Nov 30 09:45:29 2012 +0100 @@ -36,7 +36,6 @@ #include #include "../Core/SQLite/Connection.h" #include "../Core/DicomFormat/DicomMap.h" -#include "../Core/FileStorage.h" #include "../Core/DicomFormat/DicomInstanceHasher.h" #include "ServerEnumerations.h" @@ -45,6 +44,8 @@ namespace Orthanc { + class ServerContext; + namespace Internals { class ServerIndexListener; @@ -66,7 +67,7 @@ SeriesStatus GetSeriesStatus(int id); public: - ServerIndex(FileStorage& fileStorage, + ServerIndex(ServerContext& context, const std::string& dbPath); ~ServerIndex(); diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerToolbox.cpp --- a/OrthancServer/ServerToolbox.cpp Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerToolbox.cpp Fri Nov 30 09:45:29 2012 +0100 @@ -36,20 +36,6 @@ namespace Orthanc { - void ReadJson(Json::Value& target, - const FileStorage& storage, - const std::string& fileUuid) - { - std::string s; - storage.ReadFile(s, fileUuid); - - Json::Reader reader; - if (!reader.parse(s, target)) - { - throw OrthancException("Corrupted JSON file"); - } - } - void SimplifyTags(Json::Value& target, const Json::Value& source) { diff -r 03aa59ecf6d8 -r 8a26a8e85edf OrthancServer/ServerToolbox.h --- a/OrthancServer/ServerToolbox.h Thu Nov 29 22:28:05 2012 +0100 +++ b/OrthancServer/ServerToolbox.h Fri Nov 30 09:45:29 2012 +0100 @@ -38,10 +38,6 @@ namespace Orthanc { - void ReadJson(Json::Value& target, - const FileStorage& storage, - const std::string& fileUuid); - void SimplifyTags(Json::Value& target, const Json::Value& source); }