Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestArchive.cpp @ 751:5197fd35333c
refactoring of OrthancRestApi
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 14 Apr 2014 11:28:35 +0200 |
parents | |
children | a811bdf8b8eb |
comparison
equal
deleted
inserted
replaced
750:4afad8cb94fd | 751:5197fd35333c |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "OrthancRestApi.h" | |
34 | |
35 #include "../../Core/Compression/HierarchicalZipWriter.h" | |
36 #include "../../Core/HttpServer/FilesystemHttpSender.h" | |
37 #include "../../Core/Uuid.h" | |
38 | |
39 #include <glog/logging.h> | |
40 | |
41 #if defined(_MSC_VER) | |
42 #define snprintf _snprintf | |
43 #endif | |
44 | |
45 static const uint64_t MEGA_BYTES = 1024 * 1024; | |
46 static const uint64_t GIGA_BYTES = 1024 * 1024 * 1024; | |
47 | |
48 namespace Orthanc | |
49 { | |
50 // Download of ZIP files ---------------------------------------------------- | |
51 | |
52 static std::string GetDirectoryNameInArchive(const Json::Value& resource, | |
53 ResourceType resourceType) | |
54 { | |
55 switch (resourceType) | |
56 { | |
57 case ResourceType_Patient: | |
58 { | |
59 std::string p = resource["MainDicomTags"]["PatientID"].asString(); | |
60 std::string n = resource["MainDicomTags"]["PatientName"].asString(); | |
61 return p + " " + n; | |
62 } | |
63 | |
64 case ResourceType_Study: | |
65 { | |
66 return resource["MainDicomTags"]["StudyDescription"].asString(); | |
67 } | |
68 | |
69 case ResourceType_Series: | |
70 { | |
71 std::string d = resource["MainDicomTags"]["SeriesDescription"].asString(); | |
72 std::string m = resource["MainDicomTags"]["Modality"].asString(); | |
73 return m + " " + d; | |
74 } | |
75 | |
76 default: | |
77 throw OrthancException(ErrorCode_InternalError); | |
78 } | |
79 } | |
80 | |
81 static bool CreateRootDirectoryInArchive(HierarchicalZipWriter& writer, | |
82 ServerContext& context, | |
83 const Json::Value& resource, | |
84 ResourceType resourceType) | |
85 { | |
86 if (resourceType == ResourceType_Patient) | |
87 { | |
88 return true; | |
89 } | |
90 | |
91 ResourceType parentType = GetParentResourceType(resourceType); | |
92 Json::Value parent; | |
93 | |
94 switch (resourceType) | |
95 { | |
96 case ResourceType_Study: | |
97 { | |
98 if (!context.GetIndex().LookupResource(parent, resource["ParentPatient"].asString(), parentType)) | |
99 { | |
100 return false; | |
101 } | |
102 | |
103 break; | |
104 } | |
105 | |
106 case ResourceType_Series: | |
107 if (!context.GetIndex().LookupResource(parent, resource["ParentStudy"].asString(), parentType) || | |
108 !CreateRootDirectoryInArchive(writer, context, parent, parentType)) | |
109 { | |
110 return false; | |
111 } | |
112 break; | |
113 | |
114 default: | |
115 throw OrthancException(ErrorCode_NotImplemented); | |
116 } | |
117 | |
118 writer.OpenDirectory(GetDirectoryNameInArchive(parent, parentType).c_str()); | |
119 return true; | |
120 } | |
121 | |
122 static bool ArchiveInstance(HierarchicalZipWriter& writer, | |
123 ServerContext& context, | |
124 const std::string& instancePublicId, | |
125 const char* filename) | |
126 { | |
127 Json::Value instance; | |
128 | |
129 if (!context.GetIndex().LookupResource(instance, instancePublicId, ResourceType_Instance)) | |
130 { | |
131 return false; | |
132 } | |
133 | |
134 writer.OpenFile(filename); | |
135 | |
136 std::string dicom; | |
137 context.ReadFile(dicom, instancePublicId, FileContentType_Dicom); | |
138 writer.Write(dicom); | |
139 | |
140 return true; | |
141 } | |
142 | |
143 static bool ArchiveInternal(HierarchicalZipWriter& writer, | |
144 ServerContext& context, | |
145 const std::string& publicId, | |
146 ResourceType resourceType, | |
147 bool isFirstLevel) | |
148 { | |
149 Json::Value resource; | |
150 if (!context.GetIndex().LookupResource(resource, publicId, resourceType)) | |
151 { | |
152 return false; | |
153 } | |
154 | |
155 if (isFirstLevel && | |
156 !CreateRootDirectoryInArchive(writer, context, resource, resourceType)) | |
157 { | |
158 return false; | |
159 } | |
160 | |
161 writer.OpenDirectory(GetDirectoryNameInArchive(resource, resourceType).c_str()); | |
162 | |
163 switch (resourceType) | |
164 { | |
165 case ResourceType_Patient: | |
166 for (Json::Value::ArrayIndex i = 0; i < resource["Studies"].size(); i++) | |
167 { | |
168 std::string studyId = resource["Studies"][i].asString(); | |
169 if (!ArchiveInternal(writer, context, studyId, ResourceType_Study, false)) | |
170 { | |
171 return false; | |
172 } | |
173 } | |
174 break; | |
175 | |
176 case ResourceType_Study: | |
177 for (Json::Value::ArrayIndex i = 0; i < resource["Series"].size(); i++) | |
178 { | |
179 std::string seriesId = resource["Series"][i].asString(); | |
180 if (!ArchiveInternal(writer, context, seriesId, ResourceType_Series, false)) | |
181 { | |
182 return false; | |
183 } | |
184 } | |
185 break; | |
186 | |
187 case ResourceType_Series: | |
188 { | |
189 // Create a filename prefix, depending on the modality | |
190 char format[16] = "%08d"; | |
191 | |
192 if (resource["MainDicomTags"].isMember("Modality")) | |
193 { | |
194 std::string modality = resource["MainDicomTags"]["Modality"].asString(); | |
195 | |
196 if (modality.size() == 1) | |
197 { | |
198 snprintf(format, sizeof(format) - 1, "%c%%07d", toupper(modality[0])); | |
199 } | |
200 else if (modality.size() >= 2) | |
201 { | |
202 snprintf(format, sizeof(format) - 1, "%c%c%%06d", toupper(modality[0]), toupper(modality[1])); | |
203 } | |
204 } | |
205 | |
206 char filename[16]; | |
207 | |
208 for (Json::Value::ArrayIndex i = 0; i < resource["Instances"].size(); i++) | |
209 { | |
210 snprintf(filename, sizeof(filename) - 1, format, i); | |
211 | |
212 std::string publicId = resource["Instances"][i].asString(); | |
213 | |
214 // This was the implementation up to Orthanc 0.7.0: | |
215 // std::string filename = instance["MainDicomTags"]["SOPInstanceUID"].asString() + ".dcm"; | |
216 | |
217 if (!ArchiveInstance(writer, context, publicId, filename)) | |
218 { | |
219 return false; | |
220 } | |
221 } | |
222 | |
223 break; | |
224 } | |
225 | |
226 default: | |
227 throw OrthancException(ErrorCode_InternalError); | |
228 } | |
229 | |
230 writer.CloseDirectory(); | |
231 return true; | |
232 } | |
233 | |
234 template <enum ResourceType resourceType> | |
235 static void GetArchive(RestApi::GetCall& call) | |
236 { | |
237 ServerContext& context = OrthancRestApi::GetContext(call); | |
238 | |
239 std::string id = call.GetUriComponent("id", ""); | |
240 | |
241 /** | |
242 * Determine whether ZIP64 is required. Original ZIP format can | |
243 * store up to 2GB of data (some implementation supporting up to | |
244 * 4GB of data), and up to 65535 files. | |
245 * https://en.wikipedia.org/wiki/Zip_(file_format)#ZIP64 | |
246 **/ | |
247 | |
248 uint64_t uncompressedSize; | |
249 uint64_t compressedSize; | |
250 unsigned int countStudies; | |
251 unsigned int countSeries; | |
252 unsigned int countInstances; | |
253 context.GetIndex().GetStatistics(compressedSize, uncompressedSize, | |
254 countStudies, countSeries, countInstances, id); | |
255 const bool isZip64 = (uncompressedSize >= 2 * GIGA_BYTES || | |
256 countInstances >= 65535); | |
257 | |
258 LOG(INFO) << "Creating a ZIP file with " << countInstances << " files of size " | |
259 << (uncompressedSize / MEGA_BYTES) << "MB using the " | |
260 << (isZip64 ? "ZIP64" : "ZIP32") << " file format"; | |
261 | |
262 // Create a RAII for the temporary file to manage the ZIP file | |
263 Toolbox::TemporaryFile tmp; | |
264 | |
265 { | |
266 // Create a ZIP writer | |
267 HierarchicalZipWriter writer(tmp.GetPath().c_str()); | |
268 writer.SetZip64(isZip64); | |
269 | |
270 // Store the requested resource into the ZIP | |
271 if (!ArchiveInternal(writer, context, id, resourceType, true)) | |
272 { | |
273 return; | |
274 } | |
275 } | |
276 | |
277 // Prepare the sending of the ZIP file | |
278 FilesystemHttpSender sender(tmp.GetPath().c_str()); | |
279 sender.SetContentType("application/zip"); | |
280 sender.SetDownloadFilename(id + ".zip"); | |
281 | |
282 // Send the ZIP | |
283 call.GetOutput().AnswerFile(sender); | |
284 | |
285 // The temporary file is automatically removed thanks to the RAII | |
286 } | |
287 | |
288 | |
289 void OrthancRestApi::RegisterArchive() | |
290 { | |
291 Register("/patients/{id}/archive", GetArchive<ResourceType_Patient>); | |
292 Register("/studies/{id}/archive", GetArchive<ResourceType_Study>); | |
293 Register("/series/{id}/archive", GetArchive<ResourceType_Series>); | |
294 } | |
295 } |