#include "../include/FileManager.h" FileManager::FileManager() {} FileManager::~FileManager() { cancelPut(); closeGetFile(); } bool FileManager::isUploading() { return this->putFile.is_open(); } bool FileManager::isDownloading() { return this->getFile.is_open(); } bool FileManager::openPutFile(const std::string &filename) { this->putBaseFileName = filename; this->putFileName = this->fileDirectory; this->putFileName.append(filename); // open file and test if it already exists this->putFile.open(this->putFileName, std::ios::app | std::ios::binary); if (this->putFile.tellp() != std::ios::beg) { // file already exists closePutFile(); return false; } else { return true; } } bool FileManager::openGetFile(const std::string &filename, int &chunks) { this->getBaseFileName = filename; std::string file = this->fileDirectory; file.append(filename); this->getFile.open(file, std::ios::ate | std::ios::binary); if (this->getFile.is_open() == 0) { // file does not exist or cannot be opened return false; } else { size_t size = this->getFile.tellg(); chunks = size / max_data_length + (size % max_data_length == 0 ? 0 : 1); this->getFile.seekg(std::ios::beg); return true; } } void FileManager::closePutFile() { this->putFile.close(); } void FileManager::closeGetFile() { this->getFile.close(); } void FileManager::cancelPut() { if (isUploading()) { closePutFile(); std::remove(this->putFileName.c_str()); } } bool FileManager::checkFilename(const std::string &name) { return name.find('/') == std::string::npos; } std::string FileManager::getGetBaseFileName() { return this->getBaseFileName; } std::string FileManager::getPutBaseFileName() { return this->putBaseFileName; } void FileManager::writePut(const std::vector &data) { std::ostreambuf_iterator output_iterator(this->putFile); std::copy(data.begin(), data.end(), output_iterator); } std::vector FileManager::readGet() { char fileBuffer[max_data_length]; int read = this->getFile.readsome(fileBuffer, max_data_length); std::vector data; data.assign(fileBuffer, fileBuffer + read); return data; }