Mercurial > hg > orthanc-transfers
annotate Framework/DownloadArea.cpp @ 27:8f3137dc55c1
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 06 Jul 2020 21:33:11 +0200 |
parents | ebf978ab064d |
children | 44a0430d7899 |
rev | line source |
---|---|
0 | 1 /** |
2 * Transfers accelerator plugin for Orthanc | |
19 | 3 * Copyright (C) 2018-2020 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "DownloadArea.h" | |
21 | |
22 | 22 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
23 | |
20 | 24 #include <Compression/GzipCompressor.h> |
25 #include <Logging.h> | |
26 #include <SystemToolbox.h> | |
0 | 27 |
28 #include <boost/filesystem.hpp> | |
29 #include <boost/filesystem/fstream.hpp> | |
30 | |
31 namespace OrthancPlugins | |
32 { | |
33 class DownloadArea::Instance::Writer : public boost::noncopyable | |
34 { | |
35 private: | |
36 boost::filesystem::ofstream stream_; | |
37 | |
38 public: | |
39 Writer(Orthanc::TemporaryFile& f, | |
40 bool create) | |
41 { | |
42 if (create) | |
43 { | |
44 // Create the file. | |
45 stream_.open(f.GetPath(), std::ofstream::out | std::ofstream::binary); | |
46 } | |
47 else | |
48 { | |
49 // Open the existing file to modify it. The "in" mode is | |
50 // necessary, otherwise previous content is lost by | |
51 // truncation (as an ofstream defaults to std::ios::trunc, | |
52 // the flag to truncate the existing content). | |
53 stream_.open(f.GetPath(), std::ofstream::in | std::ofstream::out | std::ofstream::binary); | |
54 } | |
55 | |
56 if (!stream_.good()) | |
57 { | |
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_CannotWriteFile); | |
59 } | |
60 } | |
61 | |
62 void Write(size_t offset, | |
63 const void* data, | |
64 size_t size) | |
65 { | |
66 stream_.seekp(offset); | |
67 stream_.write(reinterpret_cast<const char*>(data), size); | |
68 } | |
69 }; | |
70 | |
71 | |
72 DownloadArea::Instance::Instance(const DicomInstanceInfo& info) : | |
73 info_(info) | |
74 { | |
75 Writer writer(file_, true); | |
76 | |
77 // Create a sparse file of expected size | |
78 if (info_.GetSize() != 0) | |
79 { | |
80 writer.Write(info_.GetSize() - 1, "", 1); | |
81 } | |
82 } | |
83 | |
84 | |
85 void DownloadArea::Instance::WriteChunk(size_t offset, | |
86 const void* data, | |
87 size_t size) | |
88 { | |
89 if (offset + size > info_.GetSize()) | |
90 { | |
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
92 } | |
93 else if (size > 0) | |
94 { | |
95 Writer writer(file_, false); | |
96 writer.Write(offset, data, size); | |
97 } | |
98 } | |
99 | |
100 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
101 void DownloadArea::Instance::Commit(bool simulate) const |
0 | 102 { |
103 std::string content; | |
104 Orthanc::SystemToolbox::ReadFile(content, file_.GetPath()); | |
105 | |
106 std::string md5; | |
107 Orthanc::Toolbox::ComputeMD5(md5, content); | |
108 | |
109 if (md5 == info_.GetMD5()) | |
110 { | |
111 if (!simulate) | |
112 { | |
113 Json::Value result; | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
114 if (!RestApiPost(result, "/instances", |
0 | 115 content.empty() ? NULL : content.c_str(), content.size(), |
116 false)) | |
117 { | |
118 LOG(ERROR) << "Cannot import a transfered DICOM instance into Orthanc: " | |
119 << info_.GetId(); | |
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile); | |
121 } | |
122 } | |
123 } | |
124 else | |
125 { | |
126 LOG(ERROR) << "Bad MD5 sum in a transfered DICOM instance: " << info_.GetId(); | |
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile); | |
128 } | |
129 } | |
130 | |
131 | |
132 void DownloadArea::Clear() | |
133 { | |
134 for (Instances::iterator it = instances_.begin(); | |
135 it != instances_.end(); ++it) | |
136 { | |
137 if (it->second != NULL) | |
138 { | |
139 delete it->second; | |
140 it->second = NULL; | |
141 } | |
142 } | |
143 | |
144 instances_.clear(); | |
145 } | |
146 | |
147 | |
148 DownloadArea::Instance& DownloadArea::LookupInstance(const std::string& id) | |
149 { | |
150 Instances::iterator it = instances_.find(id); | |
151 | |
152 if (it == instances_.end()) | |
153 { | |
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); | |
155 } | |
156 else if (it->first != id || | |
157 it->second == NULL) | |
158 { | |
159 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
160 } | |
161 else | |
162 { | |
163 return *it->second; | |
164 } | |
165 } | |
166 | |
167 | |
168 void DownloadArea::WriteUncompressedBucket(const TransferBucket& bucket, | |
169 const void* data, | |
170 size_t size) | |
171 { | |
172 if (size != bucket.GetTotalSize()) | |
173 { | |
174 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
175 } | |
176 | |
177 if (size == 0) | |
178 { | |
179 return; | |
180 } | |
181 | |
182 size_t pos = 0; | |
183 | |
184 for (size_t i = 0; i < bucket.GetChunksCount(); i++) | |
185 { | |
186 size_t chunkSize = bucket.GetChunkSize(i); | |
187 size_t offset = bucket.GetChunkOffset(i); | |
188 | |
189 if (pos + chunkSize > size) | |
190 { | |
191 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
192 } | |
193 | |
194 Instance& instance = LookupInstance(bucket.GetChunkInstanceId(i)); | |
195 instance.WriteChunk(offset, reinterpret_cast<const char*>(data) + pos, chunkSize); | |
196 | |
197 pos += chunkSize; | |
198 } | |
199 | |
200 if (pos != size) | |
201 { | |
202 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
203 } | |
204 } | |
205 | |
206 | |
207 void DownloadArea::Setup(const std::vector<DicomInstanceInfo>& instances) | |
208 { | |
209 totalSize_ = 0; | |
210 | |
211 for (size_t i = 0; i < instances.size(); i++) | |
212 { | |
213 const std::string& id = instances[i].GetId(); | |
214 | |
215 assert(instances_.find(id) == instances_.end()); | |
216 instances_[id] = new Instance(instances[i]); | |
217 | |
218 totalSize_ += instances[i].GetSize(); | |
219 } | |
220 } | |
221 | |
222 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
223 void DownloadArea::CommitInternal(bool simulate) |
0 | 224 { |
225 boost::mutex::scoped_lock lock(mutex_); | |
226 | |
227 for (Instances::iterator it = instances_.begin(); | |
228 it != instances_.end(); ++it) | |
229 { | |
230 if (it->second != NULL) | |
231 { | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
232 it->second->Commit(simulate); |
0 | 233 delete it->second; |
234 it->second = NULL; | |
235 } | |
236 else | |
237 { | |
238 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
239 } | |
240 } | |
241 } | |
242 | |
243 | |
244 DownloadArea::DownloadArea(const TransferScheduler& scheduler) | |
245 { | |
246 std::vector<DicomInstanceInfo> instances; | |
247 scheduler.ListInstances(instances); | |
248 Setup(instances); | |
249 } | |
250 | |
251 | |
252 void DownloadArea::WriteBucket(const TransferBucket& bucket, | |
253 const void* data, | |
254 size_t size, | |
255 BucketCompression compression) | |
256 { | |
257 boost::mutex::scoped_lock lock(mutex_); | |
258 | |
259 switch (compression) | |
260 { | |
261 case BucketCompression_None: | |
262 WriteUncompressedBucket(bucket, data, size); | |
263 break; | |
264 | |
265 case BucketCompression_Gzip: | |
266 { | |
267 std::string uncompressed; | |
268 Orthanc::GzipCompressor compressor; | |
269 compressor.Uncompress(uncompressed, data, size); | |
270 WriteUncompressedBucket(bucket, uncompressed.c_str(), uncompressed.size()); | |
271 break; | |
272 } | |
273 | |
274 default: | |
275 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
276 } | |
277 } | |
278 | |
279 | |
280 void DownloadArea::WriteInstance(const std::string& instanceId, | |
281 const void* data, | |
282 size_t size) | |
283 { | |
284 std::string md5; | |
285 Orthanc::Toolbox::ComputeMD5(md5, data, size); | |
286 | |
287 { | |
288 boost::mutex::scoped_lock lock(mutex_); | |
289 | |
290 Instances::const_iterator it = instances_.find(instanceId); | |
291 if (it == instances_.end() || | |
292 it->second == NULL || | |
293 it->second->GetInfo().GetId() != instanceId || | |
294 it->second->GetInfo().GetSize() != size || | |
295 it->second->GetInfo().GetMD5() != md5) | |
296 { | |
297 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile); | |
298 } | |
299 else | |
300 { | |
301 it->second->WriteChunk(0, data, size); | |
302 } | |
303 } | |
304 } | |
305 | |
306 | |
307 void DownloadArea::CheckMD5() | |
308 { | |
309 LOG(INFO) << "Checking MD5 sum without committing (testing)"; | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
310 CommitInternal(true); |
0 | 311 } |
312 | |
313 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
314 void DownloadArea::Commit() |
0 | 315 { |
316 LOG(INFO) << "Importing transfered DICOM files from the temporary download area into Orthanc"; | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
317 CommitInternal(false); |
0 | 318 } |
319 } |