Mercurial > hg > orthanc-object-storage
annotate Common/FileSystemStorage.cpp @ 210:408c90c9027f default tip
todo: google soft delete
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Wed, 09 Oct 2024 11:48:14 +0200 |
parents | d62f52be1943 |
children |
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 |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
24 #include <Logging.h> |
77 | 25 #include <SystemToolbox.h> |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
26 |
77 | 27 #include <boost/filesystem.hpp> |
28 #include <boost/filesystem/fstream.hpp> | |
29 | |
30 namespace fs = boost::filesystem; | |
31 | |
32 void FileSystemStoragePlugin::FileSystemWriter::Write(const char* data, size_t size) | |
33 { | |
34 Orthanc::SystemToolbox::MakeDirectory(path_.parent_path().string()); | |
35 | |
36 Orthanc::SystemToolbox::WriteFile(reinterpret_cast<const void*>(data), size, path_.string(), fsync_); | |
37 } | |
38 | |
39 size_t FileSystemStoragePlugin::FileSystemReader::GetSize() | |
40 { | |
41 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string())) | |
42 { | |
43 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
44 } | |
45 | |
46 try | |
47 { | |
48 fs::ifstream f; | |
49 f.open(path_.string(), std::ifstream::in | std::ifstream::binary); | |
50 if (!f.good()) | |
51 { | |
52 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
53 } | |
54 | |
55 f.seekg(0, std::ios::end); | |
56 std::streamsize fileSize = f.tellg(); | |
57 f.seekg(0, std::ios::beg); | |
58 | |
59 f.close(); | |
60 | |
61 return fileSize; | |
62 } | |
63 catch (...) | |
64 { | |
65 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string()); | |
66 } | |
67 | |
68 } | |
69 | |
70 void FileSystemStoragePlugin::FileSystemReader::ReadWhole(char* data, size_t size) | |
71 { | |
72 ReadRange(data, size, 0); | |
73 } | |
74 | |
75 void FileSystemStoragePlugin::FileSystemReader::ReadRange(char* data, size_t size, size_t fromOffset) | |
76 { | |
77 if (!Orthanc::SystemToolbox::IsRegularFile(path_.string())) | |
78 { | |
79 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
80 } | |
81 | |
82 try | |
83 { | |
84 fs::ifstream f; | |
85 f.open(path_.string(), std::ifstream::in | std::ifstream::binary); | |
86 if (!f.good()) | |
87 { | |
88 throw StoragePluginException(std::string("The path does not point to a regular file: ") + path_.string()); | |
89 } | |
90 | |
91 f.seekg(fromOffset, std::ios::beg); | |
92 | |
93 f.read(reinterpret_cast<char*>(data), size); | |
94 | |
95 f.close(); | |
96 } | |
97 catch (...) | |
98 { | |
99 throw StoragePluginException(std::string("Unexpected error while reading: ") + path_.string()); | |
100 } | |
101 } | |
102 | |
103 | |
104 | |
78 | 105 IStorage::IWriter* FileSystemStoragePlugin::GetWriterForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
77 | 106 { |
78 | 107 return new FileSystemWriter(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_), fsync_); |
77 | 108 } |
109 | |
78 | 110 IStorage::IReader* FileSystemStoragePlugin::GetReaderForObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) |
77 | 111 { |
78 | 112 return new FileSystemReader(BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_)); |
77 | 113 } |
114 | |
115 void FileSystemStoragePlugin::DeleteObject(const char* uuid, OrthancPluginContentType type, bool encryptionEnabled) | |
116 { | |
117 try | |
118 { | |
119 namespace fs = boost::filesystem; | |
120 | |
78 | 121 fs::path path = BaseStorage::GetOrthancFileSystemPath(uuid, fileSystemRootPath_); |
77 | 122 |
123 try | |
124 { | |
125 fs::remove(path.string()); | |
126 } | |
127 catch (...) | |
128 { | |
129 // Ignore the error | |
130 } | |
131 | |
132 // Remove the two parent directories, ignoring the error code if | |
133 // these directories are not empty | |
134 | |
135 try | |
136 { | |
137 boost::system::error_code err; | |
138 fs::remove(path.parent_path().string(), err); | |
139 fs::remove(path.parent_path().parent_path().string(), err); | |
140 } | |
141 catch (...) | |
142 { | |
143 // Ignore the error | |
144 } | |
145 } | |
146 catch(Orthanc::OrthancException& e) | |
147 { | |
152
d62f52be1943
use Orthanc frameworking for logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
145
diff
changeset
|
148 LOG(ERROR) << GetNameForLogs() << ": error while deleting object " << uuid << ": " << e.What(); |
77 | 149 } |
150 | |
151 } | |
152 | |
111
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
153 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
|
154 { |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
155 namespace fs = boost::filesystem; |
77 | 156 |
111
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
157 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
|
158 |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
159 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
|
160 } |
407bd022b0cf
in /move-storage: now detecting if file should be moved or not
Alain Mazy <am@osimis.io>
parents:
78
diff
changeset
|
161 |