diff --git a/program/Build/RaspberryCloud.sdf b/program/Build/RaspberryCloud.sdf index 95505ab8a40804fc8f804e46c65e4cee59a54e69..955739a198c833a77c7cebaa928dcbf56e7c235a 100644 Binary files a/program/Build/RaspberryCloud.sdf and b/program/Build/RaspberryCloud.sdf differ diff --git a/program/Build/RaspberryCloud.v12.suo b/program/Build/RaspberryCloud.v12.suo index 8874f0914b95b2e01407bce8f8576a8ac2200f50..d016c74cd39840bae5bc413c3ee67a2e37a4220f 100644 Binary files a/program/Build/RaspberryCloud.v12.suo and b/program/Build/RaspberryCloud.v12.suo differ diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj index 8747446c29207e448c7f21025ca411b9746924dc..aca42221e628ce5508623c6789ff9017585b0c9d 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj @@ -94,6 +94,7 @@ <ClCompile Include="..\..\Source\cloud\CloudManager.cpp" /> <ClCompile Include="..\..\Source\cloud\DropboxAdapter.cpp" /> <ClCompile Include="..\..\Source\cloud\ExampleCloudAdapter.cpp" /> + <ClCompile Include="..\..\Source\cloud\FTPAdapter.cpp" /> <ClCompile Include="..\..\Source\cloud\LocalCloudAdapter.cpp" /> <ClCompile Include="..\..\Source\dataAccess\CloudFileSystem.cpp" /> <ClCompile Include="..\..\Source\dataAccess\ComputeOnClient.cpp" /> @@ -119,6 +120,7 @@ <ClInclude Include="..\..\Source\cloud\CloudManager.h" /> <ClInclude Include="..\..\Source\cloud\DropboxAdapter.h" /> <ClInclude Include="..\..\Source\cloud\ExampleCloudAdapter.h" /> + <ClInclude Include="..\..\Source\cloud\FTPAdapter.h" /> <ClInclude Include="..\..\Source\cloud\LocalCloudAdapter.h" /> <ClInclude Include="..\..\Source\dataAccess\Cache.h" /> <ClInclude Include="..\..\Source\dataAccess\CloudFileList.h" /> diff --git a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters index a92faf57f517096e6ec3331a6bf790cdba10f684..a0b9d38c5259d509a6dcc64e4d3a84ba21edac6d 100644 --- a/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters +++ b/program/Build/RaspberryCloud/RaspberryCloud.vcxproj.filters @@ -117,6 +117,9 @@ <ClCompile Include="..\..\Source\cloud\DropboxAdapter.cpp"> <Filter>Source Files\cloud</Filter> </ClCompile> + <ClCompile Include="..\..\Source\cloud\FTPAdapter.cpp"> + <Filter>Source Files\cloud</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\Source\app\UI.h"> @@ -200,5 +203,8 @@ <ClInclude Include="..\..\Source\cloud\DropboxAdapter.h"> <Filter>Header Files\cloud</Filter> </ClInclude> + <ClInclude Include="..\..\Source\cloud\FTPAdapter.h"> + <Filter>Header Files\cloud</Filter> + </ClInclude> </ItemGroup> </Project> \ No newline at end of file diff --git a/program/Source/app/Application.cpp b/program/Source/app/Application.cpp index 5ba38637f7dd47531ef593da7e04356b3d21bff2..0e6216a08ec18d086b1bfa9d69cee7ef1ed6a5aa 100644 --- a/program/Source/app/Application.cpp +++ b/program/Source/app/Application.cpp @@ -1,8 +1,9 @@ #include <string.h> -#include "logger\logger.h" +#include "logger/logger.h" #include "Application.h" -#include "cloud\LocalCloudAdapter.h" -#include <cloud/DropboxAdapter.h> +#include "cloud/LocalCloudAdapter.h" +#include "cloud/DropboxAdapter.h" +#include "cloud/FTPAdapter.h" using namespace std; @@ -27,6 +28,7 @@ int main(int argc, char **argv) { CloudManager cloudMan = CloudManager(); cloudMan.addCloud(make_shared<LocalCloudAdapter>()); cloudMan.addCloud(make_shared<DropboxAdapter>()); + cloudMan.addCloud(make_shared<FTPAdapter>()); Application application(cloudMan); application.cal = CloudAccessLayer(); diff --git a/program/Source/cloud/FTPAdapter.cpp b/program/Source/cloud/FTPAdapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f623410933ef9d09690147f0fea7e7d3890a09e1 --- /dev/null +++ b/program/Source/cloud/FTPAdapter.cpp @@ -0,0 +1,146 @@ +#include "FTPAdapter.h" +#include "logger\\Logger.h" +#include <fstream> +#include "Poco/Net/FTPClientSession.h" +#include "Poco/Net/NetException.h" +#include "Poco/Exception.h" +#include "Poco/StreamCopier.h" +#include <iostream> +#include <memory> +#include <filesystem> +#include <Poco/Path.h> + +using namespace Poco::Net; +using namespace Poco; + +const string FTPAdapter::fragFolder = "C:\\Users\\krisz\\raspberrycloud\\skeleton\\Program\\temp\\"; +const string FTPAdapter::USERNAME = "b18_16883957"; +const string FTPAdapter::PASSWORD = "lolwutdoge"; +const string FTPAdapter::HOST = "ftp.byethost18.com"; + +FTPAdapter::FTPAdapter(string _cloudID) : CloudAdapter(_cloudID) { LOG_ENTER_EXIT; } + +bool FTPAdapter::upload(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + // Get file + auto localFile(fragFolder + cloudFile.getCloudFileID()); + ifstream in(localFile, ios::in | ios::binary); + + if (!in) + return false; // If file can't be opened + + Path filePath(cloudFile.getCloudFileID()); + FTPClientSession session(HOST, FTPClientSession::FTP_PORT, USERNAME, PASSWORD); + + try + { + for (auto i = 0; i < filePath.depth(); i++) + { + string dirName(filePath.directory(i)); + + try + { + session.setWorkingDirectory(dirName); + } + catch (FTPException&) + { + // Directory doesn't exist, try to create it + try + { + session.createDirectory(dirName); + session.setWorkingDirectory(dirName); + } + catch (FTPException& ex) + { + // Error creating directory + cerr << ex.message() << endl; + return 1; + } + } + } + + session.setFileType(FTPClientSession::TYPE_BINARY); + auto& os = session.beginUpload(filePath.getFileName()); + StreamCopier::copyStream(in, os); + session.endUpload(); + + // delete local fragmant + remove(localFile.c_str()); + + return true; + } + catch (FTPException& e) + { + cerr << "error: " << e.message() << endl; + return false; + } +} + +shared_ptr<Fragment> FTPAdapter::download(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + FTPClientSession session(HOST, FTPClientSession::FTP_PORT, USERNAME, PASSWORD); + + tr2::sys::path localFilePath = fragFolder + cloudFile.getCloudFileID(); + + if (!exists(localFilePath.parent_path())) + { + create_directories(localFilePath.parent_path()); + } + + ofstream file(localFilePath, ios::out | ios::binary); + + try + { + session.setFileType(FTPClientSession::TYPE_BINARY); + auto& is = session.beginDownload(cloudFile.getCloudFileID()); + StreamCopier::copyStream(is, file); + session.endDownload(); + return make_shared<Fragment>(cloudFile);; + } + catch (FTPException& e) + { + cerr << "error: " << e.message() << endl; + return nullptr; + } +} + +bool FTPAdapter::deleteFile(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + FTPClientSession session(HOST, FTPClientSession::FTP_PORT, USERNAME, PASSWORD); + + try + { + session.remove(cloudFile.getCloudFileID()); + return true; + } + catch (FTPException& e) + { + cerr << "error: " << e.message() << endl; + return false; + } +} + +bool FTPAdapter::auth() { + LOG_ENTER_EXIT; + return true; +} + +long FTPAdapter::getSize(CloudFile cloudFile) { + LOG_ENTER_EXIT; + + FTPClientSession session(HOST, FTPClientSession::FTP_PORT, USERNAME, PASSWORD); + + string response; + session.sendCommand("SIZE " + cloudFile.getCloudFileID(), response); + + if (response.find("213 ") != string::npos) + return stol(response.substr(4)); + return -1; +} + +bool FTPAdapter::checkExists(CloudFile cloudFile){ + return getSize(cloudFile) >= 0; +} \ No newline at end of file diff --git a/program/Source/cloud/FTPAdapter.h b/program/Source/cloud/FTPAdapter.h new file mode 100644 index 0000000000000000000000000000000000000000..a251f2e8610a7da087666ae15f4efcfdf6c7e54c --- /dev/null +++ b/program/Source/cloud/FTPAdapter.h @@ -0,0 +1,65 @@ +#ifndef _FTPADAPTER_H +#define _FTPADAPTER_H + +#include <string> +#include "fileModel\CloudFile.h" +#include "cloud\CloudAdapter.h" +#include "fileModel\Fragment.h" + +/** +* DropboxAdapter class +*/ +class FTPAdapter : public CloudAdapter { + static const string fragFolder; + static const string USERNAME; + static const string PASSWORD; + static const string HOST; + +public: + string cloudID; + + explicit FTPAdapter(string _cloudID = "FTPAdapter"); + + /** + * Uploads a file to the cloud + * @param cloudFile file to upload + * @return successful + */ + bool upload(CloudFile cloudFile) override; + + /** + * Downloads a file from the cloud + * @param cloudFile file to download + * @return a downloaded file-fragment + */ + shared_ptr<Fragment> download(CloudFile cloudFile) override; + + /** + * Deletes a file from the cloud + * @param cloudFile file to download + * @return successful + */ + bool deleteFile(CloudFile cloudFile) override; + + /** + * Established authentication + * @return successful + */ + bool auth() override; + + /** + * Measures a files size on the cloud + * @param cloudFile file to measure + * @return size of file + */ + long getSize(CloudFile cloudFile) override; + + /** + * Checks if the file exists. + * @param cloudFile descriptor of the file to check + * @return true if the file exists, false otherwise + */ + bool checkExists(CloudFile cloudFile) override; +}; + +#endif //_FTPADAPTER_H \ No newline at end of file