Mantid
Loading...
Searching...
No Matches
DownloadFile.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 +
12
13#include "Poco/URI.h"
14
15#include <stdexcept>
16#include <string>
17
18namespace Mantid::DataHandling {
19
24
25// Register the algorithm into the AlgorithmFactory
26DECLARE_ALGORITHM(DownloadFile)
27
28//----------------------------------------------------------------------------------------------
31DownloadFile::DownloadFile() : m_internetHelper(new Kernel::InternetHelper()) {}
32
33//----------------------------------------------------------------------------------------------
37
38//----------------------------------------------------------------------------------------------
39
41const std::string DownloadFile::name() const { return "DownloadFile"; }
42
44int DownloadFile::version() const { return 1; }
45
47const std::string DownloadFile::category() const { return "DataHandling"; }
48
50const std::string DownloadFile::summary() const { return "Downloads a file from a url to the file system"; }
51
52//----------------------------------------------------------------------------------------------
56 declareProperty("Address", "", std::make_shared<MandatoryValidator<std::string>>(),
57 "The address of the network resource to download.", Direction::InOut);
58 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
59 "The filename to save the download to.");
60}
61
62//----------------------------------------------------------------------------------------------
66 std::string address = getProperty("Address");
67 if ((!address.starts_with("http://")) && (!address.starts_with("https://"))) {
68 address = "http://" + address;
69 g_log.information("Address must start http:// or https://, http has been "
70 "assumed to continue: " +
71 address);
72 }
73 std::string filename = getProperty("Filename");
74
75 Poco::URI url(address);
76 m_internetHelper->downloadFile(url.toString(), filename);
77 setProperty("Address", address);
78}
79
80} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
A specialized class for dealing with file properties.
@ Save
to specify a file to write to, the file may or may not exist
DownloadFile : Downloads a file from a url to the file system.
void exec() override
Execute the algorithm.
Kernel::InternetHelper * m_internetHelper
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
const std::string name() const override
Algorithms name for identification.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
virtual ~DownloadFile() override
Destructor.
void init() override
Initialize the algorithm's properties.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
InternetHelper : A helper class for supporting access to resources through HTTP and HTTPS.
virtual HTTPStatus downloadFile(const std::string &urlFile, const std::string &localFilePath="")
Download a url and fetch it inside the local path given.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
Validator to check that a property is not left empty.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ InOut
Both an input & output workspace.
Definition Property.h:55