Mercurial > hg > orthanc
annotate Core/FileStorage/CompressedFileStorageAccessor.cpp @ 1126:bf67431a7383
handling of file content type in IStorageArea
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Sep 2014 17:01:42 +0200 |
parents | 790ff7a5b3bf |
children | ba9fd42284d0 |
rev | line source |
---|---|
221 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
221 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
221 | 34 #include "CompressedFileStorageAccessor.h" |
35 | |
36 #include "../OrthancException.h" | |
37 #include "FileStorageAccessor.h" | |
38 #include "../HttpServer/BufferHttpSender.h" | |
39 | |
1124
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
40 #include <memory> |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
41 |
221 | 42 namespace Orthanc |
43 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
44 FileInfo CompressedFileStorageAccessor::WriteInternal(const void* data, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
45 size_t size, |
233 | 46 FileContentType type) |
221 | 47 { |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
48 std::string md5; |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
49 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
50 if (storeMD5_) |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
51 { |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
52 Toolbox::ComputeMD5(md5, data, size); |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
53 } |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
54 |
221 | 55 switch (compressionType_) |
56 { | |
57 case CompressionType_None: | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
58 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
59 std::string uuid = storage_.Create(data, size, type); |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
60 return FileInfo(uuid, type, size, md5); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
61 } |
221 | 62 |
63 case CompressionType_Zlib: | |
64 { | |
65 std::string compressed; | |
66 zlib_.Compress(compressed, data, size); | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
67 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
68 std::string compressedMD5; |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
69 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
70 if (storeMD5_) |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
71 { |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
72 Toolbox::ComputeMD5(compressedMD5, compressed); |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
73 } |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
74 |
1124
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
75 std::string uuid; |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
76 if (compressed.size() > 0) |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
77 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
78 uuid = storage_.Create(&compressed[0], compressed.size(), type); |
1124
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
79 } |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
80 else |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
81 { |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
82 uuid = storage_.Create(NULL, 0, type); |
1124
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
83 } |
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
84 |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
85 return FileInfo(uuid, type, size, md5, |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
86 CompressionType_Zlib, compressed.size(), compressedMD5); |
221 | 87 } |
88 | |
89 default: | |
90 throw OrthancException(ErrorCode_NotImplemented); | |
91 } | |
92 } | |
93 | |
1124
790ff7a5b3bf
IStorageArea abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
94 CompressedFileStorageAccessor::CompressedFileStorageAccessor(IStorageArea& storage) : |
221 | 95 storage_(storage) |
96 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
97 compressionType_ = CompressionType_None; |
221 | 98 } |
99 | |
100 void CompressedFileStorageAccessor::Read(std::string& content, | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
101 const std::string& uuid, |
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
102 FileContentType type) |
221 | 103 { |
104 switch (compressionType_) | |
105 { | |
106 case CompressionType_None: | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
107 storage_.Read(content, uuid, type); |
221 | 108 break; |
109 | |
110 case CompressionType_Zlib: | |
111 { | |
112 std::string compressed; | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
113 storage_.Read(compressed, uuid, type); |
221 | 114 zlib_.Uncompress(content, compressed); |
115 break; | |
116 } | |
117 | |
118 default: | |
119 throw OrthancException(ErrorCode_NotImplemented); | |
120 } | |
121 } | |
122 | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
123 HttpFileSender* CompressedFileStorageAccessor::ConstructHttpFileSender(const std::string& uuid, |
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
124 FileContentType type) |
221 | 125 { |
126 switch (compressionType_) | |
127 { | |
128 case CompressionType_None: | |
129 { | |
130 FileStorageAccessor uncompressedAccessor(storage_); | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
131 return uncompressedAccessor.ConstructHttpFileSender(uuid, type); |
221 | 132 } |
133 | |
134 case CompressionType_Zlib: | |
135 { | |
136 std::string compressed; | |
1126
bf67431a7383
handling of file content type in IStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1124
diff
changeset
|
137 storage_.Read(compressed, uuid, type); |
221 | 138 |
139 std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender); | |
140 zlib_.Uncompress(sender->GetBuffer(), compressed); | |
141 | |
142 return sender.release(); | |
143 } | |
144 | |
145 default: | |
146 throw OrthancException(ErrorCode_NotImplemented); | |
147 } | |
148 } | |
149 } |