///////////////////////////////////////////////////////////////////////////// // Name: ftpfilecache.cpp // tag: aBridge ftp file cache implementation. // Author: David Roundy // Modified by: // Copyright: (c) 2002 David Roundy // Licence: GPL //--------------------------------------------------------------------------- // Last modified: /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes . #include #ifndef WX_PRECOMP #include #include #include #include #include #endif #include "debug.h" #include "config.h" #include "ftpfilecache.h" #include "ftpprivate.h" #include "md5.h" // My dealer constructor ftpFileCache::ftpFileCache(const wxString &cachedir, const wxString &servername, const wxString &username, const wxString &password) { if (!wxDirExists(cachedir)) { if (wxMkdir(cachedir)) { DebugMsg("Created cache directory: " + cachedir); } else { DebugMsg("Couldn't create cache directory: " + cachedir); } } m_cachedir = cachedir; if (wxDirExists(cachedir)) { wxSetWorkingDirectory(cachedir); DebugMsg("Using " + cachedir + " as my working directory"); } else { DebugMsg("Oh no, there is no such dir as " + cachedir); } m_server = servername; m_username = username; m_password = password; } ftpFileCache::~ftpFileCache() { } void ftpFileCache::GetFile(const wxString &filename) { if (wxFileExists(filename)) { DebugMsg("We already have " + filename + " so no need to fetch it"); } else { #ifdef USE_THREADS ftpGetter *mygetter = new ftpGetter(filename, filename, m_server, m_username, m_password); mygetter->Create(); mygetter->Run(); #else //mygetter->Entry(); //delete mygetter; #endif } } wxString ftpFileCache::PutFile(const wxString &filename) { if (filename.IsSameAs("")) { DebugMsg("Aaack the filename is empty"); return "unknown.png"; } if (!wxFileExists(filename)) { DebugMsg("file '" + filename + "' does not exist"); return "unknown.png"; } wxFileInputStream *in_stream = new wxFileInputStream(filename); if (!in_stream || (in_stream->LastError() != wxStream_NOERROR)) { DebugMsg("error opening file '" + filename + "' for reading"); return "unknown.png"; } else { DebugMsg("Successfully opened file '" + filename + "' for reading"); } long the_size=in_stream->StreamSize(); if (the_size == 0) { DebugMsg("Aaack the file is empty"); return "unknown.png"; } char *data = new char[the_size]; in_stream->Read(data, the_size); wxInt32 i; // make hash here unsigned char my_buffer[16]; // I am trusting that the md5 code is bugfree. md5_buffer(data, the_size, my_buffer); wxString md5sum = ""; for (i=0;i<16;i++) md5sum += wxString::Format("%02x", (unsigned int) my_buffer[i]); md5sum += "."; md5sum += filename.AfterLast('.'); DebugMsg("I got an md5sum of " + md5sum); if (!wxCopyFile(filename, md5sum)) { DebugMsg("Error copying file " + filename + " to cache as " + md5sum); } delete[] data; delete in_stream; #ifdef USE_THREADS ftpPutter *myputter = new ftpPutter(filename, md5sum, m_server, m_username, m_password); myputter->Create(); myputter->Run(); #else //myputter->Entry(); //delete myputter; #endif return md5sum; } //////////////////////////////////////////////////////// // ftpGetter //////////////////////////////////////////////////////// ftpGetter::ftpGetter(const wxString &remotefilename, const wxString &localfilename, const wxString &servername, const wxString &username, const wxString &password) : wxThread(wxTHREAD_DETACHED) { m_remotefilename = remotefilename; m_localfilename = localfilename; m_servername = servername; m_username = username; m_password = password; } ftpGetter::~ftpGetter() { } void *ftpGetter::Entry() { wxFTP ftp; wxInputStream *in_stream; DebugMsg("Connecting to " + m_servername + " as " + m_username); ftp.SetUser(m_username); ftp.SetPassword(m_password); if (!ftp.Connect(m_servername)) { DebugMsg("Error connecting to " + m_servername); return NULL; } in_stream = ftp.GetInputStream(m_remotefilename); if (!in_stream || in_stream->LastError() != wxStream_NOERROR) { DebugMsg("Problem finding file" + m_remotefilename); return NULL; } DebugMsg("Reading file " + m_remotefilename); wxFileOutputStream *out_stream = new wxFileOutputStream(m_localfilename); if (!out_stream || out_stream->LastError() != wxStream_NOERROR) { DebugMsg("error opening file '" + m_localfilename + "' for output"); return NULL; } out_stream->Write(*in_stream); DebugMsg("Finished getting file " + m_remotefilename); delete in_stream; /* Close the DATA connection */ delete out_stream; /* Close the file */ ftp.Close(); /* Close the COMMAND connection */ return NULL; } //////////////////////////////////////////////////////// // ftpPutter //////////////////////////////////////////////////////// ftpPutter::ftpPutter(const wxString &localfilename, const wxString &remotefilename, const wxString &servername, const wxString &username, const wxString &password) : wxThread(wxTHREAD_DETACHED) { m_remotefilename = remotefilename; m_localfilename = localfilename; m_servername = servername; m_username = username; m_password = password; } ftpPutter::~ftpPutter() { } void *ftpPutter::Entry() { wxFTP ftp; wxOutputStream *out_stream; wxFileInputStream *in_stream = new wxFileInputStream(m_localfilename); if (!in_stream || in_stream->LastError() != wxStream_NOERROR) { DebugMsg("error opening file " + m_localfilename + "for reading"); return NULL; } DebugMsg("Connecting to " + m_servername + " as " + m_username); ftp.SetUser(m_username); ftp.SetPassword(m_password); if (!ftp.Connect(m_servername)) { DebugMsg("Error connecting to " + m_servername); return NULL; } out_stream = ftp.GetOutputStream(m_remotefilename); if (!out_stream || out_stream->LastError() != wxStream_NOERROR) { DebugMsg("Problem creating remote file " + m_remotefilename); DebugMsg("Perhaps " + m_remotefilename + " is already on server?"); return NULL; } DebugMsg("Uploading file " + m_remotefilename); out_stream->Write(*in_stream); DebugMsg("Finished uploading file " + m_remotefilename); delete in_stream; /* Close the DATA connection */ delete out_stream; /* Close the file */ ftp.Close(); /* Close the COMMAND connection */ return NULL; }