comparison Core/HttpServer/FilesystemHttpSender.cpp @ 1522:f938f7779bcb

fixes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 15:37:42 +0200
parents 8bd0d897763f
children c388502a066d
comparison
equal deleted inserted replaced
1521:3606278d305e 1522:f938f7779bcb
31 31
32 #include "../PrecompiledHeaders.h" 32 #include "../PrecompiledHeaders.h"
33 #include "FilesystemHttpSender.h" 33 #include "FilesystemHttpSender.h"
34 34
35 #include "../Toolbox.h" 35 #include "../Toolbox.h"
36 #include "../OrthancException.h"
37 #include "../Compression/ZlibCompressor.h"
36 38
37 #include <stdio.h> 39 #include <stdio.h>
38 40
39 41
40 static const size_t CHUNK_SIZE = 64 * 1024; // Use 64KB chunks 42 static const size_t CHUNK_SIZE = 64 * 1024; // Use 64KB chunks
41 43
42 namespace Orthanc 44 namespace Orthanc
43 { 45 {
44 void FilesystemHttpSender::Open() 46 void FilesystemHttpSender::Initialize(const boost::filesystem::path& path)
45 { 47 {
46 SetFilename(path_.filename().string()); 48 sourceCompression_ = CompressionType_None;
47 file_.open(path_.string().c_str(), std::ifstream::binary); 49 targetCompression_ = HttpCompression_None;
50
51 SetContentFilename(path.filename().string());
52 file_.open(path.string().c_str(), std::ifstream::binary);
53
54 if (!file_.is_open())
55 {
56 throw OrthancException(ErrorCode_InexistentFile);
57 }
48 58
49 file_.seekg(0, file_.end); 59 file_.seekg(0, file_.end);
50 size_ = file_.tellg(); 60 size_ = file_.tellg();
51 file_.seekg(0, file_.beg); 61 file_.seekg(0, file_.beg);
52
53 chunk_.resize(CHUNK_SIZE);
54 chunkSize_ = 0;
55 } 62 }
56 63
57 FilesystemHttpSender::FilesystemHttpSender(const char* path) 64
65 HttpCompression FilesystemHttpSender::GetHttpCompression(bool gzipAllowed,
66 bool deflateAllowed)
58 { 67 {
59 path_ = std::string(path); 68 switch (sourceCompression_)
60 Open(); 69 {
61 } 70 case CompressionType_None:
71 {
72 return HttpCompression_None;
73 }
62 74
63 FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path) 75 case CompressionType_ZlibWithSize:
64 { 76 {
65 path_ = path; 77 if (size_ == 0)
66 Open(); 78 {
67 } 79 return HttpCompression_None;
80 }
68 81
69 FilesystemHttpSender::FilesystemHttpSender(const FilesystemStorage& storage, 82 if (size_ < sizeof(uint64_t))
70 const std::string& uuid) 83 {
71 { 84 throw OrthancException(ErrorCode_CorruptedFile);
72 path_ = storage.GetPath(uuid).string(); 85 }
73 Open(); 86
87 if (deflateAllowed)
88 {
89 file_.seekg(sizeof(uint64_t), file_.end);
90 return HttpCompression_Deflate;
91 }
92 else
93 {
94 uncompressed_.reset(new BufferHttpSender);
95
96 // TODO Stream-based uncompression
97 assert(size_ != 0);
98 std::string compressed;
99 compressed.resize(size_);
100
101 file_.read(&compressed[0], size_);
102 if ((file_.flags() & std::istream::failbit) ||
103 !(file_.flags() & std::istream::eofbit))
104 {
105 throw OrthancException(ErrorCode_CorruptedFile);
106 }
107
108 ZlibCompressor compressor;
109 IBufferCompressor::Uncompress(uncompressed_->GetBuffer(), compressor, compressed);
110 return HttpCompression_None;
111 }
112
113 break;
114 }
115
116 default:
117 throw OrthancException(ErrorCode_NotImplemented);
118 }
74 } 119 }
75 120
76 121
77 bool FilesystemHttpSender::ReadNextChunk() 122 bool FilesystemHttpSender::ReadNextChunk()
78 { 123 {
79 file_.read(&chunk_[0], chunk_.size()); 124 if (uncompressed_.get() != NULL)
125 {
126 return uncompressed_->ReadNextChunk();
127 }
128 else
129 {
130 if (chunk_.size() == 0)
131 {
132 chunk_.resize(CHUNK_SIZE);
133 }
80 134
81 chunkSize_ = file_.gcount(); 135 file_.read(&chunk_[0], chunk_.size());
82 136
83 return chunkSize_ > 0; 137 if (file_.flags() & std::istream::failbit)
138 {
139 throw OrthancException(ErrorCode_CorruptedFile);
140 }
141
142 chunkSize_ = file_.gcount();
143
144 return chunkSize_ > 0;
145 }
146 }
147
148
149 const char* FilesystemHttpSender::GetChunkContent()
150 {
151 if (uncompressed_.get() != NULL)
152 {
153 return uncompressed_->GetChunkContent();
154 }
155 else
156 {
157 return chunk_.c_str();
158 }
159 }
160
161
162 size_t FilesystemHttpSender::GetChunkSize()
163 {
164 if (uncompressed_.get() != NULL)
165 {
166 return uncompressed_->GetChunkSize();
167 }
168 else
169 {
170 return chunkSize_;
171 }
84 } 172 }
85 } 173 }