FileManager.h 2.2 KB

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