FileManager.h 3.4 KB

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