comparison Orthanc/Core/FileStorage/FilesystemStorage.cpp @ 145:d850500b8ca6

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Nov 2016 10:15:05 +0100
parents 145e654112d6
children 1a90184363f8
comparison
equal deleted inserted replaced
144:daf99382bc18 145:d850500b8ca6
84 root_ = root; 84 root_ = root;
85 85
86 Toolbox::MakeDirectory(root); 86 Toolbox::MakeDirectory(root);
87 } 87 }
88 88
89
90
91 static const char* GetDescriptionInternal(FileContentType content)
92 {
93 // This function is for logging only (internal use), a more
94 // fully-featured version is available in ServerEnumerations.cpp
95 switch (content)
96 {
97 case FileContentType_Unknown:
98 return "Unknown";
99
100 case FileContentType_Dicom:
101 return "DICOM";
102
103 case FileContentType_DicomAsJson:
104 return "JSON summary of DICOM";
105
106 default:
107 return "User-defined";
108 }
109 }
110
111
89 void FilesystemStorage::Create(const std::string& uuid, 112 void FilesystemStorage::Create(const std::string& uuid,
90 const void* content, 113 const void* content,
91 size_t size, 114 size_t size,
92 FileContentType /*type*/) 115 FileContentType type)
93 { 116 {
117 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
118 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
119
94 boost::filesystem::path path; 120 boost::filesystem::path path;
95 121
96 path = GetPath(uuid); 122 path = GetPath(uuid);
97 123
98 if (boost::filesystem::exists(path)) 124 if (boost::filesystem::exists(path))
115 { 141 {
116 throw OrthancException(ErrorCode_FileStorageCannotWrite); 142 throw OrthancException(ErrorCode_FileStorageCannotWrite);
117 } 143 }
118 } 144 }
119 145
120 boost::filesystem::ofstream f; 146 Toolbox::WriteFile(content, size, path.string());
121 f.open(path, std::ofstream::out | std::ios::binary);
122 if (!f.good())
123 {
124 throw OrthancException(ErrorCode_FileStorageCannotWrite);
125 }
126
127 if (size != 0)
128 {
129 f.write(static_cast<const char*>(content), size);
130 if (!f.good())
131 {
132 f.close();
133 throw OrthancException(ErrorCode_FileStorageCannotWrite);
134 }
135 }
136
137 f.close();
138 } 147 }
139 148
140 149
141 void FilesystemStorage::Read(std::string& content, 150 void FilesystemStorage::Read(std::string& content,
142 const std::string& uuid, 151 const std::string& uuid,
143 FileContentType /*type*/) 152 FileContentType type)
144 { 153 {
154 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
155 << "\" content type";
156
145 content.clear(); 157 content.clear();
146 Toolbox::ReadFile(content, GetPath(uuid).string()); 158 Toolbox::ReadFile(content, GetPath(uuid).string());
147 } 159 }
148 160
149 161
209 } 221 }
210 } 222 }
211 223
212 224
213 void FilesystemStorage::Remove(const std::string& uuid, 225 void FilesystemStorage::Remove(const std::string& uuid,
214 FileContentType /*type*/) 226 FileContentType type)
215 { 227 {
216 #if ORTHANC_ENABLE_GOOGLE_LOG == 1 228 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type);
217 LOG(INFO) << "Deleting file " << uuid;
218 #endif
219 229
220 namespace fs = boost::filesystem; 230 namespace fs = boost::filesystem;
221 231
222 fs::path p = GetPath(uuid); 232 fs::path p = GetPath(uuid);
223 233