FileManager.h 3.1 KB

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