comparison OrthancServer/OrthancRestApi/OrthancRestArchive.cpp @ 2969:2c16c29b287d

preparing for asynchronous generation of archives
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Dec 2018 18:02:11 +0100
parents 10c610e80b15
children eea66afed0db
comparison
equal deleted inserted replaced
2968:e361df74639f 2969:2c16c29b287d
112 extended = defaultExtended; 112 extended = defaultExtended;
113 } 113 }
114 } 114 }
115 115
116 116
117 static void SubmitJob(RestApiCall& call, 117 static void SubmitJob(RestApiOutput& output,
118 ServerContext& context, 118 ServerContext& context,
119 std::auto_ptr<ArchiveJob>& job, 119 std::auto_ptr<ArchiveJob>& job,
120 bool synchronous,
120 const std::string& filename) 121 const std::string& filename)
121 { 122 {
122 if (job.get() == NULL) 123 if (job.get() == NULL)
123 { 124 {
124 throw OrthancException(ErrorCode_NullPointer); 125 throw OrthancException(ErrorCode_NullPointer);
125 } 126 }
126 127
127 job->SetDescription("REST API"); 128 job->SetDescription("REST API");
128 129
129 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile); 130 if (synchronous)
130 job->SetSynchronousTarget(tmp); 131 {
132 boost::shared_ptr<TemporaryFile> tmp(new TemporaryFile);
133 job->SetSynchronousTarget(tmp);
131 134
132 Json::Value publicContent; 135 Json::Value publicContent;
133 if (context.GetJobsEngine().GetRegistry().SubmitAndWait 136 if (context.GetJobsEngine().GetRegistry().SubmitAndWait
134 (publicContent, job.release(), 0 /* TODO priority */)) 137 (publicContent, job.release(), 0 /* TODO priority */))
135 { 138 {
136 // The archive is now created: Prepare the sending of the ZIP file 139 // The archive is now created: Prepare the sending of the ZIP file
137 FilesystemHttpSender sender(tmp->GetPath()); 140 FilesystemHttpSender sender(tmp->GetPath());
138 sender.SetContentType(MimeType_Gzip); 141 sender.SetContentType(MimeType_Gzip);
139 sender.SetContentFilename(filename); 142 sender.SetContentFilename(filename);
140 143
141 // Send the ZIP 144 // Send the ZIP
142 call.GetOutput().AnswerStream(sender); 145 output.AnswerStream(sender);
143 } 146 }
144 else 147 else
145 { 148 {
146 call.GetOutput().SignalError(HttpStatus_500_InternalServerError); 149 output.SignalError(HttpStatus_500_InternalServerError);
147 } 150 }
151 }
152 else
153 {
154 throw OrthancException(ErrorCode_NotImplemented);
155 }
148 } 156 }
149 157
150 158
151 static void CreateBatchArchive(RestApiPostCall& call) 159 static void CreateBatchArchive(RestApiPostCall& call)
152 { 160 {
158 bool synchronous, extended; 166 bool synchronous, extended;
159 GetJobParameters(synchronous, extended, body, false /* by default, not extended */); 167 GetJobParameters(synchronous, extended, body, false /* by default, not extended */);
160 168
161 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended)); 169 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, extended));
162 AddResourcesOfInterest(*job, body); 170 AddResourcesOfInterest(*job, body);
163 SubmitJob(call, context, job, "Archive.zip"); 171 SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip");
164 } 172 }
165 else 173 else
166 { 174 {
167 throw OrthancException(ErrorCode_BadFileFormat, 175 throw OrthancException(ErrorCode_BadFileFormat,
168 "Expected a list of resources to archive in the body"); 176 "Expected a list of resources to archive in the body");
181 bool synchronous, extended; 189 bool synchronous, extended;
182 GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED); 190 GetJobParameters(synchronous, extended, body, DEFAULT_EXTENDED);
183 191
184 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended)); 192 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, extended));
185 AddResourcesOfInterest(*job, body); 193 AddResourcesOfInterest(*job, body);
186 SubmitJob(call, context, job, "Archive.zip"); 194 SubmitJob(call.GetOutput(), context, job, synchronous, "Archive.zip");
187 } 195 }
188 else 196 else
189 { 197 {
190 throw OrthancException(ErrorCode_BadFileFormat, 198 throw OrthancException(ErrorCode_BadFileFormat,
191 "Expected a list of resources to archive in the body"); 199 "Expected a list of resources to archive in the body");
200 std::string id = call.GetUriComponent("id", ""); 208 std::string id = call.GetUriComponent("id", "");
201 209
202 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false)); 210 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, false, false));
203 job->AddResource(id); 211 job->AddResource(id);
204 212
205 SubmitJob(call, context, job, id + ".zip"); 213 SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip");
206 } 214 }
207 215
208 216
209 static void CreateMedia(RestApiGetCall& call) 217 static void CreateMedia(RestApiGetCall& call)
210 { 218 {
213 std::string id = call.GetUriComponent("id", ""); 221 std::string id = call.GetUriComponent("id", "");
214 222
215 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended"))); 223 std::auto_ptr<ArchiveJob> job(new ArchiveJob(context, true, call.HasArgument("extended")));
216 job->AddResource(id); 224 job->AddResource(id);
217 225
218 SubmitJob(call, context, job, id + ".zip"); 226 SubmitJob(call.GetOutput(), context, job, true /* synchronous */, id + ".zip");
219 } 227 }
220 228
221 229
222 void OrthancRestApi::RegisterArchive() 230 void OrthancRestApi::RegisterArchive()
223 { 231 {