123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #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<char> &data) {
- std::ostreambuf_iterator<char> output_iterator(this->putFile);
- std::copy(data.begin(), data.end(), output_iterator);
- }
- std::vector<char> FileManager::readGet() {
- char fileBuffer[max_data_length];
- int read = this->getFile.readsome(fileBuffer, max_data_length);
- std::vector<char> data;
- data.assign(fileBuffer, fileBuffer + read);
- return data;
- }
|