use StdioFile for uploading (#88)

pull/40/head
Nestal Wan 2012-07-17 01:58:16 +08:00
parent 75c67dad8d
commit ce245576b5
4 changed files with 59 additions and 14 deletions

View File

@ -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 File

@ -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 File

@ -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 File

@ -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,