Mercurial > hg > orthanc-transfers
annotate Framework/DownloadArea.cpp @ 72:d60d1a3a7357 OrthancTransfers-1.4
fix md5
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 26 Mar 2024 10:16:15 +0100 |
parents | 1256194e1c08 |
children | 1e396fb509ca |
rev | line source |
---|---|
0 | 1 /** |
2 * Transfers accelerator plugin for Orthanc | |
33
44a0430d7899
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
3 * Copyright (C) 2018-2021 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 { | |
40
1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_CannotWriteFile, std::string("Unable to write to ") + f.GetPath()); |
0 | 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 { | |
40
1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, "WriteChunk out of bounds"); |
0 | 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 { | |
40
1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource, "Unknown instance"); |
0 | 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 { | |
40
1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
174 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, |
1256194e1c08
sync orthanc + sdk 1.5.0 + added more info in error logs
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
175 "WriteUncompressedBucket: " + boost::lexical_cast<std::string>(size) + " != " + boost::lexical_cast<std::string>(bucket.GetTotalSize())); |
0 | 176 } |
177 | |
178 if (size == 0) | |
179 { | |
180 return; | |
181 } | |
182 | |
183 size_t pos = 0; | |
184 | |
185 for (size_t i = 0; i < bucket.GetChunksCount(); i++) | |
186 { | |
187 size_t chunkSize = bucket.GetChunkSize(i); | |
188 size_t offset = bucket.GetChunkOffset(i); | |
189 | |
190 if (pos + chunkSize > size) | |
191 { | |
192 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
193 } | |
194 | |
195 Instance& instance = LookupInstance(bucket.GetChunkInstanceId(i)); | |
196 instance.WriteChunk(offset, reinterpret_cast<const char*>(data) + pos, chunkSize); | |
197 | |
198 pos += chunkSize; | |
199 } | |
200 | |
201 if (pos != size) | |
202 { | |
203 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
204 } | |
205 } | |
206 | |
207 | |
208 void DownloadArea::Setup(const std::vector<DicomInstanceInfo>& instances) | |
209 { | |
210 totalSize_ = 0; | |
211 | |
212 for (size_t i = 0; i < instances.size(); i++) | |
213 { | |
214 const std::string& id = instances[i].GetId(); | |
215 | |
216 assert(instances_.find(id) == instances_.end()); | |
217 instances_[id] = new Instance(instances[i]); | |
218 | |
219 totalSize_ += instances[i].GetSize(); | |
220 } | |
221 } | |
222 | |
223 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
224 void DownloadArea::CommitInternal(bool simulate) |
0 | 225 { |
226 boost::mutex::scoped_lock lock(mutex_); | |
227 | |
228 for (Instances::iterator it = instances_.begin(); | |
229 it != instances_.end(); ++it) | |
230 { | |
231 if (it->second != NULL) | |
232 { | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
233 it->second->Commit(simulate); |
0 | 234 delete it->second; |
235 it->second = NULL; | |
236 } | |
237 else | |
238 { | |
239 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
240 } | |
241 } | |
242 } | |
243 | |
244 | |
245 DownloadArea::DownloadArea(const TransferScheduler& scheduler) | |
246 { | |
247 std::vector<DicomInstanceInfo> instances; | |
248 scheduler.ListInstances(instances); | |
249 Setup(instances); | |
250 } | |
251 | |
252 | |
253 void DownloadArea::WriteBucket(const TransferBucket& bucket, | |
254 const void* data, | |
255 size_t size, | |
256 BucketCompression compression) | |
257 { | |
258 boost::mutex::scoped_lock lock(mutex_); | |
259 | |
260 switch (compression) | |
261 { | |
262 case BucketCompression_None: | |
263 WriteUncompressedBucket(bucket, data, size); | |
264 break; | |
265 | |
266 case BucketCompression_Gzip: | |
267 { | |
268 std::string uncompressed; | |
269 Orthanc::GzipCompressor compressor; | |
270 compressor.Uncompress(uncompressed, data, size); | |
271 WriteUncompressedBucket(bucket, uncompressed.c_str(), uncompressed.size()); | |
272 break; | |
273 } | |
274 | |
275 default: | |
276 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
277 } | |
278 } | |
279 | |
280 | |
281 void DownloadArea::WriteInstance(const std::string& instanceId, | |
282 const void* data, | |
283 size_t size) | |
284 { | |
285 std::string md5; | |
286 Orthanc::Toolbox::ComputeMD5(md5, data, size); | |
287 | |
288 { | |
289 boost::mutex::scoped_lock lock(mutex_); | |
290 | |
291 Instances::const_iterator it = instances_.find(instanceId); | |
292 if (it == instances_.end() || | |
293 it->second == NULL || | |
294 it->second->GetInfo().GetId() != instanceId || | |
295 it->second->GetInfo().GetSize() != size || | |
296 it->second->GetInfo().GetMD5() != md5) | |
297 { | |
298 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile); | |
299 } | |
300 else | |
301 { | |
302 it->second->WriteChunk(0, data, size); | |
303 } | |
304 } | |
305 } | |
306 | |
307 | |
308 void DownloadArea::CheckMD5() | |
309 { | |
310 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
|
311 CommitInternal(true); |
0 | 312 } |
313 | |
314 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
315 void DownloadArea::Commit() |
0 | 316 { |
317 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
|
318 CommitInternal(false); |
0 | 319 } |
320 } |