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

FbDownloader.cpp « MyRuLib - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9c012738e602b0c43fcf42223ed37c87d65cee1 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "FbDownloader.h"
#include "MyRuLibApp.h"
#include "FbBookEvent.h"
#include "InfoCash.h"
#include "FbConst.h"
#include "FbParams.h"
#include "FbDatabase.h"
#include "BaseThread.h"
#include "polarssl/md5.h"
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include "FbDataPath.h"

class FbInternetBook
{
	public:
		FbInternetBook(const wxString& md5sum);
		static wxString GetURL(const int id);
		bool Execute();
	private:
		bool DoDownload();
		bool CheckMD5();
		void SaveFile(const bool success);
	private:
		int m_id;
		wxString m_url;
		wxString m_md5sum;
		wxString m_filetype;
		wxString m_filename;
		bool m_zipped;
};

class FbURL: public wxURL
{
	public:
		FbURL(const wxString& sUrl = wxEmptyString);
};

FbURL::FbURL(const wxString& sUrl): wxURL(sUrl)
{
	if (FbParams::GetValue(FB_USE_PROXY))
		SetProxy(FbParams::GetText(FB_PROXY_ADDR));
	GetProtocol().SetTimeout(10);
}

FbInternetBook::FbInternetBook(const wxString& md5sum)
	: m_id(0), m_md5sum(md5sum), m_zipped(false)
{
	wxString sql = wxT("SELECT id, file_type FROM books WHERE md5sum=? AND id>0");
	try {
		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 = FbDownloader::GetURL(m_id);
		}
	} catch (wxSQLite3Exception & e) {
		wxLogError(e.GetMessage());
	}
}

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

bool FbInternetBook::DoDownload()
{
	FbURL url(m_url);
	if (url.GetError() != wxURL_NOERR) {
		wxLogError(wxT("URL error: ") + m_url);
		return false;
	}
	wxHTTP & http = (wxHTTP&)url.GetProtocol();

	wxInputStream * in = url.GetInputStream();
	if (url.GetError() != wxURL_NOERR) {
		wxLogError(wxT("Connect error: ") + m_url);
		return false;
	}
	int responce = http.GetResponse();
	if (responce == 302) {
		m_url = http.GetHeader(wxT("Location"));
		return DoDownload();
	}

	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() : 0xFFFFF;
	size_t count = 0;
	size_t pos = 0;

//	http.GetHeader(wxT("Content-Type")) == "application/zip"
//	Content-Type	application/zip

	md5_context md5;
	md5_starts( &md5 );
	do {
		FbProgressEvent(ID_PROGRESS_UPDATE, m_url, pos*1000/size, _("Загрузка файла")).Post();
		count = in->Read(buf, BUFSIZE).LastRead();
		if (count) md5_update( &md5, buf, (int) count );
		out.Write(buf, count);
		pos += count;
	} while (count);
	FbProgressEvent(ID_PROGRESS_UPDATE).Post();

	if (size != (size_t)-1 && out.GetSize() !=size) {
		wxLogError(wxT("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
		return CheckMD5();
}

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

	bool bNotFound = true;
	if (wxZipEntry * entry = zip.GetNextEntry()) {
		bNotFound = ! zip.OpenEntry(*entry);
		delete entry;
	}
	if (bNotFound) {
		wxLogError(wxT("Zip read error: ") + 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 {
		wxLogError(wxT("Wrong MD5 sum: "), m_url.c_str());
		return false;
	}
	return true;
}

void FbInternetBook::SaveFile(const bool success)
{
	if (success) {
		wxFileName zipname = m_md5sum + (m_zipped ? wxT(".zip") : wxEmptyString);
		zipname.SetPath( FbStandardPaths().GetDownloadDir(true) );
		wxRenameFile(m_filename, zipname.GetFullPath(), true);
	} else {
		wxRemoveFile(m_filename);
	}

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

	try {
		FbLocalDatabase database;
		wxSQLite3Statement stmt = database.PrepareStatement(sql);
		stmt.Bind(1, success ? -1 : -2);
		stmt.Bind(2, m_md5sum);
		stmt.ExecuteUpdate();
	} catch (wxSQLite3Exception & e) {
		wxLogError(e.GetMessage());
	}

	wxLogInfo(wxT("Download finished: ")+ m_url);

	InfoCash::EmptyInfo(m_id);

	FbFolderEvent(ID_UPDATE_FOLDER, 0, FT_DOWNLOAD).Post();

	FbCommandEvent event(fbEVT_BOOK_ACTION, ID_UPDATE_ALLBOOKS);
	event.SetInt(m_id);
	event.Post();
}

bool FbDownloader::sm_running = false;

wxCriticalSection FbDownloader::sm_queue;

void FbDownloader::Start()
{
	wxCriticalSectionLocker locker(sm_queue);

	static bool isNull = true;
	if (isNull) {
		wxURL(strHomePage).GetProtocol().SetTimeout(10);
		wxThread * thread = new FbDownloader();
		if ( thread->Create() == wxTHREAD_NO_ERROR ) thread->Run();
		isNull = false;
	}

	sm_running = true;
}

void FbDownloader::Pause()
{
	wxCriticalSectionLocker locker(sm_queue);
	sm_running = false;
}

bool FbDownloader::IsRunning()
{
	wxCriticalSectionLocker locker(sm_queue);
	return sm_running;
}

void * FbDownloader::Entry()
{
	wxString addr;
	wxString md5sum;
	wxString ext;

	while (true) {
		if (IsRunning()) {
			wxArrayString md5sum;
			GetBooklist(md5sum);
			size_t count = md5sum.Count();
			if (!count) Pause();
			for (size_t i=0; i<count; i++) {
				FbInternetBook(md5sum[i]).Execute();
			}
		}
		wxSleep(3);
	}
	return NULL;
}

void FbDownloader::GetBooklist(wxArrayString &md5sum)
{
	wxString sql = wxT("SELECT md5sum FROM states WHERE download>0 ORDER BY download");
	FbLocalDatabase database;
	wxSQLite3ResultSet result = database.ExecuteQuery(sql);
	while ( result.NextRow() ) {
		md5sum.Add( result.GetString(0) );
	}
}

wxString FbDownloader::GetURL(const int id)
{
	return FbParams::GetText(FB_LIBRUSEC_URL) + wxString::Format(wxT("/b/%d/download"), id);
}