comparison OrthancServer/OrthancRestApi.cpp @ 698:aae83e1e31f7

verify MD5 of attachments through REST
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Feb 2014 17:20:43 +0100
parents dd1ce9a2844c
children 2929e17f8447
comparison
equal deleted inserted replaced
697:dd1ce9a2844c 698:aae83e1e31f7
1892 operations.append("md5"); 1892 operations.append("md5");
1893 } 1893 }
1894 1894
1895 operations.append("size"); 1895 operations.append("size");
1896 1896
1897 if (info.GetCompressedMD5() != "") 1897 if (info.GetCompressedMD5() != "" &&
1898 info.GetUncompressedMD5() != "")
1898 { 1899 {
1899 operations.append("verify-md5"); 1900 operations.append("verify-md5");
1900 } 1901 }
1901 1902
1902 call.GetOutput().AnswerJson(operations); 1903 call.GetOutput().AnswerJson(operations);
1957 FileInfo info; 1958 FileInfo info;
1958 if (GetAttachmentInfo(info, call) && 1959 if (GetAttachmentInfo(info, call) &&
1959 info.GetCompressedMD5() != "") 1960 info.GetCompressedMD5() != "")
1960 { 1961 {
1961 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain"); 1962 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedMD5()), "text/plain");
1963 }
1964 }
1965
1966
1967 static void VerifyAttachment(RestApi::PostCall& call)
1968 {
1969 RETRIEVE_CONTEXT(call);
1970 CheckValidResourceType(call);
1971
1972 std::string publicId = call.GetUriComponent("id", "");
1973 std::string name = call.GetUriComponent("name", "");
1974
1975 FileInfo info;
1976 if (!GetAttachmentInfo(info, call) ||
1977 info.GetCompressedMD5() == "" ||
1978 info.GetUncompressedMD5() == "")
1979 {
1980 // Inexistent resource, or no MD5 available
1981 return;
1982 }
1983
1984 bool ok = false;
1985
1986 // First check whether the compressed data is correctly stored in the disk
1987 std::string data;
1988 context.ReadFile(data, publicId, StringToContentType(name), false);
1989
1990 std::string actualMD5;
1991 Toolbox::ComputeMD5(actualMD5, data);
1992
1993 if (actualMD5 == info.GetCompressedMD5())
1994 {
1995 // The compressed data is OK. If a compression algorithm was
1996 // applied to it, now check the MD5 of the uncompressed data.
1997 if (info.GetCompressionType() == CompressionType_None)
1998 {
1999 ok = true;
2000 }
2001 else
2002 {
2003 context.ReadFile(data, publicId, StringToContentType(name), true);
2004 Toolbox::ComputeMD5(actualMD5, data);
2005 ok = (actualMD5 == info.GetUncompressedMD5());
2006 }
2007 }
2008
2009 if (ok)
2010 {
2011 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has the right MD5";
2012 call.GetOutput().AnswerBuffer("{}", "application/json");
2013 }
2014 else
2015 {
2016 LOG(INFO) << "The attachment " << name << " of resource " << publicId << " has bad MD5!";
1962 } 2017 }
1963 } 2018 }
1964 2019
1965 2020
1966 2021
2015 Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5); 2070 Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5);
2016 Register("/{resourceType}/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize); 2071 Register("/{resourceType}/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize);
2017 Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>); 2072 Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>);
2018 Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5); 2073 Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5);
2019 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize); 2074 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize);
2075 Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment);
2020 2076
2021 Register("/patients/{id}/protected", IsProtectedPatient); 2077 Register("/patients/{id}/protected", IsProtectedPatient);
2022 Register("/patients/{id}/protected", SetPatientProtection); 2078 Register("/patients/{id}/protected", SetPatientProtection);
2023 Register("/instances/{id}/file", GetInstanceFile); 2079 Register("/instances/{id}/file", GetInstanceFile);
2024 Register("/instances/{id}/export", ExportInstanceFile); 2080 Register("/instances/{id}/export", ExportInstanceFile);