ctkXnatFile.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkXnatFile.h"
  16. #include "ctkXnatException.h"
  17. #include "ctkXnatObjectPrivate.h"
  18. #include "ctkXnatSession.h"
  19. #include <QCryptographicHash>
  20. #include <QDebug>
  21. #include <QFile>
  22. const QString ctkXnatFile::FILE_NAME = "Name";
  23. const QString ctkXnatFile::FILE_TAGS = "file_tags";
  24. const QString ctkXnatFile::FILE_FORMAT = "file_format";
  25. const QString ctkXnatFile::FILE_CONTENT = "file_content";
  26. //----------------------------------------------------------------------------
  27. class ctkXnatFilePrivate : public ctkXnatObjectPrivate
  28. {
  29. public:
  30. ctkXnatFilePrivate()
  31. : ctkXnatObjectPrivate()
  32. {
  33. }
  34. void reset()
  35. {
  36. }
  37. QString localFilePath;
  38. };
  39. //----------------------------------------------------------------------------
  40. ctkXnatFile::ctkXnatFile(ctkXnatObject* parent, const QString& schemaType)
  41. : ctkXnatObject(*new ctkXnatFilePrivate(), parent, schemaType)
  42. {
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkXnatFile::~ctkXnatFile()
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. void ctkXnatFile::setName(const QString &name)
  50. {
  51. this->setProperty(FILE_NAME, name);
  52. }
  53. //----------------------------------------------------------------------------
  54. QString ctkXnatFile::name() const
  55. {
  56. return this->property(FILE_NAME);
  57. }
  58. //----------------------------------------------------------------------------
  59. void ctkXnatFile::setFileFormat(const QString &fileFormat)
  60. {
  61. this->setProperty(FILE_FORMAT, fileFormat);
  62. }
  63. //----------------------------------------------------------------------------
  64. QString ctkXnatFile::fileFormat() const
  65. {
  66. return this->property(FILE_FORMAT);
  67. }
  68. //----------------------------------------------------------------------------
  69. void ctkXnatFile::setFileContent(const QString &fileContent)
  70. {
  71. this->setProperty(FILE_CONTENT, fileContent);
  72. }
  73. //----------------------------------------------------------------------------
  74. QString ctkXnatFile::fileContent() const
  75. {
  76. return this->property(FILE_CONTENT);
  77. }
  78. //----------------------------------------------------------------------------
  79. void ctkXnatFile::setFileTags(const QString &fileTags)
  80. {
  81. this->setProperty(FILE_TAGS, fileTags);
  82. }
  83. //----------------------------------------------------------------------------
  84. QString ctkXnatFile::fileTags() const
  85. {
  86. return this->property(FILE_TAGS);
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkXnatFile::setLocalFilePath(const QString &filePath)
  90. {
  91. Q_D(ctkXnatFile);
  92. d->localFilePath = filePath;
  93. }
  94. //----------------------------------------------------------------------------
  95. QString ctkXnatFile::localFilePath() const
  96. {
  97. Q_D(const ctkXnatFile);
  98. return d->localFilePath;
  99. }
  100. //----------------------------------------------------------------------------
  101. QString ctkXnatFile::resourceUri() const
  102. {
  103. return QString("%1/files/%2").arg(parent()->resourceUri(), this->name());
  104. }
  105. //----------------------------------------------------------------------------
  106. void ctkXnatFile::reset()
  107. {
  108. ctkXnatObject::reset();
  109. }
  110. //----------------------------------------------------------------------------
  111. void ctkXnatFile::fetchImpl()
  112. {
  113. // Does not make sense to fetch a file
  114. }
  115. //----------------------------------------------------------------------------
  116. void ctkXnatFile::downloadImpl(const QString& filename)
  117. {
  118. QString query = this->resourceUri();
  119. this->session()->download(filename, query);
  120. }
  121. //----------------------------------------------------------------------------
  122. void ctkXnatFile::saveImpl()
  123. {
  124. Q_D(ctkXnatFile);
  125. QString query = this->resourceUri();
  126. QString filename = this->localFilePath();
  127. QFile file(filename);
  128. if (!file.exists())
  129. {
  130. QString msg = "Error uploading file! ";
  131. msg.append(QString("File \"%1\" does not exist!").arg(filename));
  132. throw ctkXnatException(msg);
  133. }
  134. // Creating the update query
  135. query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
  136. const QMap<QString, QString>& properties = this->properties();
  137. QMapIterator<QString, QString> itProperties(properties);
  138. while (itProperties.hasNext())
  139. {
  140. itProperties.next();
  141. // Do not append these file specific properties since they require a slightly
  142. // different key for uploading a file (e.g. instead of "file_format" only "format")
  143. if (itProperties.key() == FILE_TAGS || itProperties.key() == FILE_FORMAT ||
  144. itProperties.key() == FILE_CONTENT)
  145. continue;
  146. query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
  147. }
  148. query.append(QString("&%1=%2").arg("format", this->fileFormat()));
  149. query.append(QString("&%1=%2").arg("content", this->fileContent()));
  150. query.append(QString("&%1=%2").arg("tags", this->fileTags()));
  151. // TODO May be flag for setting overwrite and not doing this automatically
  152. if (this->exists())
  153. query.append(QString("&%1=%2").arg("overwrite", true));
  154. // Flag needed for file upload
  155. query.append(QString("&%1=%2").arg("inbody", "true"));
  156. this->session()->upload(filename, query);
  157. // Validating the file upload by requesting the catalog XML
  158. // of the parent resource. Unfortunately for XNAT versions <= 1.6.4
  159. // this is the only way to get the file's MD5 hash form the server.
  160. QString md5Query = this->parent()->resourceUri();
  161. QUuid md5ID = this->session()->httpGet(md5Query);
  162. QList<QVariantMap> result = this->session()->httpSync(md5ID);
  163. QString md5ChecksumRemote ("0");
  164. // Newly added files are usually at the end of the catalog
  165. // and hence at the end of the result list. So iterating backwards
  166. // is for performance reasons.
  167. QList<QVariantMap>::const_iterator it = result.constEnd()-1;
  168. while (it != result.constBegin()-1)
  169. {
  170. QVariantMap::const_iterator it2 = (*it).find(this->name());
  171. if (it2 != (*it).constEnd())
  172. {
  173. md5ChecksumRemote = it2.value().toString();
  174. break;
  175. }
  176. --it;
  177. }
  178. if (file.open(QFile::ReadOnly) && md5ChecksumRemote != "0")
  179. {
  180. QCryptographicHash hash(QCryptographicHash::Md5);
  181. // TODO Do this in case of Qt5
  182. //if (hash.addData(&file))
  183. hash.addData(file.readAll());
  184. QString md5ChecksumLocal(hash.result().toHex());
  185. // Retrieving the md5 checksum on the server and comparing
  186. // it with the local file md5 sum
  187. if (md5ChecksumLocal != md5ChecksumRemote)
  188. {
  189. // Remove corrupted file from server
  190. this->erase();
  191. throw ctkXnatException("Upload failed! An error occurred during file upload.");
  192. }
  193. }
  194. else
  195. {
  196. qWarning()<<"Could not validate file upload!";
  197. }
  198. // End file validation
  199. }