Переглянути джерело

implemented getChunksFromFile and updated listFiles

marius.rescheleit 5 роки тому
батько
коміт
b6b3163371
2 змінених файлів з 25 додано та 4 видалено
  1. 11 0
      daemon/include/FileManager.h
  2. 14 4
      daemon/src/FileManager.cpp

+ 11 - 0
daemon/include/FileManager.h

@@ -122,9 +122,20 @@ public:
   virtual std::vector<char> readGet();
 
   /**
+   * Get vector of all files in current directory.
+   * Ignores directories
+   *
    * @return vector of all files in the current directory
    */
    std::vector<std::string> listFiles();
+
+   /**
+    * Calculates the chunks of a given file.
+    *
+    * @param filename The name of the file.
+    * @return chunks of the given file. returns -1 if directory
+    */
+    int getChunksFromFile(const std::string &filename);
 };
 
 #endif

+ 14 - 4
daemon/src/FileManager.cpp

@@ -82,13 +82,23 @@ std::vector<char> FileManager::readGet() {
 
 std::vector<std::string> FileManager::listFiles() {
   std::vector<std::string> res;
+  // TODO make path configurable
   boost::filesystem::path p{"."};
-  boost::filesystem::file_status s = status(p);
-  if(!is_directory(s)) {
-    return res;
-  }
   for(auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(p), {})) {
+    if(boost::filesystem::is_directory(entry.path()))
+      continue;
     res.push_back(entry.path().string());
   }
   return res;
 }
+
+int FileManager::getChunksFromFile(const std::string &filename) {
+  boost::filesystem::path p{filename};
+  boost::filesystem::file_status s = status(p);
+  if(is_directory(s)) {
+    return -1;
+  }
+  uintmax_t size = boost::filesystem::file_size(filename);
+  int chunks = size / max_data_length + (size % max_data_length == 0 ? 0 : 1);
+  return chunks;
+}