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 "boost/algorithm/string/predicate.hpp"
16#include "boost/make_shared.hpp"
17
18#include <stdexcept>
19#include <string>
20
21namespace Mantid::DataHandling {
22
27
28// Register the algorithm into the AlgorithmFactory
29DECLARE_ALGORITHM(DownloadFile)
30
31//----------------------------------------------------------------------------------------------
34DownloadFile::DownloadFile() : m_internetHelper(new Kernel::InternetHelper()) {}
35
36//----------------------------------------------------------------------------------------------
40
41//----------------------------------------------------------------------------------------------
42
44const std::string DownloadFile::name() const { return "DownloadFile"; }
45
47int DownloadFile::version() const { return 1; }
48
50const std::string DownloadFile::category() const { return "DataHandling"; }
51
53const std::string DownloadFile::summary() const { return "Downloads a file from a url to the file system"; }
54
55//----------------------------------------------------------------------------------------------
59 declareProperty("Address", "", std::make_shared<MandatoryValidator<std::string>>(),
60 "The address of the network resource to download.", Direction::InOut);
61 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
62 "The filename to save the download to.");
63}
64
65//----------------------------------------------------------------------------------------------
69 std::string address = getProperty("Address");
70 if ((!boost::starts_with(address, "http://")) && (!boost::starts_with(address, "https://"))) {
71 address = "http://" + address;
72 g_log.information("Address must start http:// or https://, http has been "
73 "assumed to continue: " +
74 address);
75 }
76 std::string filename = getProperty("Filename");
77
78 Poco::URI url(address);
79 m_internetHelper->downloadFile(url.toString(), filename);
80 setProperty("Address", address);
81}
82
83} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
A specialized class for dealing with file properties.
Definition: FileProperty.h:42
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
DownloadFile : Downloads a file from a url to the file system.
Definition: DownloadFile.h:23
void exec() override
Execute the algorithm.
Kernel::InternetHelper * m_internetHelper
Definition: DownloadFile.h:35
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...
Definition: ListValidator.h:29
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
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