FileManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  35. * list vector for list command
  36. */
  37. std::vector<std::string> list;
  38. /**
  39. * The number of remainig chunks in vector list.
  40. */
  41. int remainingListChunks;
  42. /**
  43. * Get vector of all files in current directory.
  44. * Ignores directories
  45. *
  46. * @return vector of all files in the current directory
  47. */
  48. std::vector<std::string> listFiles();
  49. public:
  50. enum { max_data_length = 512 };
  51. /**
  52. * Creates the file manager
  53. */
  54. FileManager();
  55. /**
  56. * Destroys the file manager
  57. */
  58. ~FileManager();
  59. /**
  60. * Checks if an upload is running
  61. * @return true - upload running | false - no upload
  62. */
  63. virtual bool isUploading();
  64. /**
  65. * Check if a download is running
  66. * @return true - download running | false - no download
  67. */
  68. virtual bool isDownloading();
  69. /**
  70. * Opens put file if it doesn't exist
  71. * @return true - file is open | false - file is not open
  72. */
  73. virtual bool openPutFile(const std::string &filename);
  74. /**
  75. * Opens get file if it exists and reports the amount of chunks
  76. * @return true - file is open | false - file is not open
  77. */
  78. virtual bool openGetFile(const std::string &filename, int &chunks);
  79. /**
  80. * Closes file
  81. */
  82. void closePutFile();
  83. /**
  84. * Closes file
  85. */
  86. void closeGetFile();
  87. /**
  88. * Closes put file and deletes it
  89. */
  90. virtual void cancelPut();
  91. /**
  92. * Checks if a file name is valid
  93. * @return true - name is valid | false - name is invalid
  94. */
  95. bool checkFilename(const std::string &name);
  96. /**
  97. * Return the name of the download file
  98. * @return name of the download file
  99. */
  100. virtual std::string getGetBaseFileName();
  101. /**
  102. * Return the name of the upload file
  103. * @return name of the upload file
  104. */
  105. virtual std::string getPutBaseFileName();
  106. /**
  107. * Writes data to put file
  108. */
  109. virtual void writePut(const std::vector<char> &data);
  110. /**
  111. * Reads data from get file
  112. */
  113. virtual std::vector<char> readGet();
  114. /**
  115. * Open list command. Set list vector and claculate chunks
  116. *
  117. * @return chunks of the resulting list.
  118. */
  119. virtual int openList();
  120. /**
  121. * @return remaining chunks to be send with listdata command
  122. */
  123. virtual int getRemainingListChunks();
  124. /**
  125. * @return size of the list vector
  126. */
  127. virtual int getListSize();
  128. /**
  129. * Return next chunk for listdata command.
  130. * Increases remainingListChunks. You need to run openList before.
  131. *
  132. * @return next chnuk vector
  133. */
  134. std::vector<std::string> getNextChunkFromList();
  135. /**
  136. * Cancel current list command.
  137. * Clear list vector and set remainingListChunks zero.
  138. */
  139. void cancelList();
  140. };
  141. #endif