Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use StdioFile for uploading (#88)

  • Loading branch information...
commit ce245576b51f9d88adbbdff966676787fd13f0a2 1 parent 75c67da
@nestal nestal authored
View
12 libgrive/src/drive/Resource.cc
@@ -582,16 +582,6 @@ bool Resource::Upload(
assert( http != 0 ) ;
StdioFile file( Path() ) ;
-
- // TODO: upload in chunks
- std::string data ;
- char buf[4096] ;
- std::size_t count = 0 ;
- while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
- data.append( buf, count ) ;
-
- assert( file.Size() == data.size() ) ;
-
std::ostringstream xcontent_len ;
xcontent_len << "X-Upload-Content-Length: " << file.Size() ;
@@ -621,7 +611,7 @@ bool Resource::Upload(
std::string uplink = http->RedirLocation() ;
http::XmlResponse xml ;
- http->Put( uplink, data, &xml, uphdr ) ;
+ http->Put( uplink, file, &xml, uphdr ) ;
AssignIDs( Entry( xml.Response() ) ) ;
sync_time = std::max(Entry(xml.Response()).MTime(), sync_time);
View
12 libgrive/src/http/Agent.hh
@@ -21,7 +21,11 @@
#include <string>
-namespace gr { namespace http {
+namespace gr {
+
+class StdioFile ;
+
+namespace http {
class Header ;
class Receivable ;
@@ -35,6 +39,12 @@ public :
Receivable *dest,
const Header& hdr ) = 0 ;
+ virtual long Put(
+ const std::string& url,
+ StdioFile& file,
+ Receivable *dest,
+ const Header& hdr ) = 0 ;
+
virtual long Get(
const std::string& url,
Receivable *dest,
View
43 libgrive/src/http/CurlAgent.cc
@@ -25,6 +25,7 @@
#include "Receivable.hh"
#include "util/log/Log.hh"
+#include "util/StdioFile.hh"
#include <boost/throw_exception.hpp>
@@ -34,6 +35,7 @@
#include <algorithm>
#include <cassert>
#include <cstring>
+#include <limits>
#include <sstream>
#include <streambuf>
#include <iostream>
@@ -43,8 +45,9 @@
namespace {
using namespace gr::http ;
+using namespace gr ;
-size_t ReadCallback( void *ptr, std::size_t size, std::size_t nmemb, std::string *data )
+size_t ReadStringCallback( void *ptr, std::size_t size, std::size_t nmemb, std::string *data )
{
assert( ptr != 0 ) ;
assert( data != 0 ) ;
@@ -59,6 +62,22 @@ size_t ReadCallback( void *ptr, std::size_t size, std::size_t nmemb, std::string
return count ;
}
+size_t ReadFileCallback( void *ptr, std::size_t size, std::size_t nmemb, StdioFile *file )
+{
+ assert( ptr != 0 ) ;
+ assert( file != 0 ) ;
+
+ u64_t count = std::min(
+ static_cast<u64_t>(size * nmemb),
+ static_cast<u64_t>(file->Size() - file->Tell()) ) ;
+ assert( count <= std::numeric_limits<std::size_t>::max() ) ;
+
+ if ( count > 0 )
+ file->Read( ptr, static_cast<std::size_t>(count) ) ;
+
+ return count ;
+}
+
} // end of local namespace
namespace gr { namespace http {
@@ -167,13 +186,33 @@ long CurlAgent::Put(
// set common options
::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
- ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadCallback ) ;
+ ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadStringCallback ) ;
::curl_easy_setopt(curl, CURLOPT_READDATA , &put_data ) ;
::curl_easy_setopt(curl, CURLOPT_INFILESIZE, put_data.size() ) ;
return ExecCurl( url, dest, hdr ) ;
}
+long CurlAgent::Put(
+ const std::string& url,
+ StdioFile& file,
+ Receivable *dest,
+ const Header& hdr )
+{
+ Trace("HTTP PUT \"%1%\"", url ) ;
+
+ Init() ;
+ CURL *curl = m_pimpl->curl ;
+
+ // set common options
+ ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L ) ;
+ ::curl_easy_setopt(curl, CURLOPT_READFUNCTION, &ReadFileCallback ) ;
+ ::curl_easy_setopt(curl, CURLOPT_READDATA , &file ) ;
+ ::curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(file.Size()) ) ;
+
+ return ExecCurl( url, dest, hdr ) ;
+}
+
long CurlAgent::Get(
const std::string& url,
Receivable *dest,
View
6 libgrive/src/http/CurlAgent.hh
@@ -44,6 +44,12 @@ public :
const std::string& data,
Receivable *dest,
const Header& hdr ) ;
+
+ long Put(
+ const std::string& url,
+ StdioFile& file,
+ Receivable *dest,
+ const Header& hdr ) ;
long Get(
const std::string& url,
Please sign in to comment.
Something went wrong with that request. Please try again.