diff Core/HttpServer/FilesystemHttpSender.cpp @ 1519:8bd0d897763f

refactoring: IHttpStreamAnswer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 13:15:16 +0200
parents 6e7e5ed91c2d
children f938f7779bcb
line wrap: on
line diff
--- a/Core/HttpServer/FilesystemHttpSender.cpp	Tue Aug 11 10:36:05 2015 +0200
+++ b/Core/HttpServer/FilesystemHttpSender.cpp	Tue Aug 11 13:15:16 2015 +0200
@@ -36,68 +36,50 @@
 
 #include <stdio.h>
 
+
+static const size_t  CHUNK_SIZE = 64 * 1024;   // Use 64KB chunks
+
 namespace Orthanc
 {
-  void FilesystemHttpSender::Setup()
-  {
-    //SetDownloadFilename(path_.filename().string());
-
-#if BOOST_HAS_FILESYSTEM_V3 == 1
-    SetContentType(Toolbox::AutodetectMimeType(path_.filename().string()));
-#else
-    SetContentType(Toolbox::AutodetectMimeType(path_.filename()));
-#endif
-  }
-
-  uint64_t FilesystemHttpSender::GetFileSize()
-  {
-    return Toolbox::GetFileSize(path_.string());
-  }
-
-  bool FilesystemHttpSender::SendData(HttpOutput& output)
+  void FilesystemHttpSender::Open()
   {
-    FILE* fp = fopen(path_.string().c_str(), "rb");
-    if (!fp)
-    {
-      return false;
-    }
-
-    std::vector<uint8_t> buffer(1024 * 1024);  // Chunks of 1MB
+    SetFilename(path_.filename().string());
+    file_.open(path_.string().c_str(), std::ifstream::binary);
 
-    for (;;)
-    {
-      size_t nbytes = fread(&buffer[0], 1, buffer.size(), fp);
-      if (nbytes == 0)
-      {
-        break;
-      }
-      else
-      {
-        output.SendBody(&buffer[0], nbytes);
-      }
-    }
+    file_.seekg(0, file_.end);
+    size_ = file_.tellg();
+    file_.seekg(0, file_.beg);
 
-    fclose(fp);
-
-    return true;
+    chunk_.resize(CHUNK_SIZE);
+    chunkSize_ = 0;
   }
 
   FilesystemHttpSender::FilesystemHttpSender(const char* path)
   {
     path_ = std::string(path);
-    Setup();
+    Open();
   }
 
   FilesystemHttpSender::FilesystemHttpSender(const boost::filesystem::path& path)
   {
     path_ = path;
-    Setup();
+    Open();
   }
 
   FilesystemHttpSender::FilesystemHttpSender(const FilesystemStorage& storage,
                                              const std::string& uuid)
   {
     path_ = storage.GetPath(uuid).string();
-    Setup();
+    Open();
+  }
+
+
+  bool FilesystemHttpSender::ReadNextChunk()
+  {
+    file_.read(&chunk_[0], chunk_.size());
+
+    chunkSize_ = file_.gcount();
+
+    return chunkSize_ > 0;
   }
 }