FileManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef FILEMANAGER_H
  2. #define FILEMANAGER_H
  3. #include <fstream>
  4. #include <vector>
  5. #include <boost/filesystem.hpp>
  6. #include <boost/range/iterator_range.hpp>
  7. /**
  8. * @class FileManager
  9. *
  10. * Manages file writes for uploads and file reads for downloads
  11. */
  12. class FileManager {
  13. private:
  14. const std::string fileDirectory = "./files/";
  15. /**
  16. * file stream for get command
  17. */
  18. std::ifstream getFile;
  19. /**
  20. * file stream for put command
  21. */
  22. std::ofstream putFile;
  23. /**
  24. * file name for put command
  25. * (used to delete the file if the upload is canceled)
  26. */
  27. std::string putFileName;
  28. /**
  29. * file name for put command
  30. */
  31. std::string putBaseFileName;
  32. /**
  33. * file name for get command
  34. */
  35. std::string getBaseFileName;
  36. public:
  37. enum { max_data_length = 512 };
  38. /**
  39. * Creates the file manager
  40. */
  41. FileManager();
  42. /**
  43. * Destroys the file manager
  44. */
  45. ~FileManager();
  46. /**
  47. * Checks if an upload is running
  48. * @return true - upload running | false - no upload
  49. */
  50. virtual bool isUploading();
  51. /**
  52. * Check if a download is running
  53. * @return true - download running | false - no download
  54. */
  55. virtual bool isDownloading();
  56. /**
  57. * Opens put file if it doesn't exist
  58. * @return true - file is open | false - file is not open
  59. */
  60. virtual bool openPutFile(const std::string &filename);
  61. /**
  62. * Opens get file if it exists and reports the amount of chunks
  63. * @return true - file is open | false - file is not open
  64. */
  65. virtual bool openGetFile(const std::string &filename, int &chunks);
  66. /**
  67. * Closes file
  68. */
  69. void closePutFile();
  70. /**
  71. * Closes file
  72. */
  73. void closeGetFile();
  74. /**
  75. * Closes put file and deletes it
  76. */
  77. virtual void cancelPut();
  78. /**
  79. * Checks if a file name is valid
  80. * @return true - name is valid | false - name is invalid
  81. */
  82. bool checkFilename(const std::string &name);
  83. /**
  84. * Return the name of the download file
  85. * @return name of the download file
  86. */
  87. virtual std::string getGetBaseFileName();
  88. /**
  89. * Return the name of the upload file
  90. * @return name of the upload file
  91. */
  92. virtual std::string getPutBaseFileName();
  93. /**
  94. * Writes data to put file
  95. */
  96. virtual void writePut(const std::vector<char> &data);
  97. /**
  98. * Reads data from get file
  99. */
  100. virtual std::vector<char> readGet();
  101. /**
  102. * Get vector of all files in current directory.
  103. * Ignores directories
  104. *
  105. * @return vector of all files in the current directory
  106. */
  107. std::vector<std::string> listFiles();
  108. /**
  109. * Calculates the chunks of a given file.
  110. *
  111. * @param filename The name of the file.
  112. * @return chunks of the given file. returns -1 if directory
  113. */
  114. int getChunksFromFile(const std::string &filename);
  115. };
  116. #endif