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