Mercurial > hg > orthanc-object-storage
annotate Common/FileSystemStorage.cpp @ 148:c02a830ae308
try
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 20 Jun 2024 18:52:57 +0200 |
parents | 3c7e0374f28e |
children | d62f52be1943 |
rev | line source |
---|---|
77 | 1 /** |
2 * Cloud storage plugins for Orthanc | |
145
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
3 * Copyright (C) 2020-2023 Osimis S.A., Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
4 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
3c7e0374f28e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
111
diff
changeset
|
5 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
77 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
78 | 21 #include "FileSystemStorage.h" |
22 #include "BaseStorage.h" | |
77 | 23 |
24 #include <SystemToolbox.h> | |
25 #include <boost/filesystem.hpp> | |
26 #include <boost/filesystem/fstream.hpp> | |
27 | |
28 namespace fs = boost::filesystem; | |
29 | |
30 void FileSystemStoragePlugin::FileSystemWriter::Write(const char* data, size_t size) | |
31 { | |
32 Orthanc::SystemToolbox::MakeDirectory(path_.parent_path().string()); | |
33 | |
34 Orthanc::SystemToolbox::WriteFile(reinterpret_cast<const void*>(data), size, path_.string(), fsync_); | |
35 } | |
36 | |
37 size_t FileSystemStoragePlugin::FileSystemReader::GetSize() | |
38 { | |
39 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string())) | |
40 { | |
41 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
42 } | |
43 | |
44 try | |
45 { | |
46 fs::ifstream f; | |
47 f.open(path_.string(), std::ifstream::in | std::ifstream::binary); | |
48 if (!f.good()) | |
49 { | |
50 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
51 } | |
52 | |
53 f.seekg(0, std::ios::end); | |
54 std::streamsize fileSize = f.tellg(); | |
55 f.seekg(0, std::ios::beg); | |
56 | |
57 f.close(); | |
58 | |
59 return fileSize; | |
60 } | |
61 catch (...) | |
62 { | |
63 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string()); | |
64 } | |
65 | |
66 } | |
67 | |
68 void FileSystemStoragePlugin::FileSystemReader::ReadWhole(char* data, size_t size) | |
69 { | |
70 ReadRange(data, size, 0); | |
71 } | |
72 | |
73 void FileSystemStoragePlugin::FileSystemReader::ReadRange(char* data, size_t size, size_t fromOffset) | |
74 { | |
75 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string())) | |
76 { | |
77 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
78 } | |
79 | |
80 try | |
81 { | |
82 fs::ifstream f; | |
83 f.open(path_.string(), std::ifstream::in | std::ifstream::binary); | |
84 if (!f.good()) | |
85 { | |
86 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
87 } | |
88 | |
89 f.seekg(fromOffset, std::ios::beg); | |
90 | |
91 f.read(reinterpret_cast<char*>(data), size); | |
92 | |
93 f.close(); | |
94 } | |
95 catch (...) | |
96 { | |
97 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string()); | |
98 } | |
99 } | |
100 | |
101 | |
102 | |
78 | 103 IStorage::IWriter* FileSystemStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
77 | 104 { |
78 | 105 return new FileSystemWriter(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_), fsync_); |
77 | 106 } |
107 | |
78 | 108 IStorage::IReader* FileSystemStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
77 | 109 { |
78 | 110 return new FileSystemReader(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_)); |
77 | 111 } |
112 | |
113 void FileSystemStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
114 { | |
115 try | |
116 { | |
117 namespace fs = boost::filesystem; | |
118 | |
78 | 119 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_); |
77 | 120 |
121 try | |
122 { | |
123 fs::remove(path.string()); | |
124 } | |
125 catch (...) | |
126 { | |
127 // Ignore the error | |
128 } | |
129 | |
130 // Remove the two parent directories, ignoring the error code if | |
131 // these directories are not empty | |
132 | |
133 try | |
134 { | |
135 boost::system::error_code err; | |
136 fs::remove(path.parent_path().string(), err); | |
137 fs::remove(path.parent_path().parent_path().string(), err); | |
138 } | |
139 catch (...) | |
140 { | |
141 // Ignore the error | |
142 } | |
143 } | |
144 catch(Orthanc::OrthancException& e) | |
145 { | |
146 OrthancPlugins::LogError(GetNameForLogs() + ": error while deleting object " + std::string(uuid) + ": " + std::string(e.What())); | |
147 } | |
148 | |
149 } | |
150 | |
111
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
151 bool FileSystemStoragePlugin::FileExists(const std::string& uuid, OrthancPluginContentType type, bool encryptionEnabled) |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
152 { |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
153 namespace fs = boost::filesystem; |
77 | 154 |
111
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
155 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_); |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
156 |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
157 return Orthanc::SystemToolbox::IsExistingFile(path.string()); |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
158 } |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
159 |