-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInitDamisServiceFile.cpp
More file actions
195 lines (148 loc) · 6.02 KB
/
InitDamisServiceFile.cpp
File metadata and controls
195 lines (148 loc) · 6.02 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
///////////////////////////////////////////////////////////
// InitDamisServiceFile.cpp
// Implementation of the Class InitDamisServiceFile
// Created on: 30-Sau-2014 17:06:18
// Original author: Povilas
///////////////////////////////////////////////////////////
#define _ELPP_THREAD_SAFE
#define _ELPP_STL_LOGGING
#define _ELPP_NO_DEFAULT_LOG_FILE
#include "InitDamisServiceFile.h"
#include "logging\easylogging++.h"
#include "ErrorResponse.h"
#include "ServiceSettings.h"
#include "curl\curl.h"
#include "HelperMethods.h"
#include <iostream>
InitDamisServiceFile::~InitDamisServiceFile(){
}
/**
* Constructor sets the file names and actually tries to download file, than checks if it is not error stream.
*/
InitDamisServiceFile::InitDamisServiceFile(std::string fURI, std::string pref) : ArffFile (pref){
InitDamisServiceFile::downloadFileURI = fURI;
InitDamisServiceFile::initialize();
}
/**
* Constructor sets the file names
*/
InitDamisServiceFile::InitDamisServiceFile(std::string pref) : ArffFile (pref){
}
/**
* Method that downloads data from the client
*/
bool InitDamisServiceFile::downloadFile(){
LOG(INFO) << "Initiating download file";
if ( !DamisFile::getFileName().empty() && !InitDamisServiceFile::getDownloadFileURI().empty() )
{
CURL *curl;
CURLcode res;
LOG(INFO) << "Initializing file structure for download at location: "<< DamisFile::getFilePath();
struct FtpFile ftpfile =
{
DamisFile::getFilePath(), /* name to store the file as if successful */
NULL
};
curl_global_init(CURL_GLOBAL_DEFAULT);
LOG(INFO) << "Initializing curl object";
curl = curl_easy_init();
if (!curl)
{
LOG(ERROR) << "Error initializing curl";
// ErrorResponse::setFaultDetail("Aaa");
ErrorResponse::setFaultDetail("Error initializing file download");
//printf ("Error initializing Curl");
return false;
}
if(curl)
{
LOG(INFO) << "Setting up curl for file download";
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL, InitDamisServiceFile::getDownloadFileURI().c_str());
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, InitDamisServiceFile::fileWriteDelegeate);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
/* Switch on full protocol/debug output */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(CURLE_OK != res)
{
LOG(ERROR) << "Error curl was not able to download file: " << InitDamisServiceFile::getDownloadFileURI();
ErrorResponse::setFaultDetail(std::string("Error downloading file, invalid download path: ") + InitDamisServiceFile::getDownloadFileURI());
return false;
}
LOG(INFO) << "Done file download from location: "<< InitDamisServiceFile::getDownloadFileURI();
}
if(ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
}
return true;
}
/**
* Delegate function that accepts and stores stream from the client
*/
size_t InitDamisServiceFile::fileWriteDelegeate(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out = (struct FtpFile *)stream;
if(out && !out->stream)
{
out->stream = fopen(out->filename.c_str(), "wb");
if(!out->stream)
{
LOG(ERROR) << "Curl callback can't open local file" << out->filename.c_str() << "to write";
ErrorResponse::setFaultDetail("File download callback failed to stream content of the file to local service file");
return -1; /* failure, can't open file to write */
}
}
return fwrite(buffer, size, nmemb, out->stream);
}
std::string InitDamisServiceFile::getDownloadFileURI()
{
return InitDamisServiceFile::downloadFileURI;
}
/**
* Checks for error stream in downloaded file.
*/
bool InitDamisServiceFile::checkForHttpError()
{
LOG (INFO) << "Checking if downloaded file " << DamisFile::getFilePath()<< " is not error stream";
std::ifstream src(DamisFile::getFilePath(), std::ios::binary);
std::string httpErr1 = "404.0 - Not Found";
std::string line;
bool found = false;
while(std::getline(src,line))
{
if(line.find(httpErr1,0)!=std::string::npos) // string::npos is returned if string is not found
{
found=true;
LOG (ERROR) << "HTTP error stream found in downloaded file: " << DamisFile::getFilePath();
ErrorResponse::setFaultDetail(std::string("File download from ") + InitDamisServiceFile::getDownloadFileURI() + std::string(" was not successful, got HTTP error stream "));
src.close();
HelperMethods::deleteFile(DamisFile::getFilePath());
/* LOG(INFO) << "Preparing to detele file: " << DamisFile::getFilePath();
if (std::remove(DamisFile::getFilePath().c_str()) != 0)
LOG (WARNING) << "Error deleting file: " << DamisFile::getFilePath() <<". Another try to delete file will be after " << ServiceSettings::keepFilesInDirectory << " seconds of service invocation";
else
LOG(INFO) <<"File deleted: " << DamisFile::getFilePath();*/
break;
}
}
if (!found)
LOG (INFO) << "Downloaded file " << DamisFile::getFilePath()<< " is OK";
return !found; //returns true if error was found
}
/**
* Performs file download and http error checking
*/
bool InitDamisServiceFile::initialize(){
if (InitDamisServiceFile::downloadFile())
if (InitDamisServiceFile::checkForHttpError())
if (ArffFile::readArffFile())
return true;
return false;
}