Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FbInternetBook.cpp « MyRuLib « sources - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0a50b54df6738f32f1950ac24ade92194303e7bb (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "FbInternetBook.h"
#include "BaseThread.h"
#include "MyRuLibApp.h"
#include "FbBookEvent.h"
#include "FbCollection.h"
#include "FbConst.h"
#include "FbParams.h"
#include "FbDatabase.h"
#include "polarssl/md5.h"
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/curl/http.h>
#include "FbDataPath.h"
#include "FbDateTime.h"
#include "controls/FbURL.h"

wxString FbInternetBook::GetURL(const int id, const wxString& md5sum)
{
	wxString host = FbParams::GetStr(DB_DOWNLOAD_HOST);
	if (FbParams::IsGenesis()) {
		wxString key = md5sum.IsEmpty() ? FbCommonDatabase().GetMd5(id) : md5sum;
		wxString addr = wxT("http://%s/get?nametype=orig&md5=%s");
		return wxString::Format(addr, host.c_str(), key.c_str());
	} else {
		wxString addr = wxT("http://%s/b/%d/download");
		return wxString::Format(addr, host.c_str(), id);
	}
}

FbInternetBook::FbInternetBook(FbDownloader * owner, const wxString& md5sum)
	: m_id(0), m_owner(owner), m_md5sum(md5sum), m_zipped(false)
{
	wxString sql = wxT("SELECT id, file_type FROM books WHERE md5sum=? AND id>0");

	FbCommonDatabase database;
	wxSQLite3Statement stmt = database.PrepareStatement(sql);
	stmt.Bind(1, md5sum);
	wxSQLite3ResultSet result = stmt.ExecuteQuery();
	if ( result.NextRow() ) {
		m_id = result.GetInt(0);
		m_filetype = result.GetString(1);
		m_url = GetURL(m_id, m_md5sum);
	}
}

bool FbInternetBook::Execute()
{
	bool result = m_id && DoDownload();
	SaveFile(result);
	return result;
}

bool FbInternetBook::DoDownload()
{
	wxString user = FbParams::GetStr(DB_DOWNLOAD_USER);
	if ( user.IsEmpty() ) return DownloadUrl();

	wxString host = FbParams::GetStr(DB_DOWNLOAD_HOST);
	wxString pass = FbParams::GetStr(DB_DOWNLOAD_PASS);
	wxString addr = wxString::Format(wxT("http://%s/b/%d/get?destination=b/%d/get"), host.c_str(), m_id, m_id);
	FbLogMessage(_("Download"), addr);

	FbURL url(addr);
	if (url.GetError() != wxURL_NOERR) {
		FbLogError(_("URL error"), m_url);
		return false;
	}
	wxHTTP & http = (wxHTTP&)url.GetProtocol();
    http.SetTimeout(FbParams::GetInt(FB_WEB_TIMEOUT));
    http.SetHeader(wxT("Content-type"), wxT("application/x-www-form-urlencoded"));
    wxString buffer = wxString::Format(wxT("form_id=user_login_block&name=%s&pass=%s"), user.c_str(), pass.c_str());
    http.SetPostBuffer(buffer);

	if (m_owner->IsClosed()) return false;

	wxInputStream * in = url.GetInputStream();
	if (url.GetError() != wxURL_NOERR) {
		FbLogError(_("Connect error"), m_url);
		return false;
	}

	wxString cookie = http.GetHeader(wxT("Set-Cookie")).BeforeFirst(wxT(';'));
	if (http.GetResponse() == 302) {
		m_url = http.GetHeader(wxT("Location"));
		FbLogMessage(_("Redirect"), m_url);
		return DownloadUrl(cookie);
	}

	bool ok = ReadFile(in);
	if ( !ok ) { FbLogError(_("Authentication failure"), m_url); }
	return ok;
}

bool FbInternetBook::DownloadUrl(const wxString &cookie)
{
	if (m_owner->IsClosed()) return false;

	m_filename = wxFileName::CreateTempFileName(wxT("~"));

	wxCurlHTTP url(m_url);
	url.SetOpt(CURLOPT_TIMEOUT, FbParams::GetInt(FB_WEB_TIMEOUT));
	url.SetOpt(CURLOPT_CONNECTTIMEOUT, FbParams::GetInt(FB_WEB_TIMEOUT));
	bool ok = url.Get(m_filename);

	if (!ok) {
		FbLogError(_("Connect error"), m_url);
		return false;
	}

	return ok;
}

bool FbInternetBook::ReadFile(wxInputStream * in)
{
	m_filename = wxFileName::CreateTempFileName(wxT("~"));
	wxFileOutputStream out(m_filename);

	const size_t BUFSIZE = 1024;
	unsigned char buf[BUFSIZE];
	size_t size = in->GetSize() ? in->GetSize() : 0xFFFFFF;
	size_t count = 0;
	size_t pos = 0;

	bool zipped = false;
	md5_context md5;
	md5_starts( &md5 );
	do {
		if (m_owner->IsClosed()) return false;
		FbProgressEvent(ID_PROGRESS_UPDATE, m_url, pos * 1000 / size, _("File download")).Post();
		count = in->Read(buf, BUFSIZE).LastRead();
		if ( count ) md5_update( &md5, buf, (int) count );
		if ( pos==0 && count>1 && buf[0]=='P' && buf[1]=='K') zipped = true;
		out.Write(buf, count);
		pos += count;
	} while (count);

	if (m_owner->IsClosed()) return false;
	FbProgressEvent(ID_PROGRESS_UPDATE).Post();

	if (size != (size_t)-1 && out.GetSize() !=size) {
		wxLogError(_("HTTP read error, read %d of %d bytes: %s"), out.GetSize(), size, m_url.c_str());
		return false;
	}

	wxString md5sum = BaseThread::CalcMd5(md5);
	if ( md5sum == m_md5sum )
		return true;
	else if ( zipped )
		return CheckZip();

	FbLogError(_("Download error"), m_url);
	return false;
}

bool FbInternetBook::CheckZip()
{
	wxFFileInputStream in(m_filename);
	wxZipInputStream zip(in);

	bool bNotFound = true;
	while (wxZipEntry * entry = zip.GetNextEntry()) {
		bool ok = (entry->GetInternalName().Right(4).Lower() != wxT(".fbd"));
		if (ok) bNotFound = ! zip.OpenEntry(*entry);
		delete entry;
		if (ok) break;
	}
	if (bNotFound) {
		FbLogError(_("Can't open zip"), m_url);
		return false;
	}

	const size_t BUFSIZE = 1024;
	unsigned char buf[BUFSIZE];
	size_t count;
	md5_context md5;
	md5_starts( &md5 );
	do {
		count = zip.Read(buf, BUFSIZE).LastRead();
		if (count) md5_update( &md5, buf, (int) count );
	} while (count);

	wxString md5sum = BaseThread::CalcMd5(md5);
	if ( md5sum == m_md5sum ) {
		m_zipped = true;
	} else {
		FbLogError(_("Wrong MD5 sum"), m_url);
		return false;
	}
	return true;
}

void FbInternetBook::SaveFile(const bool success)
{
	if (success) {
		wxFileName zipname = FbDownloader::GetFilename(m_md5sum, true);
		if (m_zipped) zipname.SetExt(wxT("zip"));
		wxRenameFile(m_filename, zipname.GetFullPath(), true);
	} else {
		wxRemoveFile(m_filename);
	}

	if (m_owner->IsClosed()) return;

	wxString sql = wxT("UPDATE states SET download=? WHERE md5sum=?");

	int code = success ? FbDateTime::Today().Code() : 1;
	FbLocalDatabase database;
	wxSQLite3Statement stmt = database.PrepareStatement(sql);
	stmt.Bind(1, code);
	stmt.Bind(2, m_md5sum);
	stmt.ExecuteUpdate();

	if (m_owner->IsClosed()) return;

	if (success) {
		FbCollection::ResetBook(m_id);
		FbCollection::ResetInfo(m_id);
		FbFolderEvent(ID_UPDATE_FOLDER, 0, FT_DOWNLOAD).Post();
//		FbCommandEvent(fbEVT_BOOK_ACTION, ID_UPDATE_BOOK, m_id).Post();
		FbLogMessage(_("Download finished"), m_url);
	}

	{
		FbMasterDownInfo info = FbMasterDownInfo::DT_WAIT;
		FbMasterEvent(ID_UPDATE_MASTER, info, m_id, false).Post();
	}

	{
		FbMasterDownInfo info = success ? FbMasterDownInfo::DT_READY : FbMasterDownInfo::DT_ERROR;
		FbMasterEvent(ID_UPDATE_MASTER, info, m_id, true).Post();
	}
}