获取文件路径
{
boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const
{
namespace fs = boost::filesystem;
if (!Toolbox::IsUuid(uuid))
{
throw OrthancException(ErrorCode_ParameterOutOfRange);
}
fs::path path = root_;
path /= std::string(&uuid[0], &uuid[2]);
path /= std::string(&uuid[2], &uuid[4]);
path /= uuid;
#if BOOST_HAS_FILESYSTEM_V3 == 1
path.make_preferred();
#endif
return path;
}
/**
* Orthanc - A Lightweight, RESTful DICOM Store
* Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
* Department, University Hospital of Liege, Belgium
* Copyright (C) 2017-2022 Osimis S.A., Belgium
* Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
**/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <sstream>
#include "../PrecompiledHeaders.h"
#include "FilesystemStorage.h"
// http://stackoverflow.com/questions/1576272/storing-large-number-of-files-in-file-system
// http://stackoverflow.com/questions/446358/storing-a-large-number-of-images
#include "../Logging.h"
#include "../OrthancException.h"
#include "../StringMemoryBuffer.h"
#include "../SystemToolbox.h"
#include "../Toolbox.h"
#include "../../../../OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h"
#include <boost/filesystem/fstream.hpp>
#include "../../../OrthancServer/UnitTestsSources/ServerIndexTests.cpp"
static std::string ToString(const boost::filesystem::path& p)
{
#if BOOST_HAS_FILESYSTEM_V3 == 1
return p.filename().string();
#else
return p.filename();
#endif
}
namespace Orthanc
{
boost::filesystem::path FilesystemStorage::GetPath(const std::string& uuid) const
{
namespace fs = boost::filesystem;
if (!Toolbox::IsUuid(uuid))
{
throw OrthancException(ErrorCode_ParameterOutOfRange);
}
fs::path path = root_;
std::time_t const now_c = std::time(0);
std::stringstream ss;
ss << std::put_time(std::localtime(&now_c), "%F");
std::unique_ptr<TestDatabaseListener> listener_;
std::unique_ptr<SQLiteDatabaseWrapper> index_;
std::unique_ptr<SQLiteDatabaseWrapper::UnitTestsTransaction> transaction_;
listener_.reset(new TestDatabaseListener);
index_.reset(new SQLiteDatabaseWrapper);
index_->Open();
transaction_.reset(dynamic_cast<SQLiteDatabaseWrapper::UnitTestsTransaction*>(
index_->StartTransaction(TransactionType_ReadWrite, *listener_)));
path /= ss.str();
path /= std::string(&uuid[0], &uuid[2]);
path /= std::string(&uuid[2], &uuid[4]);
path /= uuid;
#if BOOST_HAS_FILESYSTEM_V3 == 1
path.make_preferred();
#endif
return path;
}
void FilesystemStorage::Setup(const std::string& root)
{
//root_ = boost::filesystem::absolute(root).string();
root_ = root;
SystemToolbox::MakeDirectory(root);
}
FilesystemStorage::FilesystemStorage(const std::string &root) :
fsyncOnWrite_(false)
{
Setup(root);
}
FilesystemStorage::FilesystemStorage(const std::string &root,
bool fsyncOnWrite) :
fsyncOnWrite_(fsyncOnWrite)
{
Setup(root);
}
static const char* GetDescriptionInternal(FileContentType content)
{
// This function is for logging only (internal use), a more
// fully-featured version is available in ServerEnumerations.cpp
switch (content)
{
case FileContentType_Unknown:
return "Unknown";
case FileContentType_Dicom:
return "DICOM";
case FileContentType_DicomAsJson:
return "JSON summary of DICOM";
case FileContentType_DicomUntilPixelData:
return "DICOM until pixel data";
default:
return "User-defined";
}
}
void FilesystemStorage::Create(const std::string& uuid,
const void* content,
size_t size,
FileContentType type)
{
LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
<< "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
boost::filesystem::path path;
path = GetPath(uuid);
if (boost::filesystem::exists(path))
{
// Extremely unlikely case: This Uuid has already been created
// in the past.
throw OrthancException(ErrorCode_InternalError);
}
if (boost::filesystem::exists(path.parent_path()))
{
if (!boost::filesystem::is_directory(path.parent_path()))
{
throw OrthancException(ErrorCode_DirectoryOverFile);
}
}
else
{
if (!boost::filesystem::create_directories(path.parent_path()))
{
throw OrthancException(ErrorCode_FileStorageCannotWrite);
}
}
SystemToolbox::WriteFile(content, size, path.string(), fsyncOnWrite_);
}
IMemoryBuffer* FilesystemStorage::Read(const std::string& uuid,
FileContentType type)
{
LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
<< "\" content type";
std::string content;
SystemToolbox::ReadFile(content, GetPath(uuid).string());
return StringMemoryBuffer::CreateFromSwap(content);
}
IMemoryBuffer* FilesystemStorage::ReadRange(const std::string& uuid,
FileContentType type,
uint64_t start /* inclusive */,
uint64_t end /* exclusive */)
{
LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" << GetDescriptionInternal(type)
<< "\" content type (range from " << start << " to " << end << ")";
std::string content;
SystemToolbox::ReadFileRange(
content, GetPath(uuid).string(), start, end, true /* throw if overflow */);
return StringMemoryBuffer::CreateFromSwap(content);
}
bool FilesystemStorage::HasReadRange() const
{
return true;
}
uintmax_t FilesystemStorage::GetSize(const std::string& uuid) const
{
boost::filesystem::path path = GetPath(uuid);
return boost::filesystem::file_size(path);
}
void FilesystemStorage::ListAllFiles(std::set<std::string>& result) const
{
namespace fs = boost::filesystem;
result.clear();
if (fs::exists(root_) && fs::is_directory(root_))
{
for (fs::recursive_directory_iterator current(root_), end; current != end ; ++current)
{
if (SystemToolbox::IsRegularFile(current->path().string()))
{
try
{
fs::path d = current->path();
std::string uuid = ToString(d);
if (Toolbox::IsUuid(uuid))
{
fs::path p0 = d.parent_path().parent_path().parent_path();
std::string p1 = ToString(d.parent_path().parent_path());
std::string p2 = ToString(d.parent_path());
if (p1.length() == 2 &&
p2.length() == 2 &&
p1 == uuid.substr(0, 2) &&
p2 == uuid.substr(2, 2) &&
p0 == root_)
{
result.insert(uuid);
}
}
}
catch (fs::filesystem_error&)
{
}
}
}
}
}
void FilesystemStorage::Clear()
{
namespace fs = boost::filesystem;
typedef std::set<std::string> List;
List result;
ListAllFiles(result);
for (List::const_iterator it = result.begin(); it != result.end(); ++it)
{
Remove(*it, FileContentType_Unknown /*ignored in this class*/);
}
}
void FilesystemStorage::Remove(const std::string& uuid,
FileContentType type)
{
LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type);
namespace fs = boost::filesystem;
fs::path p = GetPath(uuid);
try
{
fs::remove(p);
}
catch (...)
{
// Ignore the error
}
// Remove the two parent directories, ignoring the error code if
// these directories are not empty
try
{
#if BOOST_HAS_FILESYSTEM_V3 == 1
boost::system::error_code err;
fs::remove(p.parent_path(), err);
fs::remove(p.parent_path().parent_path(), err);
#else
fs::remove(p.parent_path());
fs::remove(p.parent_path().parent_path());
#endif
}
catch (...)
{
// Ignore the error
}
}
uintmax_t FilesystemStorage::GetCapacity() const
{
return boost::filesystem::space(root_).capacity;
}
uintmax_t FilesystemStorage::GetAvailableSpace() const
{
return boost::filesystem::space(root_).available;
}
#if ORTHANC_BUILDING_FRAMEWORK_LIBRARY == 1
FilesystemStorage::FilesystemStorage(std::string root) :
fsyncOnWrite_(false)
{
Setup(root);
}
#endif
#if ORTHANC_BUILDING_FRAMEWORK_LIBRARY == 1
void FilesystemStorage::Read(std::string& content,
const std::string& uuid,
FileContentType type)
{
std::unique_ptr<IMemoryBuffer> buffer(Read(uuid, type));
buffer->MoveToString(content);
}
#endif
}
StorageAccessor.cpp
StorageAccessor::StorageAccessor(IStorageArea &area,
StorageCache* cache,
MetricsRegistry &metrics) :
area_(area),
cache_(cache),
metrics_(&metrics)
{
}
FileInfo StorageAccessor::Write(const void* data,
size_t size,
FileContentType type,
CompressionType compression,
bool storeMd5)
{
std::string uuid = Toolbox::GenerateUuid();
std::string md5;
if (storeMd5)
{
Toolbox::ComputeMD5(md5, data, size);
}
switch (compression)
{
case CompressionType_None:
{
MetricsTimer timer(*this, METRICS_CREATE);
area_.Create(uuid, data, size, type);
if (cache_ != NULL)
{
cache_->Add(uuid, type, data, size);
}
return FileInfo(uuid, type, size, md5);
}
case CompressionType_ZlibWithSize:
{
ZlibCompressor zlib;
std::string compressed;
zlib.Compress(compressed, data, size);
std::string compressedMD5;
if (storeMd5)
{
Toolbox::ComputeMD5(compressedMD5, compressed);
}
{
MetricsTimer timer(*this, METRICS_CREATE);
if (compressed.size() > 0)
{
area_.Create(uuid, &compressed[0], compressed.size(), type);
}
else
{
area_.Create(uuid, NULL, 0, type);
}
}
if (cache_ != NULL)
{
cache_->Add(uuid, type, data, size); // always add uncompressed data to cache
}
return FileInfo(uuid, type, size, md5,
CompressionType_ZlibWithSize, compressed.size(), compressedMD5);
}
default:
throw OrthancException(ErrorCode_NotImplemented);
}
ServerContext.cpp
bool ServerContext::AddAttachment(int64_t& newRevision,
const std::string& resourceId,
FileContentType attachmentType,
const void* data,
size_t size,
bool hasOldRevision,
int64_t oldRevision,
const std::string& oldMD5)
{
LOG(INFO) << "Adding attachment " << EnumerationToString(attachmentType) << " to resource " << resourceId;
// TODO Should we use "gzip" instead?
CompressionType compression = (compressionEnabled_ ? CompressionType_ZlibWithSize : CompressionType_None);
StorageAccessor accessor(area_, &storageCache_, GetMetricsRegistry());
FileInfo attachment = accessor.Write(data, size, attachmentType, compression, storeMD5_);
try
{
StoreStatus status = index_.AddAttachment(
newRevision, attachment, resourceId, hasOldRevision, oldRevision, oldMD5);
if (status != StoreStatus_Success)
{
accessor.Remove(attachment);
return false;
}
else
{
return true;
}
}
catch (OrthancException&)
{
// Fixed in Orthanc 1.9.6
accessor.Remove(attachment);
throw;
}
}
OrthancRestResources.cpp
static void UploadAttachment(RestApiPutCall& call)
{
if (call.IsDocumentation())
{
ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
call.GetDocumentation()
.SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
.SetSummary("Set attachment")
.SetDescription("Attach a file to the given DICOM " + r +
". This call will fail if trying to modify a system attachment (i.e. whose index is < 1024).")
.SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
.SetUriArgument("name", "The name of the attachment, or its index (cf. `UserContentType` configuration option)")
.AddRequestType(MimeType_Binary, "Binary data containing the attachment")
.AddAnswerType(MimeType_Json, "Empty JSON object in the case of a success")
.SetHttpHeader("If-Match", "Revision of the attachment, if this is not the first time this attachment is set.");
return;
}
ServerContext& context = OrthancRestApi::GetContext(call);
CheckValidResourceType(call);
std::string publicId = call.GetUriComponent("id", "");
std::string name = call.GetUriComponent("name", "");
FileContentType contentType = StringToContentType(name);
if (IsUserContentType(contentType)) // It is forbidden to modify internal attachments
{
int64_t oldRevision;
std::string oldMD5;
bool hasOldRevision = GetRevisionHeader(oldRevision, oldMD5, call, "if-match");
if (!hasOldRevision)
{
OrthancConfiguration::ReaderLock lock;
if (lock.GetConfiguration().GetBooleanParameter(CHECK_REVISIONS, false))
{
// "StatelessDatabaseOperations::AddAttachment()" will ignore
// the actual value of "oldRevision" if the metadata is
// inexistent as expected
hasOldRevision = true;
oldRevision = -1; // dummy value
oldMD5.clear(); // dummy value
}
}
int64_t newRevision;
context.AddAttachment(newRevision, publicId, StringToContentType(name), call.GetBodyData(),
call.GetBodySize(), hasOldRevision, oldRevision, oldMD5);
SetBufferContentETag(call.GetOutput(), newRevision, call.GetBodyData(), call.GetBodySize()); // New in Orthanc 1.9.2
call.GetOutput().AnswerBuffer("{}", MimeType_Json);
}
else
{
call.GetOutput().SignalError(HttpStatus_403_Forbidden);
}
}
ServerIndex.cpp
StoreStatus ServerIndex::AddAttachment(int64_t& newRevision,
const FileInfo& attachment,
const std::string& publicId,
bool hasOldRevision,
int64_t oldRevision,
const std::string& oldMD5)
{
uint64_t maximumStorageSize;
unsigned int maximumPatients;
{
boost::mutex::scoped_lock lock(monitoringMutex_);
maximumStorageSize = maximumStorageSize_;
maximumPatients = maximumPatients_;
}
return StatelessDatabaseOperations::AddAttachment(
newRevision, attachment, publicId, maximumStorageSize, maximumPatients,
hasOldRevision, oldRevision, oldMD5);
}
StatelessDatabaseOperations.cpp
StoreStatus StatelessDatabaseOperations::AddAttachment(int64_t& newRevision,
const FileInfo& attachment,
const std::string& publicId,
uint64_t maximumStorageSize,
unsigned int maximumPatients,
bool hasOldRevision,
int64_t oldRevision,
const std::string& oldMD5)
{
class Operations : public IReadWriteOperations
{
private:
int64_t& newRevision_;
StoreStatus status_;
const FileInfo& attachment_;
const std::string& publicId_;
uint64_t maximumStorageSize_;
unsigned int maximumPatientCount_;
bool hasOldRevision_;
int64_t oldRevision_;
const std::string& oldMD5_;
public:
Operations(int64_t& newRevision,
const FileInfo& attachment,
const std::string& publicId,
uint64_t maximumStorageSize,
unsigned int maximumPatientCount,
bool hasOldRevision,
int64_t oldRevision,
const std::string& oldMD5) :
newRevision_(newRevision),
status_(StoreStatus_Failure),
attachment_(attachment),
publicId_(publicId),
maximumStorageSize_(maximumStorageSize),
maximumPatientCount_(maximumPatientCount),
hasOldRevision_(hasOldRevision),
oldRevision_(oldRevision),
oldMD5_(oldMD5)
{
}
StoreStatus GetStatus() const
{
return status_;
}
virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
{
ResourceType resourceType;
int64_t resourceId;
if (!transaction.LookupResource(resourceId, resourceType, publicId_))
{
status_ = StoreStatus_Failure; // Inexistent resource
}
else
{
// Possibly remove previous attachment
{
FileInfo oldFile;
int64_t expectedRevision;
if (transaction.LookupAttachment(oldFile, expectedRevision, resourceId, attachment_.GetContentType()))
{
if (hasOldRevision_ &&
(expectedRevision != oldRevision_ ||
oldFile.GetUncompressedMD5() != oldMD5_))
{
throw OrthancException(ErrorCode_Revision);
}
else
{
newRevision_ = expectedRevision + 1;
transaction.DeleteAttachment(resourceId, attachment_.GetContentType());
}
}
else
{
// The attachment is not existing yet: Ignore "oldRevision"
// and initialize a new sequence of revisions
newRevision_ = 0;
}
}
// Locate the patient of the target resource
int64_t patientId = resourceId;
for (;;)
{
int64_t parent;
if (transaction.LookupParent(parent, patientId))
{
// We have not reached the patient level yet
patientId = parent;
}
else
{
// We have reached the patient level
break;
}
}
// Possibly apply the recycling mechanism while preserving this patient
assert(transaction.GetResourceType(patientId) == ResourceType_Patient);
transaction.Recycle(maximumStorageSize_, maximumPatientCount_,
attachment_.GetCompressedSize(), transaction.GetPublicId(patientId));
transaction.AddAttachment(resourceId, attachment_, newRevision_);
if (IsUserContentType(attachment_.GetContentType()))
{
transaction.LogChange(resourceId, ChangeType_UpdatedAttachment, resourceType, publicId_);
}
transaction.GetTransactionContext().SignalAttachmentsAdded(attachment_.GetCompressedSize());
status_ = StoreStatus_Success;
}
}
};
Operations operations(newRevision, attachment, publicId, maximumStorageSize, maximumPatients,
hasOldRevision, oldRevision, oldMD5);
Apply(operations);
return operations.GetStatus();
}
class ReadWriteTransaction : public ReadOnlyTransaction
{
public:
ReadWriteTransaction(IDatabaseWrapper::ITransaction& transaction,
ITransactionContext& context) :
ReadOnlyTransaction(transaction, context)
{
}
void AddAttachment(int64_t id,
const FileInfo& attachment,
int64_t revision)
{
transaction_.AddAttachment(id, attachment, revision);
}
void ClearChanges()
{
transaction_.ClearChanges();
}
void ClearExportedResources()
{
transaction_.ClearExportedResources();
}
void ClearMainDicomTags(int64_t id)
{
return transaction_.ClearMainDicomTags(id);
}
bool CreateInstance(IDatabaseWrapper::CreateInstanceResult& result, /* out */
int64_t& instanceId, /* out */
const std::string& patient,
const std::string& study,
const std::string& series,
const std::string& instance)
{
return transaction_.CreateInstance(result, instanceId, patient, study, series, instance);
}
void DeleteAttachment(int64_t id,
FileContentType attachment)
{
return transaction_.DeleteAttachment(id, attachment);
}
void DeleteMetadata(int64_t id,
MetadataType type)
{
transaction_.DeleteMetadata(id, type);
}
void DeleteResource(int64_t id)
{
transaction_.DeleteResource(id);
}
void LogChange(int64_t internalId,
ChangeType changeType,
ResourceType resourceType,
const std::string& publicId);
void LogExportedResource(const ExportedResource& resource)
{
transaction_.LogExportedResource(resource);
}
void SetGlobalProperty(GlobalProperty property,
bool shared,
const std::string& value)
{
transaction_.SetGlobalProperty(property, shared, value);
}
void SetMetadata(int64_t id,
MetadataType type,
const std::string& value,
int64_t revision)
{
return transaction_.SetMetadata(id, type, value, revision);
}
void SetProtectedPatient(int64_t internalId,
bool isProtected)
{
transaction_.SetProtectedPatient(internalId, isProtected);
}
void SetResourcesContent(const ResourcesContent& content)
{
transaction_.SetResourcesContent(content);
}
void Recycle(uint64_t maximumStorageSize,
unsigned int maximumPatients,
uint64_t addedInstanceSize,
const std::string& newPatientId);
bool HasReachedMaxStorageSize(uint64_t maximumStorageSize,
uint64_t addedInstanceSize);
bool HasReachedMaxPatientCount(unsigned int maximumPatientCount,
const std::string& patientId);
bool IsRecyclingNeeded(uint64_t maximumStorageSize,
unsigned int maximumPatients,
uint64_t addedInstanceSize,
const std::string& newPatientId);
};
/**
* The "StartTransaction()" method is guaranteed to return a class
* derived from "UnitTestsTransaction". The methods of
* "UnitTestsTransaction" give access to additional information
* about the underlying SQLite database to be used in unit tests.
**/
class UnitTestsTransaction : public ITransaction
class ORTHANC_PUBLIC Transaction : public ITransaction
AddAttachment(int64_t id
![](https://i-blog.csdnimg.cn/blog_migrate/de9064a478867a3b5534e6b457966c9d.png)
SQLiteDatabaseWrapper.cpp
virtual void AddAttachment(int64_t id,
const FileInfo& attachment,
int64_t revision) ORTHANC_OVERRIDE
{
// TODO - REVISIONS
SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
s.BindInt64(0, id);
s.BindInt(1, attachment.GetContentType());
s.BindString(2, attachment.GetUuid());
s.BindInt64(3, attachment.GetCompressedSize());
s.BindInt64(4, attachment.GetUncompressedSize());
s.BindInt(5, attachment.GetCompressionType());
s.BindString(6, attachment.GetUncompressedMD5());
s.BindString(7, attachment.GetCompressedMD5());
s.Run();
}