static void GetArchive(RestApiGetCall& call) { ServerContext& context = OrthancRestApi::GetContext(call); std::string id = call.GetUriComponent("id", ""); bool isZip64 = IsZip64Required(context.GetIndex(), id); // Create a RAII for the temporary file to manage the ZIP file Toolbox::TemporaryFile tmp; { // Create a ZIP writer HierarchicalZipWriter writer(tmp.GetPath().c_str()); writer.SetZip64(isZip64); // Store the requested resource into the ZIP if (!ArchiveInternal(writer, context, id, resourceType, true)) { return; } } // Prepare the sending of the ZIP file FilesystemHttpSender sender(tmp.GetPath().c_str()); sender.SetContentType("application/zip"); sender.SetDownloadFilename(id + ".zip"); // Send the ZIP call.GetOutput().AnswerFile(sender); // The temporary file is automatically removed thanks to the RAII }
static void GetMediaArchive(RestApiGetCall& call) { ServerContext& context = OrthancRestApi::GetContext(call); std::string id = call.GetUriComponent("id", ""); bool isZip64 = IsZip64Required(context.GetIndex(), id); // Create a RAII for the temporary file to manage the ZIP file Toolbox::TemporaryFile tmp; { // Create a ZIP writer HierarchicalZipWriter writer(tmp.GetPath().c_str()); writer.SetZip64(isZip64); writer.OpenDirectory("IMAGES"); // Create the DICOMDIR writer DicomDirWriter dicomDir; // Retrieve the list of the instances std::list<std::string> instances; context.GetIndex().GetChildInstances(instances, id); size_t pos = 0; for (std::list<std::string>::const_iterator it = instances.begin(); it != instances.end(); ++it, ++pos) { // "DICOM restricts the filenames on DICOM media to 8 // characters (some systems wrongly use 8.3, but this does not // conform to the standard)." std::string filename = "IM" + boost::lexical_cast<std::string>(pos); writer.OpenFile(filename.c_str()); std::string dicom; context.ReadFile(dicom, *it, FileContentType_Dicom); writer.Write(dicom); ParsedDicomFile parsed(dicom); dicomDir.Add("IMAGES", filename, parsed); } // Add the DICOMDIR writer.CloseDirectory(); writer.OpenFile("DICOMDIR"); std::string s; dicomDir.Encode(s); writer.Write(s); } // Prepare the sending of the ZIP file FilesystemHttpSender sender(tmp.GetPath().c_str()); sender.SetContentType("application/zip"); sender.SetDownloadFilename(id + ".zip"); // Send the ZIP call.GetOutput().AnswerFile(sender); // The temporary file is automatically removed thanks to the RAII }
TEST(Toolbox, WriteFile) { std::string path; { Toolbox::TemporaryFile tmp; path = tmp.GetPath(); std::string s; s.append("Hello"); s.push_back('\0'); s.append("World"); ASSERT_EQ(11u, s.size()); Toolbox::WriteFile(s, path.c_str()); std::string t; Toolbox::ReadFile(t, path.c_str()); ASSERT_EQ(11u, t.size()); ASSERT_EQ(0, t[5]); ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size())); } std::string u; ASSERT_THROW(Toolbox::ReadFile(u, path.c_str()), OrthancException); }
DcmDicomDir& GetDicomDir() { if (dir_.get() == NULL) { dir_.reset(new DcmDicomDir(file_.GetPath().c_str(), fileSetId_.c_str())); } return *dir_; }