Mercurial > hg > orthanc
annotate Core/FileStorage/CompressedFileStorageAccessor.cpp @ 940:4864b3e304be
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Jun 2014 09:06:34 +0200 |
parents | a811bdf8b8eb |
children | 1d60316c3618 |
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 | |
40 namespace Orthanc | |
41 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
42 FileInfo CompressedFileStorageAccessor::WriteInternal(const void* data, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
43 size_t size, |
233 | 44 FileContentType type) |
221 | 45 { |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
46 std::string md5; |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
47 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
48 if (storeMD5_) |
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 Toolbox::ComputeMD5(md5, data, size); |
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 |
221 | 53 switch (compressionType_) |
54 { | |
55 case CompressionType_None: | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
56 { |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
57 std::string uuid = storage_.Create(data, size); |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
58 return FileInfo(uuid, type, size, md5); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
59 } |
221 | 60 |
61 case CompressionType_Zlib: | |
62 { | |
63 std::string compressed; | |
64 zlib_.Compress(compressed, data, size); | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
65 |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
66 std::string compressedMD5; |
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 if (storeMD5_) |
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 Toolbox::ComputeMD5(compressedMD5, compressed); |
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 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
73 std::string uuid = storage_.Create(compressed); |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
74 return FileInfo(uuid, type, size, md5, |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
75 CompressionType_Zlib, compressed.size(), compressedMD5); |
221 | 76 } |
77 | |
78 default: | |
79 throw OrthancException(ErrorCode_NotImplemented); | |
80 } | |
81 } | |
82 | |
83 CompressedFileStorageAccessor::CompressedFileStorageAccessor(FileStorage& storage) : | |
84 storage_(storage) | |
85 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
86 compressionType_ = CompressionType_None; |
221 | 87 } |
88 | |
89 void CompressedFileStorageAccessor::Read(std::string& content, | |
90 const std::string& uuid) | |
91 { | |
92 switch (compressionType_) | |
93 { | |
94 case CompressionType_None: | |
95 storage_.ReadFile(content, uuid); | |
96 break; | |
97 | |
98 case CompressionType_Zlib: | |
99 { | |
100 std::string compressed; | |
101 storage_.ReadFile(compressed, uuid); | |
102 zlib_.Uncompress(content, compressed); | |
103 break; | |
104 } | |
105 | |
106 default: | |
107 throw OrthancException(ErrorCode_NotImplemented); | |
108 } | |
109 } | |
110 | |
111 HttpFileSender* CompressedFileStorageAccessor::ConstructHttpFileSender(const std::string& uuid) | |
112 { | |
113 switch (compressionType_) | |
114 { | |
115 case CompressionType_None: | |
116 { | |
117 FileStorageAccessor uncompressedAccessor(storage_); | |
118 return uncompressedAccessor.ConstructHttpFileSender(uuid); | |
119 } | |
120 | |
121 case CompressionType_Zlib: | |
122 { | |
123 std::string compressed; | |
124 storage_.ReadFile(compressed, uuid); | |
125 | |
126 std::auto_ptr<BufferHttpSender> sender(new BufferHttpSender); | |
127 zlib_.Uncompress(sender->GetBuffer(), compressed); | |
128 | |
129 return sender.release(); | |
130 } | |
131 | |
132 default: | |
133 throw OrthancException(ErrorCode_NotImplemented); | |
134 } | |
135 } | |
136 } |