FileManager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "../include/FileManager.h"
  2. #include <boost/filesystem.hpp>
  3. #include <boost/range/iterator_range.hpp>
  4. #include "../include/Config.h"
  5. FileManager::FileManager() : fileDirectory(Config::getValue("filedirectory")), deleteAllowed(Config::getValue("deleteAllowed") == "true") {}
  6. FileManager::~FileManager() {
  7. cancelPut();
  8. closeGetFile();
  9. }
  10. bool FileManager::isUploading() { return this->putFile.is_open(); }
  11. bool FileManager::isDownloading() { return this->getFile.is_open(); }
  12. bool FileManager::openPutFile(const std::string &filename) {
  13. this->putBaseFileName = filename;
  14. this->putFileName = this->fileDirectory;
  15. this->putFileName.append(filename);
  16. std::ifstream ifile(this->putFileName);
  17. if (ifile.is_open()) {
  18. // file alread exists
  19. ifile.close();
  20. closePutFile();
  21. return false;
  22. }
  23. // open file and test if it already exists
  24. this->putFile.open(this->putFileName, std::ios::app | std::ios::binary);
  25. return true;
  26. }
  27. std::pair<bool, int> FileManager::openGetFile(const std::string &filename) {
  28. this->getBaseFileName = filename;
  29. std::string file = this->fileDirectory;
  30. file.append(filename);
  31. this->getFile.open(file, std::ios::ate | std::ios::binary);
  32. if (this->getFile.is_open() == 0) {
  33. // file does not exist or cannot be opened
  34. return std::pair<bool, int>(false, -1);
  35. } else {
  36. this->getFileSize = this->getFile.tellg();
  37. int chunks = this->getFileSize / max_data_length + (this->getFileSize % max_data_length == 0 ? 0 : 1);
  38. this->getFile.seekg(std::ios::beg);
  39. return std::pair<bool, int>(true, chunks);
  40. }
  41. }
  42. void FileManager::closePutFile() { this->putFile.close(); }
  43. void FileManager::closeGetFile() { this->getFile.close(); }
  44. void FileManager::cancelPut() {
  45. if (isUploading()) {
  46. closePutFile();
  47. std::remove(this->putFileName.c_str());
  48. }
  49. }
  50. bool FileManager::checkFilename(const std::string &name) { return name.find('/') == std::string::npos; }
  51. std::string FileManager::getGetBaseFileName() { return this->getBaseFileName; }
  52. std::string FileManager::getPutBaseFileName() { return this->putBaseFileName; }
  53. void FileManager::writePut(const std::vector<char> &data) { this->putFile.write(data.data(), data.size()); }
  54. std::vector<char> FileManager::readGet() {
  55. // store position before read
  56. std::streamoff read = this->getFile.tellg();
  57. if (read + max_data_length > this->getFileSize) {
  58. read = this->getFileSize % max_data_length;
  59. } else {
  60. read = max_data_length;
  61. }
  62. std::vector<char> data;
  63. data.resize(read);
  64. this->getFile.read(data.data(), read);
  65. return data;
  66. }
  67. int FileManager::openList() {
  68. // add empty chunk vector
  69. this->list.push_back(std::vector<std::string>());
  70. int cursize = 0;
  71. for (const auto &entry : boost::filesystem::directory_iterator(this->fileDirectory)) {
  72. // getting filename from
  73. if (boost::filesystem::is_directory(entry.path()))
  74. continue;
  75. const std::string s = entry.path().filename().string();
  76. // check if the size is too big
  77. if (s.length() > max_data_length)
  78. return -1;
  79. cursize += s.length() + 3;
  80. if (cursize > max_data_length) {
  81. this->list.push_back(std::vector<std::string>());
  82. cursize = 0;
  83. }
  84. // add string to its chunk
  85. this->list.back().push_back(s);
  86. }
  87. // empty list if no file was read
  88. if (this->list.size() == 1 && this->list.back().size() == 0) {
  89. this->list.clear();
  90. }
  91. return this->list.size();
  92. }
  93. int FileManager::getRemainingListChunks() { return this->list.size(); }
  94. int FileManager::getListSize() {
  95. int size = 0;
  96. for (const std::vector<std::string> &l : this->list) {
  97. size += l.size();
  98. }
  99. return size;
  100. }
  101. std::vector<std::string> FileManager::getNextChunkFromList() {
  102. if (getRemainingListChunks() == 0) { // This should never happen!!!
  103. return std::vector<std::string>();
  104. }
  105. std::vector<std::string> ret = this->list.back();
  106. this->list.pop_back();
  107. return ret;
  108. }
  109. void FileManager::cancelList() { this->list.clear(); }
  110. std::pair<std::vector<char>, FileManager::Error> FileManager::getBytesFromFile(const std::string &filename, int numOfBytes) {
  111. std::ifstream file;
  112. std::string fname = this->fileDirectory;
  113. fname.append(filename);
  114. file.open(fname, std::ios::ate | std::ios::binary);
  115. std::vector<char> bytes;
  116. if (!file.is_open()) {
  117. return std::make_pair(bytes, FileManager::Error::no_such_file);
  118. }
  119. auto size = file.tellg();
  120. if (size < numOfBytes) {
  121. return std::make_pair(bytes, FileManager::Error::file_too_small);
  122. }
  123. bytes.resize(numOfBytes);
  124. file.seekg(0, std::ios::beg);
  125. file.read(bytes.data(), numOfBytes);
  126. file.close();
  127. return std::make_pair(bytes, FileManager::Error::no_error);
  128. }
  129. FileManager::Error FileManager::deleteFile(const std::string &filename) {
  130. if (!this->deleteAllowed) {
  131. return not_allowed;
  132. }
  133. std::string fname = this->fileDirectory;
  134. fname.append(filename);
  135. if (!boost::filesystem::exists(fname)) {
  136. return no_such_file;
  137. }
  138. boost::filesystem::remove(fname);
  139. return no_error;
  140. }