Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestArchive.cpp @ 2970:eea66afed0db
remove redundancies
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 06 Dec 2018 10:10:58 +0100 |
parents | 2c16c29b287d |
children | cb5d75143da0 |
comparison
equal
deleted
inserted
replaced
2969:2c16c29b287d | 2970:eea66afed0db |
---|---|
94 } | 94 } |
95 | 95 |
96 | 96 |
97 static void GetJobParameters(bool& synchronous, /* out */ | 97 static void GetJobParameters(bool& synchronous, /* out */ |
98 bool& extended, /* out */ | 98 bool& extended, /* out */ |
99 int& priority, /* out */ | |
99 const Json::Value& body, /* in */ | 100 const Json::Value& body, /* in */ |
100 const bool defaultExtended /* in */) | 101 const bool defaultExtended /* in */) |
101 { | 102 { |
102 synchronous = OrthancRestApi::IsSynchronousJobRequest | 103 synchronous = OrthancRestApi::IsSynchronousJobRequest |
103 (true /* synchronous by default */, body); | 104 (true /* synchronous by default */, body); |
105 | |
106 priority = OrthancRestApi::GetJobRequestPriority(body); | |
104 | 107 |
105 if (body.type() == Json::objectValue && | 108 if (body.type() == Json::objectValue && |
106 body.isMember(KEY_EXTENDED)) | 109 body.isMember(KEY_EXTENDED)) |
107 { | 110 { |
108 extended = SerializationToolbox::ReadBoolean(body, KEY_EXTENDED); | 111 extended = SerializationToolbox::ReadBoolean(body, KEY_EXTENDED); |
115 | 118 |
116 | 119 |
117 static void SubmitJob(RestApiOutput& output, | 120 static void SubmitJob(RestApiOutput& output, |
118 ServerContext& context, | 121 ServerContext& context, |
119 std::auto_ptr<ArchiveJob>& job, | 122 std::auto_ptr<ArchiveJob>& job, |
123 int priority, | |
120 bool synchronous, | 124 bool synchronous, |
121 const std::string& filename) | 125 const std::string& filename) |
122 { | 126 { |
123 if (job.get() == NULL) | 127 if (job.get() == NULL) |
124 { | 128 { |
132 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); | 136 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); |
133 job->SetSynchronousTarget(tmp); | 137 job->SetSynchronousTarget(tmp); |
134 | 138 |
135 Json::Value publicContent; | 139 Json::Value publicContent; |
136 if (context.GetJobsEngine().GetRegistry().SubmitAndWait | 140 if (context.GetJobsEngine().GetRegistry().SubmitAndWait |
137 (publicContent, job.release(), 0 /* TODO priority */)) | 141 (publicContent, job.release(), priority)) |
138 { | 142 { |
139 // The archive is now created: Prepare the sending of the ZIP file | 143 // The archive is now created: Prepare the sending of the ZIP file |
140 FilesystemHttpSender sender(tmp->GetPath()); | 144 FilesystemHttpSender sender(tmp->GetPath()); |
141 sender.SetContentType(MimeType_Gzip); | 145 sender.SetContentType(MimeType_Gzip); |
142 sender.SetContentFilename(filename); | 146 sender.SetContentFilename(filename); |
154 throw OrthancException(ErrorCode_NotImplemented); | 158 throw OrthancException(ErrorCode_NotImplemented); |
155 } | 159 } |
156 } | 160 } |
157 | 161 |
158 | 162 |
159 static void CreateBatchArchive(RestApiPostCall& call) | 163 template <bool IS_MEDIA, |
164 bool DEFAULT_IS_EXTENDED /* only makes sense for media (i.e. not ZIP archives) */ > | |
165 static void CreateBatch(RestApiPostCall& call) | |
160 { | 166 { |
161 ServerContext& context = OrthancRestApi::GetContext(call); | 167 ServerContext& context = OrthancRestApi::GetContext(call); |
162 | 168 |
163 Json::Value body; | 169 Json::Value body; |
164 if (call.ParseJsonRequest(body)) | 170 if (call.ParseJsonRequest(body)) |
165 { | 171 { |
166 bool synchronous, extended; | 172 bool synchronous, extended; |
167 GetJobParameters(synchronous, extended, body, false /* by default, not extended */); | 173 int priority; |
174 GetJobParameters(synchronous, extended, priority, body, DEFAULT_IS_EXTENDED); | |
168 | 175 |
169 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended)); | 176 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended)); |
170 AddResourcesOfInterest(*job, body); | 177 AddResourcesOfInterest(*job, body); |
171 SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip"); | 178 SubmitJob(call.GetOutput(), context, job, priority, synchronous, "Archive.zip"); |
172 } | 179 } |
173 else | 180 else |
174 { | 181 { |
175 throw OrthancException(ErrorCode_BadFileFormat, | 182 throw OrthancException(ErrorCode_BadFileFormat, |
176 "Expected a list of resources to archive in the body"); | 183 "Expected a list of resources to archive in the body"); |
177 } | 184 } |
178 } | 185 } |
179 | 186 |
180 | 187 |
181 template <bool DEFAULT_EXTENDED> | 188 template <bool IS_MEDIA, |
182 static void CreateBatchMedia(RestApiPostCall& call) | 189 bool DEFAULT_IS_EXTENDED /* only makes sense for media (i.e. not ZIP archives) */ > |
190 static void CreateSingleGet(RestApiGetCall& call) | |
183 { | 191 { |
184 ServerContext& context = OrthancRestApi::GetContext(call); | 192 ServerContext& context = OrthancRestApi::GetContext(call); |
185 | 193 |
186 Json::Value body; | |
187 if (call.ParseJsonRequest(body)) | |
188 { | |
189 bool synchronous, extended; | |
190 GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED); | |
191 | |
192 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended)); | |
193 AddResourcesOfInterest(*job, body); | |
194 SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip"); | |
195 } | |
196 else | |
197 { | |
198 throw OrthancException(ErrorCode_BadFileFormat, | |
199 "Expected a list of resources to archive in the body"); | |
200 } | |
201 } | |
202 | |
203 | |
204 static void CreateArchive(RestApiGetCall& call) | |
205 { | |
206 ServerContext& context = OrthancRestApi::GetContext(call); | |
207 | |
208 std::string id = call.GetUriComponent("id", ""); | 194 std::string id = call.GetUriComponent("id", ""); |
209 | 195 |
210 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false)); | 196 bool extended; |
197 if (IS_MEDIA) | |
198 { | |
199 extended = call.HasArgument("extended"); | |
200 } | |
201 else | |
202 { | |
203 extended = false; | |
204 } | |
205 | |
206 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, IS_MEDIA, extended)); | |
211 job->AddResource(id); | 207 job->AddResource(id); |
212 | 208 |
213 SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip"); | 209 SubmitJob(call.GetOutput(), context, job, 0 /* priority */, |
214 } | 210 true /* synchronous */, id + ".zip"); |
215 | |
216 | |
217 static void CreateMedia(RestApiGetCall& call) | |
218 { | |
219 ServerContext& context = OrthancRestApi::GetContext(call); | |
220 | |
221 std::string id = call.GetUriComponent("id", ""); | |
222 | |
223 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended"))); | |
224 job->AddResource(id); | |
225 | |
226 SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip"); | |
227 } | 211 } |
228 | 212 |
229 | 213 |
230 void OrthancRestApi::RegisterArchive() | 214 void OrthancRestApi::RegisterArchive() |
231 { | 215 { |
232 Register("/patients/{id}/archive", CreateArchive); | 216 Register("/patients/{id}/archive", |
233 Register("/studies/{id}/archive", CreateArchive); | 217 CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); |
234 Register("/series/{id}/archive", CreateArchive); | 218 Register("/studies/{id}/archive", |
235 | 219 CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); |
236 Register("/patients/{id}/media", CreateMedia); | 220 Register("/series/{id}/archive", |
237 Register("/studies/{id}/media", CreateMedia); | 221 CreateSingleGet<false /* ZIP */, false /* extended makes no sense in ZIP */>); |
238 Register("/series/{id}/media", CreateMedia); | 222 |
239 | 223 Register("/patients/{id}/media", |
240 Register("/tools/create-archive", CreateBatchArchive); | 224 CreateSingleGet<true /* media */, false /* not extended by default */>); |
241 Register("/tools/create-media", CreateBatchMedia<false>); | 225 Register("/studies/{id}/media", |
242 Register("/tools/create-media-extended", CreateBatchMedia<true>); | 226 CreateSingleGet<true /* media */, false /* not extended by default */>); |
227 Register("/series/{id}/media", | |
228 CreateSingleGet<true /* media */, false /* not extended by default */>); | |
229 | |
230 Register("/tools/create-archive", | |
231 CreateBatch<false /* ZIP */, false /* extended makes no sense in ZIP */>); | |
232 Register("/tools/create-media", | |
233 CreateBatch<true /* media */, false /* not extended by default */>); | |
234 Register("/tools/create-media-extended", | |
235 CreateBatch<true /* media */, true /* extended by default */>); | |
243 } | 236 } |
244 } | 237 } |