comparison OrthancServer/ServerContext.cpp @ 232:5368bbe813cf

refactoring of attachments
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 14:22:27 +0100
parents 209ca3f6db62
children c11273198cef
comparison
equal deleted inserted replaced
231:8098448bd827 232:5368bbe813cf
48 48
49 namespace Orthanc 49 namespace Orthanc
50 { 50 {
51 ServerContext::ServerContext(const boost::filesystem::path& path) : 51 ServerContext::ServerContext(const boost::filesystem::path& path) :
52 storage_(path.string()), 52 storage_(path.string()),
53 index_(*this, path.string()) 53 index_(*this, path.string()),
54 accessor_(storage_)
54 { 55 {
55 } 56 }
56 57
57 void ServerContext::RemoveFile(const std::string& fileUuid) 58 void ServerContext::RemoveFile(const std::string& fileUuid)
58 { 59 {
63 size_t dicomSize, 64 size_t dicomSize,
64 const DicomMap& dicomSummary, 65 const DicomMap& dicomSummary,
65 const Json::Value& dicomJson, 66 const Json::Value& dicomJson,
66 const std::string& remoteAet) 67 const std::string& remoteAet)
67 { 68 {
68 std::string fileUuid = storage_.Create(dicomFile, dicomSize); 69 //accessor_.SetCompressionForNextOperations(CompressionType_Zlib);
69 std::string jsonUuid = storage_.Create(dicomJson.toStyledString()); 70
70 StoreStatus status = index_.Store(dicomSummary, fileUuid, dicomSize, jsonUuid, remoteAet); 71 FileInfo dicomInfo = accessor_.Write(dicomFile, dicomSize, FileType_Dicom);
72 FileInfo jsonInfo = accessor_.Write(dicomJson.toStyledString(), FileType_Json);
73
74 ServerIndex::Attachments attachments;
75 attachments.push_back(dicomInfo);
76 attachments.push_back(jsonInfo);
77
78 StoreStatus status = index_.Store(dicomSummary, attachments, remoteAet);
71 79
72 if (status != StoreStatus_Success) 80 if (status != StoreStatus_Success)
73 { 81 {
74 storage_.Remove(fileUuid); 82 storage_.Remove(dicomInfo.GetUuid());
75 storage_.Remove(jsonUuid); 83 storage_.Remove(jsonInfo.GetUuid());
76 } 84 }
77 85
78 switch (status) 86 switch (status)
79 { 87 {
80 case StoreStatus_Success: 88 case StoreStatus_Success:
94 } 102 }
95 103
96 104
97 void ServerContext::AnswerFile(RestApiOutput& output, 105 void ServerContext::AnswerFile(RestApiOutput& output,
98 const std::string& instancePublicId, 106 const std::string& instancePublicId,
99 AttachedFileType content) 107 FileType content)
100 { 108 {
101 CompressionType compressionType; 109 FileInfo attachment;
102 std::string fileUuid; 110 if (index_.LookupAttachment(attachment, instancePublicId, FileType_Dicom))
111 {
112 assert(attachment.GetCompressionType() == CompressionType_None);
113 assert(attachment.GetFileType() == FileType_Dicom);
103 114
104 if (index_.GetFile(fileUuid, compressionType, 115 FilesystemHttpSender sender(storage_, attachment.GetUuid());
105 instancePublicId, AttachedFileType_Dicom)) 116 sender.SetDownloadFilename(attachment.GetUuid() + ".dcm");
106 {
107 assert(compressionType == CompressionType_None);
108
109 FilesystemHttpSender sender(storage_, fileUuid);
110 sender.SetDownloadFilename(fileUuid + ".dcm");
111 sender.SetContentType("application/dicom"); 117 sender.SetContentType("application/dicom");
112 output.AnswerFile(sender); 118 output.AnswerFile(sender);
113 } 119 }
114 } 120 }
115 121
116 122
117 void ServerContext::ReadJson(Json::Value& result, 123 void ServerContext::ReadJson(Json::Value& result,
118 const std::string& instancePublicId) 124 const std::string& instancePublicId)
119 { 125 {
120 std::string s; 126 std::string s;
121 ReadFile(s, instancePublicId, AttachedFileType_Json); 127 ReadFile(s, instancePublicId, FileType_Json);
122 128
123 Json::Reader reader; 129 Json::Reader reader;
124 if (!reader.parse(s, result)) 130 if (!reader.parse(s, result))
125 { 131 {
126 throw OrthancException("Corrupted JSON file"); 132 throw OrthancException("Corrupted JSON file");
128 } 134 }
129 135
130 136
131 void ServerContext::ReadFile(std::string& result, 137 void ServerContext::ReadFile(std::string& result,
132 const std::string& instancePublicId, 138 const std::string& instancePublicId,
133 AttachedFileType content) 139 FileType content)
134 { 140 {
135 CompressionType compressionType; 141 FileInfo attachment;
136 std::string fileUuid; 142 if (!index_.LookupAttachment(attachment, instancePublicId, content))
137 if (!index_.GetFile(fileUuid, compressionType, instancePublicId, content))
138 { 143 {
139 throw OrthancException(ErrorCode_InternalError); 144 throw OrthancException(ErrorCode_InternalError);
140 } 145 }
141 146
142 assert(compressionType == CompressionType_None); 147 accessor_.SetCompressionForNextOperations(attachment.GetCompressionType());
143 storage_.ReadFile(result, fileUuid); 148 accessor_.Read(result, attachment.GetUuid());
144 } 149 }
145 } 150 }