Mercurial > hg > orthanc
comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 2908:9d277f8ad698
new enumeration: MimeType
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 Oct 2018 16:16:07 +0100 |
parents | ae20fccdd867 |
children | aeeb40a35ce1 |
comparison
equal
deleted
inserted
replaced
2907:0204af4ece6a | 2908:9d277f8ad698 |
---|---|
199 | 199 |
200 static void IsProtectedPatient(RestApiGetCall& call) | 200 static void IsProtectedPatient(RestApiGetCall& call) |
201 { | 201 { |
202 std::string publicId = call.GetUriComponent("id", ""); | 202 std::string publicId = call.GetUriComponent("id", ""); |
203 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); | 203 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); |
204 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", MIME_PLAIN_TEXT); | 204 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", MimeType_PlainText); |
205 } | 205 } |
206 | 206 |
207 | 207 |
208 static void SetPatientProtection(RestApiPutCall& call) | 208 static void SetPatientProtection(RestApiPutCall& call) |
209 { | 209 { |
216 body = Toolbox::StripSpaces(body); | 216 body = Toolbox::StripSpaces(body); |
217 | 217 |
218 if (body == "0") | 218 if (body == "0") |
219 { | 219 { |
220 context.GetIndex().SetProtectedPatient(publicId, false); | 220 context.GetIndex().SetProtectedPatient(publicId, false); |
221 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 221 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
222 } | 222 } |
223 else if (body == "1") | 223 else if (body == "1") |
224 { | 224 { |
225 context.GetIndex().SetProtectedPatient(publicId, true); | 225 context.GetIndex().SetProtectedPatient(publicId, true); |
226 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 226 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
227 } | 227 } |
228 else | 228 else |
229 { | 229 { |
230 // Bad request | 230 // Bad request |
231 } | 231 } |
254 | 254 |
255 std::string target; | 255 std::string target; |
256 call.BodyToString(target); | 256 call.BodyToString(target); |
257 SystemToolbox::WriteFile(dicom, target); | 257 SystemToolbox::WriteFile(dicom, target); |
258 | 258 |
259 call.GetOutput().AnswerBuffer("{}", MIME_JSON); | 259 call.GetOutput().AnswerBuffer("{}", MimeType_Json); |
260 } | 260 } |
261 | 261 |
262 | 262 |
263 template <DicomToJsonFormat format> | 263 template <DicomToJsonFormat format> |
264 static void GetInstanceTags(RestApiGetCall& call) | 264 static void GetInstanceTags(RestApiGetCall& call) |
282 // This path allows to avoid the JSON decoding if no | 282 // This path allows to avoid the JSON decoding if no |
283 // simplification is asked, and if no "ignore-length" argument | 283 // simplification is asked, and if no "ignore-length" argument |
284 // is present | 284 // is present |
285 std::string full; | 285 std::string full; |
286 context.ReadDicomAsJson(full, publicId); | 286 context.ReadDicomAsJson(full, publicId); |
287 call.GetOutput().AnswerBuffer(full, MIME_JSON); | 287 call.GetOutput().AnswerBuffer(full, MimeType_Json); |
288 } | 288 } |
289 } | 289 } |
290 | 290 |
291 | 291 |
292 static void GetInstanceTagsBis(RestApiGetCall& call) | 292 static void GetInstanceTagsBis(RestApiGetCall& call) |
338 { | 338 { |
339 private: | 339 private: |
340 std::auto_ptr<ImageAccessor>& image_; | 340 std::auto_ptr<ImageAccessor>& image_; |
341 ImageExtractionMode mode_; | 341 ImageExtractionMode mode_; |
342 bool invert_; | 342 bool invert_; |
343 std::string format_; | 343 MimeType format_; |
344 std::string answer_; | 344 std::string answer_; |
345 | 345 |
346 public: | 346 public: |
347 ImageToEncode(std::auto_ptr<ImageAccessor>& image, | 347 ImageToEncode(std::auto_ptr<ImageAccessor>& image, |
348 ImageExtractionMode mode, | 348 ImageExtractionMode mode, |
358 output.AnswerBuffer(answer_, format_); | 358 output.AnswerBuffer(answer_, format_); |
359 } | 359 } |
360 | 360 |
361 void EncodeUsingPng() | 361 void EncodeUsingPng() |
362 { | 362 { |
363 format_ = MIME_PNG; | 363 format_ = MimeType_Png; |
364 DicomImageDecoder::ExtractPngImage(answer_, image_, mode_, invert_); | 364 DicomImageDecoder::ExtractPngImage(answer_, image_, mode_, invert_); |
365 } | 365 } |
366 | 366 |
367 void EncodeUsingPam() | 367 void EncodeUsingPam() |
368 { | 368 { |
369 format_ = MIME_PAM; | 369 format_ = MimeType_Pam; |
370 DicomImageDecoder::ExtractPamImage(answer_, image_, mode_, invert_); | 370 DicomImageDecoder::ExtractPamImage(answer_, image_, mode_, invert_); |
371 } | 371 } |
372 | 372 |
373 void EncodeUsingJpeg(uint8_t quality) | 373 void EncodeUsingJpeg(uint8_t quality) |
374 { | 374 { |
375 format_ = MIME_JPEG; | 375 format_ = MimeType_Jpeg; |
376 DicomImageDecoder::ExtractJpegImage(answer_, image_, mode_, invert_, quality); | 376 DicomImageDecoder::ExtractJpegImage(answer_, image_, mode_, invert_, quality); |
377 } | 377 } |
378 }; | 378 }; |
379 | 379 |
380 class EncodePng : public HttpContentNegociation::IHandler | 380 class EncodePng : public HttpContentNegociation::IHandler |
594 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame)); | 594 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame)); |
595 | 595 |
596 std::string result; | 596 std::string result; |
597 decoded->ToMatlabString(result); | 597 decoded->ToMatlabString(result); |
598 | 598 |
599 call.GetOutput().AnswerBuffer(result, MIME_PLAIN_TEXT); | 599 call.GetOutput().AnswerBuffer(result, MimeType_PlainText); |
600 } | 600 } |
601 | 601 |
602 | 602 |
603 template <bool GzipCompression> | 603 template <bool GzipCompression> |
604 static void GetRawFrame(RestApiGetCall& call) | 604 static void GetRawFrame(RestApiGetCall& call) |
614 { | 614 { |
615 return; | 615 return; |
616 } | 616 } |
617 | 617 |
618 std::string publicId = call.GetUriComponent("id", ""); | 618 std::string publicId = call.GetUriComponent("id", ""); |
619 std::string raw, mime; | 619 std::string raw; |
620 MimeType mime; | |
620 | 621 |
621 { | 622 { |
622 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); | 623 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); |
623 locker.GetDicom().GetRawFrame(raw, mime, frame); | 624 locker.GetDicom().GetRawFrame(raw, mime, frame); |
624 } | 625 } |
626 if (GzipCompression) | 627 if (GzipCompression) |
627 { | 628 { |
628 GzipCompressor gzip; | 629 GzipCompressor gzip; |
629 std::string compressed; | 630 std::string compressed; |
630 gzip.Compress(compressed, raw.empty() ? NULL : raw.c_str(), raw.size()); | 631 gzip.Compress(compressed, raw.empty() ? NULL : raw.c_str(), raw.size()); |
631 call.GetOutput().AnswerBuffer(compressed, "application/gzip"); | 632 call.GetOutput().AnswerBuffer(compressed, MimeType_Gzip); |
632 } | 633 } |
633 else | 634 else |
634 { | 635 { |
635 call.GetOutput().AnswerBuffer(raw, mime); | 636 call.GetOutput().AnswerBuffer(raw, mime); |
636 } | 637 } |
707 MetadataType metadata = StringToMetadata(name); | 708 MetadataType metadata = StringToMetadata(name); |
708 | 709 |
709 std::string value; | 710 std::string value; |
710 if (OrthancRestApi::GetIndex(call).LookupMetadata(value, publicId, metadata)) | 711 if (OrthancRestApi::GetIndex(call).LookupMetadata(value, publicId, metadata)) |
711 { | 712 { |
712 call.GetOutput().AnswerBuffer(value, MIME_PLAIN_TEXT); | 713 call.GetOutput().AnswerBuffer(value, MimeType_PlainText); |
713 } | 714 } |
714 } | 715 } |
715 | 716 |
716 | 717 |
717 static void DeleteMetadata(RestApiDeleteCall& call) | 718 static void DeleteMetadata(RestApiDeleteCall& call) |
723 MetadataType metadata = StringToMetadata(name); | 724 MetadataType metadata = StringToMetadata(name); |
724 | 725 |
725 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata | 726 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata |
726 { | 727 { |
727 OrthancRestApi::GetIndex(call).DeleteMetadata(publicId, metadata); | 728 OrthancRestApi::GetIndex(call).DeleteMetadata(publicId, metadata); |
728 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 729 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
729 } | 730 } |
730 else | 731 else |
731 { | 732 { |
732 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 733 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
733 } | 734 } |
747 | 748 |
748 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata | 749 if (IsUserMetadata(metadata)) // It is forbidden to modify internal metadata |
749 { | 750 { |
750 // It is forbidden to modify internal metadata | 751 // It is forbidden to modify internal metadata |
751 OrthancRestApi::GetIndex(call).SetMetadata(publicId, metadata, value); | 752 OrthancRestApi::GetIndex(call).SetMetadata(publicId, metadata, value); |
752 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 753 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
753 } | 754 } |
754 else | 755 else |
755 { | 756 { |
756 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 757 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
757 } | 758 } |
848 else | 849 else |
849 { | 850 { |
850 // Return the raw data (possibly compressed), as stored on the filesystem | 851 // Return the raw data (possibly compressed), as stored on the filesystem |
851 std::string content; | 852 std::string content; |
852 context.ReadAttachment(content, publicId, type, false); | 853 context.ReadAttachment(content, publicId, type, false); |
853 call.GetOutput().AnswerBuffer(content, MIME_BINARY); | 854 call.GetOutput().AnswerBuffer(content, MimeType_Binary); |
854 } | 855 } |
855 } | 856 } |
856 | 857 |
857 | 858 |
858 static void GetAttachmentSize(RestApiGetCall& call) | 859 static void GetAttachmentSize(RestApiGetCall& call) |
859 { | 860 { |
860 FileInfo info; | 861 FileInfo info; |
861 if (GetAttachmentInfo(info, call)) | 862 if (GetAttachmentInfo(info, call)) |
862 { | 863 { |
863 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MIME_PLAIN_TEXT); | 864 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MimeType_PlainText); |
864 } | 865 } |
865 } | 866 } |
866 | 867 |
867 | 868 |
868 static void GetAttachmentCompressedSize(RestApiGetCall& call) | 869 static void GetAttachmentCompressedSize(RestApiGetCall& call) |
869 { | 870 { |
870 FileInfo info; | 871 FileInfo info; |
871 if (GetAttachmentInfo(info, call)) | 872 if (GetAttachmentInfo(info, call)) |
872 { | 873 { |
873 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MIME_PLAIN_TEXT); | 874 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MimeType_PlainText); |
874 } | 875 } |
875 } | 876 } |
876 | 877 |
877 | 878 |
878 static void GetAttachmentMD5(RestApiGetCall& call) | 879 static void GetAttachmentMD5(RestApiGetCall& call) |
879 { | 880 { |
880 FileInfo info; | 881 FileInfo info; |
881 if (GetAttachmentInfo(info, call) && | 882 if (GetAttachmentInfo(info, call) && |
882 info.GetUncompressedMD5() != "") | 883 info.GetUncompressedMD5() != "") |
883 { | 884 { |
884 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), MIME_PLAIN_TEXT); | 885 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedMD5()), MimeType_PlainText); |
885 } | 886 } |
886 } | 887 } |
887 | 888 |
888 | 889 |
889 static void GetAttachmentCompressedMD5(RestApiGetCall& call) | 890 static void GetAttachmentCompressedMD5(RestApiGetCall& call) |
890 { | 891 { |
891 FileInfo info; | 892 FileInfo info; |
892 if (GetAttachmentInfo(info, call) && | 893 if (GetAttachmentInfo(info, call) && |
893 info.GetCompressedMD5() != "") | 894 info.GetCompressedMD5() != "") |
894 { | 895 { |
895 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), MIME_PLAIN_TEXT); | 896 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), MimeType_PlainText); |
896 } | 897 } |
897 } | 898 } |
898 | 899 |
899 | 900 |
900 static void VerifyAttachment(RestApiPostCall& call) | 901 static void VerifyAttachment(RestApiPostCall& call) |
940 } | 941 } |
941 | 942 |
942 if (ok) | 943 if (ok) |
943 { | 944 { |
944 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5"; | 945 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5"; |
945 call.GetOutput().AnswerBuffer("{}", MIME_JSON); | 946 call.GetOutput().AnswerBuffer("{}", MimeType_Json); |
946 } | 947 } |
947 else | 948 else |
948 { | 949 { |
949 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; | 950 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; |
950 } | 951 } |
961 | 962 |
962 FileContentType contentType = StringToContentType(name); | 963 FileContentType contentType = StringToContentType(name); |
963 if (IsUserContentType(contentType) && // It is forbidden to modify internal attachments | 964 if (IsUserContentType(contentType) && // It is forbidden to modify internal attachments |
964 context.AddAttachment(publicId, StringToContentType(name), call.GetBodyData(), call.GetBodySize())) | 965 context.AddAttachment(publicId, StringToContentType(name), call.GetBodyData(), call.GetBodySize())) |
965 { | 966 { |
966 call.GetOutput().AnswerBuffer("{}", MIME_JSON); | 967 call.GetOutput().AnswerBuffer("{}", MimeType_Json); |
967 } | 968 } |
968 else | 969 else |
969 { | 970 { |
970 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 971 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
971 } | 972 } |
999 } | 1000 } |
1000 | 1001 |
1001 if (allowed) | 1002 if (allowed) |
1002 { | 1003 { |
1003 OrthancRestApi::GetIndex(call).DeleteAttachment(publicId, contentType); | 1004 OrthancRestApi::GetIndex(call).DeleteAttachment(publicId, contentType); |
1004 call.GetOutput().AnswerBuffer("{}", MIME_JSON); | 1005 call.GetOutput().AnswerBuffer("{}", MimeType_Json); |
1005 } | 1006 } |
1006 else | 1007 else |
1007 { | 1008 { |
1008 call.GetOutput().SignalError(HttpStatus_403_Forbidden); | 1009 call.GetOutput().SignalError(HttpStatus_403_Forbidden); |
1009 } | 1010 } |
1018 std::string publicId = call.GetUriComponent("id", ""); | 1019 std::string publicId = call.GetUriComponent("id", ""); |
1019 std::string name = call.GetUriComponent("name", ""); | 1020 std::string name = call.GetUriComponent("name", ""); |
1020 FileContentType contentType = StringToContentType(name); | 1021 FileContentType contentType = StringToContentType(name); |
1021 | 1022 |
1022 OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression); | 1023 OrthancRestApi::GetContext(call).ChangeAttachmentCompression(publicId, contentType, compression); |
1023 call.GetOutput().AnswerBuffer("{}", MIME_JSON); | 1024 call.GetOutput().AnswerBuffer("{}", MimeType_Json); |
1024 } | 1025 } |
1025 | 1026 |
1026 | 1027 |
1027 static void IsAttachmentCompressed(RestApiGetCall& call) | 1028 static void IsAttachmentCompressed(RestApiGetCall& call) |
1028 { | 1029 { |
1029 FileInfo info; | 1030 FileInfo info; |
1030 if (GetAttachmentInfo(info, call)) | 1031 if (GetAttachmentInfo(info, call)) |
1031 { | 1032 { |
1032 std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1"; | 1033 std::string answer = (info.GetCompressionType() == CompressionType_None) ? "0" : "1"; |
1033 call.GetOutput().AnswerBuffer(answer, MIME_PLAIN_TEXT); | 1034 call.GetOutput().AnswerBuffer(answer, MimeType_PlainText); |
1034 } | 1035 } |
1035 } | 1036 } |
1036 | 1037 |
1037 | 1038 |
1038 // Raw access to the DICOM tags of an instance ------------------------------ | 1039 // Raw access to the DICOM tags of an instance ------------------------------ |
1465 std::string pdf; | 1466 std::string pdf; |
1466 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); | 1467 ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); |
1467 | 1468 |
1468 if (locker.GetDicom().ExtractPdf(pdf)) | 1469 if (locker.GetDicom().ExtractPdf(pdf)) |
1469 { | 1470 { |
1470 call.GetOutput().AnswerBuffer(pdf, MIME_PDF); | 1471 call.GetOutput().AnswerBuffer(pdf, MimeType_Pdf); |
1471 return; | 1472 return; |
1472 } | 1473 } |
1473 } | 1474 } |
1474 | 1475 |
1475 | 1476 |
1527 { | 1528 { |
1528 index.DeleteAttachment(*instance, FileContentType_DicomAsJson); | 1529 index.DeleteAttachment(*instance, FileContentType_DicomAsJson); |
1529 } | 1530 } |
1530 } | 1531 } |
1531 | 1532 |
1532 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 1533 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
1533 } | 1534 } |
1534 | 1535 |
1535 | 1536 |
1536 template <enum ResourceType type> | 1537 template <enum ResourceType type> |
1537 static void ReconstructResource(RestApiPostCall& call) | 1538 static void ReconstructResource(RestApiPostCall& call) |
1538 { | 1539 { |
1539 ServerContext& context = OrthancRestApi::GetContext(call); | 1540 ServerContext& context = OrthancRestApi::GetContext(call); |
1540 ServerToolbox::ReconstructResource(context, call.GetUriComponent("id", "")); | 1541 ServerToolbox::ReconstructResource(context, call.GetUriComponent("id", "")); |
1541 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 1542 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
1542 } | 1543 } |
1543 | 1544 |
1544 | 1545 |
1545 static void ReconstructAllResources(RestApiPostCall& call) | 1546 static void ReconstructAllResources(RestApiPostCall& call) |
1546 { | 1547 { |
1553 study = studies.begin(); study != studies.end(); ++study) | 1554 study = studies.begin(); study != studies.end(); ++study) |
1554 { | 1555 { |
1555 ServerToolbox::ReconstructResource(context, *study); | 1556 ServerToolbox::ReconstructResource(context, *study); |
1556 } | 1557 } |
1557 | 1558 |
1558 call.GetOutput().AnswerBuffer("", MIME_PLAIN_TEXT); | 1559 call.GetOutput().AnswerBuffer("", MimeType_PlainText); |
1559 } | 1560 } |
1560 | 1561 |
1561 | 1562 |
1562 void OrthancRestApi::RegisterResources() | 1563 void OrthancRestApi::RegisterResources() |
1563 { | 1564 { |