Mantid
Loading...
Searching...
No Matches
ISISDataArchive.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
15#include <filesystem>
16
17namespace Mantid::DataHandling {
18namespace {
20Kernel::Logger g_log("ISISDataArchive");
21} // namespace
22
23DECLARE_ARCHIVESEARCH(ISISDataArchive, ISISDataSearch)
24
25namespace {
26#ifdef _WIN32
27constexpr std::string_view URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/windir?name=";
28#else
29constexpr std::string_view URL_PREFIX = "http://data.isis.rl.ac.uk/where.py/unixdir?name=";
30#endif
31} // namespace
32
41const API::Result<std::string> ISISDataArchive::getArchivePath(const std::set<std::string> &filenames,
42 const std::vector<std::string> &exts) const {
43 if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
44 for (const auto &filename : filenames) {
45 g_log.debug() << filename << ")\n";
46 }
47 for (const auto &ext : exts) {
48 g_log.debug() << ext << ")\n";
49 }
50 }
51
52 std::string errors = "";
53 for (const auto &filename : filenames) {
54 std::string path_without_extension = getPath(filename);
55 if (!path_without_extension.empty()) {
56#ifdef __APPLE__
57 // Replace Linux path with macOS path if we're on a Mac.
58 constexpr std::string_view unixString = "/archive/";
59 constexpr std::string_view macOSString = "/Volumes/inst$/";
60 path_without_extension.replace(0, unixString.length(), macOSString);
61#endif
62 std::string fullPath = getCorrectExtension(path_without_extension, exts);
63 if (!fullPath.empty())
64 return API::Result<std::string>(fullPath);
65 errors += "No file found. ";
66#ifdef __linux__
67 errors +=
68 "If you are an IDAaaS user, and your file is in the ISIS archive, then check if the archive is mounted. If it is not mounted, \
69 \nthen click Applications->Data->Experiment Archive (Staff Only), and enter your federal ID credentials with 'clrc' as the domain.";
70#endif // __linux__
71 }
72 }
73 return API::Result<std::string>("", errors);
74}
75
81std::string ISISDataArchive::getPath(const std::string &fName) const {
82 g_log.debug() << "ISISDataArchive::getPath() - fName=" << fName << "\n";
83 if (fName.empty())
84 return ""; // Avoid pointless call to service
85
86 std::ostringstream os = sendRequest(fName);
87 os << std::string(1, std::filesystem::path::preferred_separator) << fName;
88 std::string expectedPath = os.str();
89 return expectedPath;
90}
91
100std::ostringstream ISISDataArchive::sendRequest(const std::string &fName) const {
101 Kernel::InternetHelper inetHelper;
102 std::ostringstream os;
103 try {
104 inetHelper.sendRequest(URL_PREFIX.data() + fName, os);
106 g_log.warning() << "Could not access archive index." << ie.what();
107 }
108 return os;
109}
110
120std::string ISISDataArchive::getCorrectExtension(const std::string &path, const std::vector<std::string> &exts) const {
121 for (const auto &ext : exts) {
122 std::string temp_path = path + ext;
123 if (fileExists(temp_path))
124 return temp_path;
125 }
126 return "";
127}
128
136bool ISISDataArchive::fileExists(const std::string &path) const {
137 try {
138 if (std::filesystem::exists(path))
139 return true;
140 } catch (const std::filesystem::filesystem_error &) {
141 }
142 return false;
143}
144
145} // namespace Mantid::DataHandling
#define DECLARE_ARCHIVESEARCH(classname, facility)
std::string getPath(const std::string &fName) const
Gets the path to the file, or most recent set of files.
virtual std::string getCorrectExtension(const std::string &path, const std::vector< std::string > &exts) const
Public and virtual for testing purposes.
virtual bool fileExists(const std::string &path) const
Checks if the given file path exists or not.
const API::Result< std::string > getArchivePath(const std::set< std::string > &filenames, const std::vector< std::string > &exts) const override
Returns the path to a filename given the list of extensions to try.
virtual std::ostringstream sendRequest(const std::string &fName) const
Queries the archive & returns the path to a single file.
Exception thrown when error occurs accessing an internet resource.
Definition Exception.h:321
const char * what() const noexcept override
Overloaded reporting method.
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
virtual HTTPStatus sendRequest(const std::string &url, std::ostream &responseStream)
Performs a request using http or https depending on the url.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
bool is(int level) const
Returns true if at least the given log level is set.
Definition Logger.cpp:177
Kernel::Logger g_log("ExperimentInfo")
static logger object