Ver código fonte

added openList getListSize and getNextChunkFromFile

marius.rescheleit 5 anos atrás
pai
commit
4f5729cf7a
3 arquivos alterados com 80 adições e 37 exclusões
  1. 18 7
      daemon/include/FileManager.h
  2. 43 11
      daemon/src/FileManager.cpp
  3. 19 19
      daemon/src/JsonCommander.cpp

+ 18 - 7
daemon/include/FileManager.h

@@ -3,8 +3,6 @@
 
 #include <fstream>
 #include <vector>
-#include <boost/filesystem.hpp>
-#include <boost/range/iterator_range.hpp>
 
 /**
  * @class FileManager
@@ -13,7 +11,7 @@
  */
 class FileManager {
 private:
-  const std::string fileDirectory = "./files/";
+  const std::string fileDirectory = ".";
 
   /**
    * file stream for get command
@@ -41,6 +39,11 @@ private:
    */
   std::string getBaseFileName;
 
+  /**
+   * list vector for list command
+   */
+   std::vector<std::string> list;
+
 public:
   enum { max_data_length = 512 };
 
@@ -130,12 +133,20 @@ public:
    std::vector<std::string> listFiles();
 
    /**
-    * Calculates the chunks of a given file.
+    * Open list command. Set list vector and claculate chunks
     *
-    * @param filename The name of the file.
-    * @return chunks of the given file. returns -1 if directory
+    * @return chunks of the resulting list.
     */
-    int getChunksFromFile(const std::string &filename);
+    int openList();
+
+    int getListSize();
+
+    /**
+     *
+     *
+     * @return next chnuk vector
+     */
+    std::vector<std::string> getNextChunkFromList();
 };
 
 #endif

+ 43 - 11
daemon/src/FileManager.cpp

@@ -1,5 +1,8 @@
 #include "../include/FileManager.h"
 
+#include <boost/filesystem.hpp>
+#include <boost/range/iterator_range.hpp>
+
 FileManager::FileManager() {}
 
 FileManager::~FileManager() {
@@ -7,7 +10,7 @@ FileManager::~FileManager() {
   closeGetFile();
 }
 
-bool FileManager::isUploading() { return this->putFile.is_open(); }
+bool FileManager::isUploading() { return this->putFile.is_open() || !list.empty(); }
 
 bool FileManager::isDownloading() { return this->getFile.is_open(); }
 
@@ -83,22 +86,51 @@ std::vector<char> FileManager::readGet() {
 std::vector<std::string> FileManager::listFiles() {
   std::vector<std::string> res;
   // TODO make path configurable
-  boost::filesystem::path p{"."};
-  for(auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(p), {})) {
-    if(boost::filesystem::is_directory(entry.path()))
+  for (const auto &entry :
+       boost::filesystem::directory_iterator(this->fileDirectory)) {
+    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;
+int FileManager::openList() {
+  list = listFiles();
+  int chunks = 0;
+  int cursize = 0;
+  for(int i=0; i<list.size(); i++) {
+    if(list.at(i).length() > max_data_length)
+      return -1;
+    cursize += list.at(i).length();
+    if(cursize > max_data_length) {
+      chunks++;
+      i--;
+      cursize = 0;
+    }
   }
-  uintmax_t size = boost::filesystem::file_size(filename);
-  int chunks = size / max_data_length + (size % max_data_length == 0 ? 0 : 1);
+  if(list.size() > 0)
+    chunks++;
   return chunks;
 }
+
+int FileManager::getListSize() {
+  return list.size();
+}
+
+std::vector<std::string> FileManager::getNextChunkFromList() {
+  std::vector<std::string> ret;
+  int cursize = 0;
+  std::string tmp;
+  while(!list.empty()) {
+    tmp = list.back();
+    cursize += tmp.length();
+    if(cursize>max_data_length) {
+      break;
+    } else {
+      ret.push_back(tmp);
+      list.pop_back();
+    }
+  }
+  return ret;
+}

+ 19 - 19
daemon/src/JsonCommander.cpp

@@ -66,21 +66,18 @@ JsonCommander::executeClose(const Json::Value &message) {
 
 JsonCommander::Response JsonCommander::executeList(const Json::Value &message) {
   JsonCommander::Response response;
-  response.action = send;
   response.json["command"] = "list";
-  response.json["accept"] = true;
-
-  int chunks = 0;
 
-  std::vector<std::string> v = fileManager.listFiles();
-  for(int i=0; i<v.size(); i++) {
-    chunks += fileManager.getChunksFromFile(v.at(i));
+  if(fileManager.isUploading()) {
+    response.action = send;
+    response.json["accept"] = false;
+    response.json["error"] = "there is an upload already running";
+  } else {
+    response.json["chunks"] = fileManager.openList();
+    response.json["items"] = fileManager.getListSize();
+    response.json["error"] = "";
   }
 
-  response.json["items"] = (int) v.size();
-  response.json["chunks"] = chunks;
-  response.json["error"] = "";
-
   return response;
 }
 
@@ -90,16 +87,19 @@ JsonCommander::Response JsonCommander::executeListData(const Json::Value &messag
   response.json["command"] = "listdata";
   Json::Value array;
 
-  std::vector<std::string> v = fileManager.listFiles();
-  for(int i=0; i<v.size(); i++) {
-    array.append(v.at(i));
+  if(fileManager.getListSize()==0) {
+    response.json["accept"] = false;
+    response.json["error"] = "nothing to upload";
+  } else {
+    std::vector<std::string> v = fileManager.getNextChunkFromList();
+    for(int i=0; i<v.size(); i++)
+      array.append(v.at(i));
+    // response.json["remaining"] = message["chunk"].asInt();
+    response.json["cancel"] = false;
+    response.json["names"] = array;
+    response.json["error"] = "";
   }
 
-  response.json["remaining"] = (int) v.size();
-  response.json["cancel"] = false;
-  response.json["names"] = array;
-  response.json["error"] = "";
-
   return response;
 }