Mercurial > hg > orthanc
comparison Core/HttpServer/FilesystemHttpSender.cpp @ 1519:8bd0d897763f
refactoring: IHttpStreamAnswer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Aug 2015 13:15:16 +0200 |
parents | 6e7e5ed91c2d |
children | f938f7779bcb |
comparison
equal
deleted
inserted
replaced
1518:eb46cc06389a | 1519:8bd0d897763f |
---|---|
34 | 34 |
35 #include "../Toolbox.h" | 35 #include "../Toolbox.h" |
36 | 36 |
37 #include <stdio.h> | 37 #include <stdio.h> |
38 | 38 |
39 | |
40 static const size_t CHUNK_SIZE = 64 * 1024; // Use 64KB chunks | |
41 | |
39 namespace Orthanc | 42 namespace Orthanc |
40 { | 43 { |
41 void FilesystemHttpSender::Setup() | 44 void FilesystemHttpSender::Open() |
42 { | 45 { |
43 //SetDownloadFilename(path_.filename().string()); | 46 SetFilename(path_.filename().string()); |
47 file_.open(path_.string().c_str(), std::ifstream::binary); | |
44 | 48 |
45 #if BOOST_HAS_FILESYSTEM_V3 == 1 | 49 file_.seekg(0, file_.end); |
46 SetContentType(Toolbox::AutodetectMimeType(path_.filename().string())); | 50 size_ = file_.tellg(); |
47 #else | 51 file_.seekg(0, file_.beg); |
48 SetContentType(Toolbox::AutodetectMimeType(path_.filename())); | |
49 #endif | |
50 } | |
51 | 52 |
52 uint64_t FilesystemHttpSender::GetFileSize() | 53 chunk_.resize(CHUNK_SIZE); |
53 { | 54 chunkSize_ = 0; |
54 return Toolbox::GetFileSize(path_.string()); | |
55 } | |
56 | |
57 bool FilesystemHttpSender::SendData(HttpOutput& output) | |
58 { | |
59 FILE* fp = fopen(path_.string().c_str(), "rb"); | |
60 if (!fp) | |
61 { | |
62 return false; | |
63 } | |
64 | |
65 std::vector<uint8_t> buffer(1024 * 1024); // Chunks of 1MB | |
66 | |
67 for (;;) | |
68 { | |
69 size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp); | |
70 if (nbytes == 0) | |
71 { | |
72 break; | |
73 } | |
74 else | |
75 { | |
76 output.SendBody(&buffer[0], nbytes); | |
77 } | |
78 } | |
79 | |
80 fclose(fp); | |
81 | |
82 return true; | |
83 } | 55 } |
84 | 56 |
85 FilesystemHttpSender::FilesystemHttpSender(const char* path) | 57 FilesystemHttpSender::FilesystemHttpSender(const char* path) |
86 { | 58 { |
87 path_ = std::string(path); | 59 path_ = std::string(path); |
88 Setup(); | 60 Open(); |
89 } | 61 } |
90 | 62 |
91 FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path) | 63 FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path) |
92 { | 64 { |
93 path_ = path; | 65 path_ = path; |
94 Setup(); | 66 Open(); |
95 } | 67 } |
96 | 68 |
97 FilesystemHttpSender::FilesystemHttpSender(const FilesystemStorage& storage, | 69 FilesystemHttpSender::FilesystemHttpSender(const FilesystemStorage& storage, |
98 const std::string& uuid) | 70 const std::string& uuid) |
99 { | 71 { |
100 path_ = storage.GetPath(uuid).string(); | 72 path_ = storage.GetPath(uuid).string(); |
101 Setup(); | 73 Open(); |
74 } | |
75 | |
76 | |
77 bool FilesystemHttpSender::ReadNextChunk() | |
78 { | |
79 file_.read(&chunk_[0], chunk_.size()); | |
80 | |
81 chunkSize_ = file_.gcount(); | |
82 | |
83 return chunkSize_ > 0; | |
102 } | 84 } |
103 } | 85 } |