added: some work in Export plugin

changed: in base redirect we are using 301 moved permanently status code now (was 303)


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@761 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2011-09-05 15:38:09 +00:00
parent 8c01b0f6c0
commit 392e8060ba
11 changed files with 230 additions and 18 deletions

View File

@@ -6,7 +6,7 @@ name = export.so
all: $(name)
$(name): $(o)
$(CXX) -shared -Wl,-soname,$(name).so -o $(name) $(CXXFLAGS) *.o
$(CXX) -shared -Wl,-soname,$(name).so -o $(name) $(CXXFLAGS) *.o -I/usr/local/include -L/usr/local/lib -lcurl

View File

@@ -1,7 +1,17 @@
# DO NOT DELETE
exportthread.o: exportthread.h ../../core/basethread.h ../../core/synchro.h
exportthread.o: message.h
exportthread.o: message.h ../../core/log.h ../../core/textstream.h
exportthread.o: ../../core/misc.h ../../core/item.h ../../core/requesttypes.h
exportthread.o: ../../core/logmanipulators.h ../../core/slog.h
exportthread.o: ../../core/cur.h ../../core/request.h ../../core/error.h
exportthread.o: ../../core/config.h ../../core/confparser.h
exportthread.o: ../../core/htmlfilter.h ../../templates/htmltextstream.h
exportthread.o: ../../core/textstream.h ../../core/session.h
exportthread.o: ../../core/user.h ../../core/plugindata.h ../../core/rebus.h
exportthread.o: ../../core/mount.h ../../templates/locale.h
exportthread.o: ../../core/confparser.h ../../core/misc.h
exportthread.o: ../../../ezc/src/utf8.h
init.o: ../../core/log.h ../../core/textstream.h ../../core/misc.h
init.o: ../../core/item.h ../../core/requesttypes.h
init.o: ../../core/logmanipulators.h ../../core/slog.h ../../core/cur.h

View File

@@ -7,19 +7,47 @@
*
*/
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <curl/curl.h>
#include <string.h>
#include "exportthread.h"
#include "core/log.h"
#include "core/misc.h"
#include "utf8.h"
namespace Export
{
ExportThread * ExportThread::exp_thread;
ExportThread::ExportThread()
{
exp_thread = 0;
utf8 = false;
}
void ExportThread::SetUTF8(bool use_utf8)
{
utf8 = use_utf8;
}
void ExportThread::AddMessage(const Message & message)
{
message_tab.insert(message_tab.end(), message);
WakeUpThread();
log << log1 << "yes ser" << logend;
}
// first thread (objects locked)
void ExportThread::AddMessage(int type, const std::wstring & url, const std::wstring & path)
{
message_add_temp.type = type;
@@ -27,6 +55,82 @@ void ExportThread::AddMessage(int type, const std::wstring & url, const std::wst
message_add_temp.path = path;
AddMessage(message_add_temp);
WakeUpThread();
}
// second thread
// objects are locked
bool ExportThread::SignalReceived()
{
log << log1 << "------------- a ---------------" << logend;
return !message_tab.empty();
}
// second thread
// objects are not locked
void ExportThread::Do()
{
MessageTab::iterator i;
bool end;
Lock();
log << log1 << "------------- swinka ---------------" << logend;
i = message_tab.begin();
Unlock();
do
{
Lock();
if( i != message_tab.end() )
{
message_work = *i;
message_tab.erase(i++);
end = false;
}
else
{
end = true;
}
Unlock();
if( !end )
DoMessage();
}
while( !end && !IsExitSignal() );
}
// second thread
// objects are not locked
// current message we have in 'message_work'
void ExportThread::DoMessage()
{
Lock();
if( utf8 )
Ezc::WideToUTF8(message_work.url, url_a);
else
AssignString(message_work.url, url_a);
Unlock();
Fetch(url_a.c_str());
Lock();
log << log1 << "sciagnalem takie cos ---------------------------------------------------" << logend;
log << "rozmiar: " << buffer.size() << logend;
log << log1 << "koniec takiego cosia ---------------------------------------------------" << logend << logsave;
Unlock();
}
@@ -34,9 +138,76 @@ void ExportThread::AddMessage(int type, const std::wstring & url, const std::wst
// second thread
// objects are not locked
bool ExportThread::Fetch(const char * url)
{
CURL * curl;
CURLcode res;
long code;
curl = curl_easy_init();
if( !curl )
{
Lock();
log << log1 << "Export: I can't use curl" << logend;
Unlock();
return false;
}
exp_thread = this;
buffer.clear();
/*
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // the http code will be from the last request
*/
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StaticSaveFunction);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Winix");
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if( res!=0 || code!=200 )
{
Lock();
log << log1 << "Ezport: error: operation result: " << (int)res << ", http code: " << code << logend;
Unlock();
return false;
}
return true;
}
size_t ExportThread::StaticSaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
{
if( exp_thread )
return exp_thread->SaveFunction(ptr, size, nmemb, userdata);
return 0;
}
size_t ExportThread::SaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
{
size_t len = size * nmemb;
if( len > 0 )
buffer.append(ptr, len);
Lock();
log << log1 << "odebralem cosik: size: " << size << ", nmemb: " << nmemb << logend;
Unlock();
return len;
}

View File

@@ -24,6 +24,9 @@ class ExportThread : public BaseThread
{
public:
ExportThread();
void SetUTF8(bool use_utf8);
void AddMessage(const Message & message);
void AddMessage(int type, const std::wstring & url, const std::wstring & path);
@@ -34,7 +37,18 @@ private:
typedef std::list<Message> MessageTab;
MessageTab message_tab;
Message message_add_temp;
Message message_work; // a message used by the second thread
static ExportThread * exp_thread;
std::string url_a;
std::string buffer;
bool utf8;
virtual bool SignalReceived();
virtual void Do();
void DoMessage();
bool Fetch(const char * url);
static size_t StaticSaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata);
size_t SaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata);
};

View File

@@ -25,7 +25,7 @@ namespace Export
const wchar_t plugin_name[] = L"export";
int mount_par_export_conf = -1;
ExportThread export_thread;
Message msg;
@@ -46,11 +46,33 @@ void FstabChanged(PluginInfo & info)
}
void InitPlugin(PluginInfo & info)
{
export_thread.SetUTF8(info.config->utf8);
info.system->thread_manager.Add(&export_thread);
}
void SendFile(PluginInfo & info)
{
Item & item = *reinterpret_cast<Item*>(info.p1);
const Item * item = reinterpret_cast<Item*>(info.p1);
log << log1 << "bedziemy wysylac strone o tytule: " << item.subject << ", url: " << item.url << logend;
if( !item )
return;
msg.type = WINIX_PL_EXPORT_TYPE_CREATE_FILE;
msg.url = info.config->url_proto;
msg.url += info.config->base_url;
info.system->dirs.MakePath(item->parent_id, msg.url, false);
msg.url += item->url;
msg.path.clear();
log << log1 << "Export: bede sciagal takiego swiniaka: " << msg.url << logend;
export_thread.AddMessage(msg);
}
@@ -75,6 +97,8 @@ using namespace Export;
plugin.Assign(WINIX_FILE_ADDED, SendFile);
plugin.Assign(WINIX_FILE_CHANGED, SendFile);
plugin.Assign(WINIX_PLUGIN_INIT, InitPlugin);
info.p1 = (void*)(plugin_name);
}