FileManager.h 3.2 KB

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