FileManager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #ifndef FILEMANAGER_H
  2. #define FILEMANAGER_H
  3. #include <fstream>
  4. #include <tuple>
  5. #include <vector>
  6. /**
  7. * @class FileManager
  8. *
  9. * Manages file writes for uploads and file reads for downloads
  10. */
  11. class FileManager {
  12. private:
  13. /**
  14. * Directory of the files uploaded
  15. */
  16. const std::string fileDirectory;
  17. /**
  18. * allows file deletion
  19. */
  20. const bool deleteAllowed;
  21. /**
  22. * file stream for get command
  23. */
  24. std::ifstream getFile;
  25. /**
  26. * file stream for put command
  27. */
  28. std::ofstream putFile;
  29. /**
  30. * file name for put command
  31. * (used to delete the file if the upload is canceled)
  32. */
  33. std::string putFileName;
  34. /**
  35. * file name for put command
  36. */
  37. std::string putBaseFileName;
  38. /**
  39. * file name for get command
  40. */
  41. std::string getBaseFileName;
  42. /*
  43. * size of the get file
  44. *
  45. * Used to determine how much to read and encode as base64
  46. */
  47. std::streamoff getFileSize;
  48. /**
  49. * list vector for list command
  50. */
  51. std::vector<std::vector<std::string>> list;
  52. /**
  53. * extendedlist vector for extendend list command
  54. * contains name head and size
  55. */
  56. std::vector<std::vector<std::tuple<std::string, std::string, double>>> extendedlist;
  57. public:
  58. enum { max_data_length = 4096 };
  59. /**
  60. * FileManager Errors
  61. *
  62. * errors which the head or deleteFile command returns
  63. */
  64. enum Error { no_error, no_such_file, file_too_small, not_allowed };
  65. /**
  66. * Creates the file manager
  67. */
  68. FileManager();
  69. /**
  70. * Destroys the file manager
  71. */
  72. ~FileManager();
  73. /**
  74. * Checks if an upload is running
  75. * @return true - upload running | false - no upload
  76. */
  77. virtual bool isUploading();
  78. /**
  79. * Check if a download is running
  80. * @return true - download running | false - no download
  81. */
  82. virtual bool isDownloading();
  83. /**
  84. * Opens put file if it doesn't exist
  85. * @return true - file is open | false - file is not open
  86. */
  87. virtual bool openPutFile(const std::string &filename);
  88. /**
  89. * Opens get file if it exists and reports the amount of chunks
  90. * @return true - file is open | false - file is not open
  91. */
  92. virtual std::pair<bool, int> openGetFile(const std::string &filename);
  93. /**
  94. * Closes file
  95. */
  96. void closePutFile();
  97. /**
  98. * Closes file
  99. */
  100. void closeGetFile();
  101. /**
  102. * Closes put file and deletes it
  103. */
  104. virtual void cancelPut();
  105. /**
  106. * Checks if a file name is valid
  107. * @return true - name is valid | false - name is invalid
  108. */
  109. bool checkFilename(const std::string &name);
  110. /**
  111. * Return the name of the download file
  112. * @return name of the download file
  113. */
  114. virtual std::string getGetBaseFileName();
  115. /**
  116. * Return the name of the upload file
  117. * @return name of the upload file
  118. */
  119. virtual std::string getPutBaseFileName();
  120. /**
  121. * Writes data to put file
  122. */
  123. virtual void writePut(const std::vector<char> &data);
  124. /**
  125. * Reads data from get file
  126. */
  127. virtual std::vector<char> readGet();
  128. /**
  129. * Open list command. Set list vector and claculate chunks
  130. *
  131. * @return chunks of the resulting list | if a filename is too long it returns
  132. * -1
  133. */
  134. virtual int openList();
  135. /**
  136. * @return remaining chunks to be send with listdata command
  137. */
  138. virtual int getRemainingListChunks();
  139. /**
  140. * @return size of the list vector
  141. */
  142. virtual int getListSize();
  143. /**
  144. * Return next chunk for listdata command.
  145. * Increases remainingListChunks. You need to run openList before.
  146. *
  147. * @return next chnuk vector
  148. */
  149. virtual std::vector<std::string> getNextChunkFromList();
  150. /**
  151. * Cancel current list command.
  152. * Clear list vector and set remainingListChunks zero.
  153. */
  154. void cancelList();
  155. /**
  156. * Get the first n bytes of a file as string.
  157. * @param filename name of the files
  158. * @param numOfBytes the number of bytes you want
  159. *
  160. * @return first: the array of chars containing the data if no error occured
  161. * second: an error code
  162. */
  163. virtual std::pair<std::vector<char>, Error> getBytesFromFile(const std::string &filename, int numOfBytes);
  164. /**
  165. * Delete a file in the file directory.
  166. * @param filename name of the file
  167. *
  168. * @return return code 0: ok 1: disabled in config 2: file not found
  169. */
  170. virtual Error deleteFile(const std::string &filename);
  171. /**
  172. * Open extendedlist command. Set extendedlist vector and claculate chunks
  173. *
  174. * @return chunks of the resulting list | if a filename is too long it returns
  175. * -1
  176. */
  177. virtual int openExtendedList();
  178. /**
  179. * Open extendedlist command. Set extendedlist vector and claculate chunks
  180. *
  181. * @return chunks of the resulting extendedlist | if a filename is too long it returns
  182. * -1
  183. */
  184. virtual int getRemainingExtendedListChunks();
  185. /**
  186. * Return next chunk for extendedlistdata command.
  187. * You need to run openList before.
  188. *
  189. * @return next chnuk vector
  190. */
  191. virtual std::vector<std::tuple<std::string, std::string, double>> getNextChunkFromExtendedList();
  192. /**
  193. * @return size of the extendendlist vector
  194. */
  195. virtual int getExtendedListSize();
  196. /**
  197. * Cancel current extendendlist command.
  198. * Clear extendendlist vector and set remainingListChunks zero.
  199. */
  200. virtual void cancelExtendedList();
  201. };
  202. #endif