Mercurial > hg > orthanc
annotate OrthancFramework/Sources/HttpServer/FilesystemHttpSender.cpp @ 5236:5e0db9eac1f8 db-protobuf
integration mainline->db-protobuf
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 04 Apr 2023 21:43:37 +0200 |
parents | 0ea402b4d901 |
children | aaf7c49a9ddc 48b8dae6dc77 |
rev | line source |
---|---|
208 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1123
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
208 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
208 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
208 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
208 | 21 **/ |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
208 | 24 #include "FilesystemHttpSender.h" |
25 | |
1522 | 26 #include "../OrthancException.h" |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
27 |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
28 static const size_t CHUNK_SIZE = 64 * 1024; // Use 64KB chunks |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
29 |
208 | 30 namespace Orthanc |
31 { | |
1522 | 32 void FilesystemHttpSender::Initialize(const boost::filesystem::path& path) |
208 | 33 { |
1522 | 34 SetContentFilename(path.filename().string()); |
35 file_.open(path.string().c_str(), std::ifstream::binary); | |
36 | |
37 if (!file_.is_open()) | |
38 { | |
39 throw OrthancException(ErrorCode_InexistentFile); | |
40 } | |
208 | 41 |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
42 file_.seekg(0, file_.end); |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
43 size_ = file_.tellg(); |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
44 file_.seekg(0, file_.beg); |
217 | 45 } |
46 | |
4298 | 47 FilesystemHttpSender::FilesystemHttpSender(const std::string& path) |
48 { | |
49 Initialize(path); | |
50 } | |
51 | |
52 FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path) | |
53 { | |
54 Initialize(path); | |
55 } | |
56 | |
57 FilesystemHttpSender::FilesystemHttpSender(const std::string& path, | |
58 MimeType contentType) | |
59 { | |
60 SetContentType(contentType); | |
61 Initialize(path); | |
62 } | |
63 | |
64 FilesystemHttpSender::FilesystemHttpSender(const FilesystemStorage& storage, | |
65 const std::string& uuid) | |
66 { | |
67 Initialize(storage.GetPath(uuid)); | |
68 } | |
69 | |
70 uint64_t FilesystemHttpSender::GetContentLength() | |
71 { | |
72 return size_; | |
73 } | |
74 | |
1522 | 75 |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
76 bool FilesystemHttpSender::ReadNextChunk() |
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
77 { |
1525 | 78 if (chunk_.size() == 0) |
1522 | 79 { |
1525 | 80 chunk_.resize(CHUNK_SIZE); |
1522 | 81 } |
82 | |
1525 | 83 file_.read(&chunk_[0], chunk_.size()); |
1519
8bd0d897763f
refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
84 |
1525 | 85 if ((file_.flags() & std::istream::failbit) || |
86 file_.gcount() < 0) | |
1522 | 87 { |
1525 | 88 throw OrthancException(ErrorCode_CorruptedFile); |
1522 | 89 } |
90 | |
2332 | 91 chunkSize_ = static_cast<size_t>(file_.gcount()); |
1525 | 92 |
93 return chunkSize_ > 0; | |
208 | 94 } |
4298 | 95 |
96 const char *FilesystemHttpSender::GetChunkContent() | |
97 { | |
98 return chunk_.c_str(); | |
99 } | |
100 | |
101 size_t FilesystemHttpSender::GetChunkSize() | |
102 { | |
103 return chunkSize_; | |
104 } | |
208 | 105 } |