-
Notifications
You must be signed in to change notification settings - Fork 122
/
DownloadFile.cpp
83 lines (66 loc) · 3.13 KB
/
DownloadFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/DownloadFile.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/InternetHelper.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "Poco/URI.h"
#include "boost/algorithm/string/predicate.hpp"
#include "boost/make_shared.hpp"
#include <stdexcept>
#include <string>
namespace Mantid::DataHandling {
using Mantid::API::FileProperty;
using Mantid::Kernel::Direction;
using Mantid::Kernel::MandatoryValidator;
using Mantid::Kernel::StringListValidator;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(DownloadFile)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
DownloadFile::DownloadFile() : m_internetHelper(new Kernel::InternetHelper()) {}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
DownloadFile::~DownloadFile() { delete m_internetHelper; }
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string DownloadFile::name() const { return "DownloadFile"; }
/// Algorithm's version for identification. @see Algorithm::version
int DownloadFile::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string DownloadFile::category() const { return "DataHandling"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string DownloadFile::summary() const { return "Downloads a file from a url to the file system"; }
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void DownloadFile::init() {
declareProperty("Address", "", std::make_shared<MandatoryValidator<std::string>>(),
"The address of the network resource to download.", Direction::InOut);
declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save),
"The filename to save the download to.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void DownloadFile::exec() {
std::string address = getProperty("Address");
if ((!boost::starts_with(address, "http://")) && (!boost::starts_with(address, "https://"))) {
address = "http://" + address;
g_log.information("Address must start http:// or https://, http has been "
"assumed to continue: " +
address);
}
std::string filename = getProperty("Filename");
Poco::URI url(address);
m_internetHelper->downloadFile(url.toString(), filename);
setProperty("Address", address);
}
} // namespace Mantid::DataHandling