# HG changeset patch # User Sebastien Jodogne # Date 1391617243 -3600 # Node ID aae83e1e31f7da5de28e5cd40c15e55f753b6ef7 # Parent dd1ce9a2844ce408b83074e4ed4ca19b890016ac verify MD5 of attachments through REST diff -r dd1ce9a2844c -r aae83e1e31f7 NEWS --- a/NEWS Wed Feb 05 16:46:59 2014 +0100 +++ b/NEWS Wed Feb 05 17:20:43 2014 +0100 @@ -1,6 +1,7 @@ Pending changes in the mainline =============================== +* Access to lowlevel info about the attached files through the REST API * AET comparison is now case-insensitive by default * Possibility to disable the HTTP server or the DICOM server * Recover pixel data for more transfer syntaxes (notably JPEG) diff -r dd1ce9a2844c -r aae83e1e31f7 OrthancServer/OrthancRestApi.cpp --- a/OrthancServer/OrthancRestApi.cpp Wed Feb 05 16:46:59 2014 +0100 +++ b/OrthancServer/OrthancRestApi.cpp Wed Feb 05 17:20:43 2014 +0100 @@ -1894,7 +1894,8 @@ operations.append("size"); - if (info.GetCompressedMD5() != "") + if (info.GetCompressedMD5() != "" && + info.GetUncompressedMD5() != "") { operations.append("verify-md5"); } @@ -1963,6 +1964,60 @@ } + static void VerifyAttachment(RestApi::PostCall& call) + { + RETRIEVE_CONTEXT(call); + CheckValidResourceType(call); + + std::string publicId = call.GetUriComponent("id", ""); + std::string name = call.GetUriComponent("name", ""); + + FileInfo info; + if (!GetAttachmentInfo(info, call) || + info.GetCompressedMD5() == "" || + info.GetUncompressedMD5() == "") + { + // Inexistent resource, or no MD5 available + return; + } + + bool ok = false; + + // First check whether the compressed data is correctly stored in the disk + std::string data; + context.ReadFile(data, publicId, StringToContentType(name), false); + + std::string actualMD5; + Toolbox::ComputeMD5(actualMD5, data); + + if (actualMD5 == info.GetCompressedMD5()) + { + // The compressed data is OK. If a compression algorithm was + // applied to it, now check the MD5 of the uncompressed data. + if (info.GetCompressionType() == CompressionType_None) + { + ok = true; + } + else + { + context.ReadFile(data, publicId, StringToContentType(name), true); + Toolbox::ComputeMD5(actualMD5, data); + ok = (actualMD5 == info.GetUncompressedMD5()); + } + } + + if (ok) + { + LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5"; + call.GetOutput().AnswerBuffer("{}", "application/json"); + } + else + { + LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!"; + } + } + + // Registration of the various REST handlers -------------------------------- @@ -2017,6 +2072,7 @@ Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>); Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5); Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize); + Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment); Register("/patients/{id}/protected", IsProtectedPatient); Register("/patients/{id}/protected", SetPatientProtection);