FileManager.h 3.9 KB

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