Mercurial > hg > orthanc-stone
comparison Resources/Orthanc/Core/FileStorage/StorageAccessor.cpp @ 200:03afbee0cc7b
integration of Orthanc core into Stone
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 23 Mar 2018 11:04:03 +0100 |
parents | |
children | 795d71f66f31 |
comparison
equal
deleted
inserted
replaced
199:dabe9982fca3 | 200:03afbee0cc7b |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "StorageAccessor.h" | |
36 | |
37 #include "../Compression/ZlibCompressor.h" | |
38 #include "../OrthancException.h" | |
39 #include "../Toolbox.h" | |
40 #include "../SystemToolbox.h" | |
41 | |
42 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 | |
43 # include "../HttpServer/HttpStreamTranscoder.h" | |
44 #endif | |
45 | |
46 namespace Orthanc | |
47 { | |
48 FileInfo StorageAccessor::Write(const void* data, | |
49 size_t size, | |
50 FileContentType type, | |
51 CompressionType compression, | |
52 bool storeMd5) | |
53 { | |
54 std::string uuid = SystemToolbox::GenerateUuid(); | |
55 | |
56 std::string md5; | |
57 | |
58 if (storeMd5) | |
59 { | |
60 Toolbox::ComputeMD5(md5, data, size); | |
61 } | |
62 | |
63 switch (compression) | |
64 { | |
65 case CompressionType_None: | |
66 { | |
67 area_.Create(uuid, data, size, type); | |
68 return FileInfo(uuid, type, size, md5); | |
69 } | |
70 | |
71 case CompressionType_ZlibWithSize: | |
72 { | |
73 ZlibCompressor zlib; | |
74 | |
75 std::string compressed; | |
76 zlib.Compress(compressed, data, size); | |
77 | |
78 std::string compressedMD5; | |
79 | |
80 if (storeMd5) | |
81 { | |
82 Toolbox::ComputeMD5(compressedMD5, compressed); | |
83 } | |
84 | |
85 if (compressed.size() > 0) | |
86 { | |
87 area_.Create(uuid, &compressed[0], compressed.size(), type); | |
88 } | |
89 else | |
90 { | |
91 area_.Create(uuid, NULL, 0, type); | |
92 } | |
93 | |
94 return FileInfo(uuid, type, size, md5, | |
95 CompressionType_ZlibWithSize, compressed.size(), compressedMD5); | |
96 } | |
97 | |
98 default: | |
99 throw OrthancException(ErrorCode_NotImplemented); | |
100 } | |
101 } | |
102 | |
103 | |
104 void StorageAccessor::Read(std::string& content, | |
105 const FileInfo& info) | |
106 { | |
107 switch (info.GetCompressionType()) | |
108 { | |
109 case CompressionType_None: | |
110 { | |
111 area_.Read(content, info.GetUuid(), info.GetContentType()); | |
112 break; | |
113 } | |
114 | |
115 case CompressionType_ZlibWithSize: | |
116 { | |
117 ZlibCompressor zlib; | |
118 | |
119 std::string compressed; | |
120 area_.Read(compressed, info.GetUuid(), info.GetContentType()); | |
121 IBufferCompressor::Uncompress(content, zlib, compressed); | |
122 break; | |
123 } | |
124 | |
125 default: | |
126 { | |
127 throw OrthancException(ErrorCode_NotImplemented); | |
128 } | |
129 } | |
130 | |
131 // TODO Check the validity of the uncompressed MD5? | |
132 } | |
133 | |
134 | |
135 void StorageAccessor::Read(Json::Value& content, | |
136 const FileInfo& info) | |
137 { | |
138 std::string s; | |
139 Read(s, info); | |
140 | |
141 Json::Reader reader; | |
142 if (!reader.parse(s, content)) | |
143 { | |
144 throw OrthancException(ErrorCode_BadFileFormat); | |
145 } | |
146 } | |
147 | |
148 | |
149 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 | |
150 void StorageAccessor::SetupSender(BufferHttpSender& sender, | |
151 const FileInfo& info, | |
152 const std::string& mime) | |
153 { | |
154 area_.Read(sender.GetBuffer(), info.GetUuid(), info.GetContentType()); | |
155 sender.SetContentType(mime); | |
156 | |
157 const char* extension; | |
158 switch (info.GetContentType()) | |
159 { | |
160 case FileContentType_Dicom: | |
161 extension = ".dcm"; | |
162 break; | |
163 | |
164 case FileContentType_DicomAsJson: | |
165 extension = ".json"; | |
166 break; | |
167 | |
168 default: | |
169 // Non-standard content type | |
170 extension = ""; | |
171 } | |
172 | |
173 sender.SetContentFilename(info.GetUuid() + std::string(extension)); | |
174 } | |
175 #endif | |
176 | |
177 | |
178 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 | |
179 void StorageAccessor::AnswerFile(HttpOutput& output, | |
180 const FileInfo& info, | |
181 const std::string& mime) | |
182 { | |
183 BufferHttpSender sender; | |
184 SetupSender(sender, info, mime); | |
185 | |
186 HttpStreamTranscoder transcoder(sender, info.GetCompressionType()); | |
187 output.Answer(transcoder); | |
188 } | |
189 #endif | |
190 | |
191 | |
192 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 | |
193 void StorageAccessor::AnswerFile(RestApiOutput& output, | |
194 const FileInfo& info, | |
195 const std::string& mime) | |
196 { | |
197 BufferHttpSender sender; | |
198 SetupSender(sender, info, mime); | |
199 | |
200 HttpStreamTranscoder transcoder(sender, info.GetCompressionType()); | |
201 output.AnswerStream(transcoder); | |
202 } | |
203 #endif | |
204 } |