-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathDownloadThread.cpp
More file actions
171 lines (134 loc) · 5.26 KB
/
Copy pathDownloadThread.cpp
File metadata and controls
171 lines (134 loc) · 5.26 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* DownloadThread
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/ScopeExit.h"
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "CommonFramework/GlobalAutoPaths.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/Tools/FileDownloader.h"
#include "CommonFramework/Tools/FileUnzip.h"
#include "CommonFramework/Tools/FileHash.h"
#include "CommonFramework/Logging/Logger.h"
#include "DownloadThread.h"
#include <iostream>
using std::cout;
using std::endl;
namespace PokemonAutomation{
DownloadThread::~DownloadThread(){
// cout << "~DownloadThread" << endl;
this->cancel();
m_worker.wait_and_ignore_exceptions();
}
DownloadThread::DownloadThread(Hooks hooks, const DownloadedResourceMetadata& remote_metadata, Mutex& lock, ConditionVariable& cv)
: m_hooks(std::move(hooks))
, m_remote_metadata(remote_metadata)
, m_download_lock(lock)
, m_download_cv(cv)
{}
void DownloadThread::start_download_thread(){
m_worker = GlobalThreadPools::unlimited_normal().dispatch_now_blocking(
[this]{
bool success = false;
{
std::unique_lock<Mutex> lock(m_download_lock);
m_download_cv.wait(lock, [this] { return m_hooks.is_ready_to_start(); });
}
// runs when lambda is finished
ScopeExit on_exit([&, this]{
m_hooks.on_finished(success);
});
try {
// Logger& logger = global_logger_tagged();
// throw_and_log<OperationFailedException>(logger, ErrorReport::NO_ERROR_REPORT,
// "Test");
// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Test.");
// std::this_thread::sleep_for(std::chrono::seconds(7));
run_download(m_remote_metadata);
cout << "Done Download" << endl;
success = true;
}catch(OperationCancelledException&){
// user cancelled action
success = false;
}catch(OperationFailedException&){
success = false;
m_hooks.report_failed();
}catch (const std::exception& e) {
std::string error_msg = "DownloadThread::start_download_thread: " + std::string(e.what());
success = false;
m_hooks.report_exception_caught(error_msg.c_str());
} catch(...){
success = false;
m_hooks.report_exception_caught("DownloadThread::start_download_thread: Unknown exception. Report this as an error.");
}
}
);
}
void DownloadThread::run_download(DownloadedResourceMetadata resource_metadata){
Logger& logger = global_logger_tagged();
// std::this_thread::sleep_for(std::chrono::seconds(5));
std::string url = resource_metadata.url;
std::string resource_name = resource_metadata.resource_name;
uint64_t expected_size = resource_metadata.size_compressed_bytes;
std::string resource_directory = DOWNLOADED_RESOURCE_PATH() + resource_name;
try{
// delete directory and the old resource
Filesystem::remove_all(resource_directory);
// download
std::string zip_path = resource_directory + "/temp.zip";
FileDownloader::download_file_to_disk(
*this,
logger,
url,
zip_path,
expected_size,
[this](uint64_t bytes_done, uint64_t total_bytes){
m_hooks.report_download_progress(bytes_done, total_bytes);
}
);
// hash
std::string hash =
hash_file(
this,
zip_path,
[this](uint64_t bytes_done, uint64_t total_bytes){
m_hooks.report_hash_progress(bytes_done, total_bytes);
}
);
std::string expected_hash = resource_metadata.sha256;
if (hash != expected_hash){
std::cerr << "current hash: " << hash << endl;
throw_and_log<OperationFailedException>(logger, ErrorReport::NO_ERROR_REPORT,
"Downloaded file failed verification. SHA 256 hash did not match the expected value.");
}
// Filesystem::Path p{zip_path};
// cout << "File size: " << std::filesystem::file_size(p) << endl;
// unzip
unzip_file(
*this,
zip_path.c_str(),
resource_directory.c_str(),
[this](uint64_t bytes_done, uint64_t total_bytes){
m_hooks.report_unzip_progress(bytes_done, total_bytes);
}
);
// delete old zip file
Filesystem::remove(zip_path);
throw_if_cancelled();
}catch(OperationCancelledException&){
// delete directory and the resource
Filesystem::remove_all(resource_directory);
throw;
}catch(OperationFailedException&){
// delete directory and the resource
Filesystem::remove_all(resource_directory);
throw;
}catch(...){
// delete directory and the resource
Filesystem::remove_all(resource_directory);
throw;
}
}
}