annotate Framework/DownloadArea.cpp @ 0:95226b754d9e

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