36
36
#include " xml/Node.hh"
37
37
#include " xml/NodeSet.hh"
38
38
#include " xml/String.hh"
39
+ #include " protocol/OAuth2.hh"
39
40
40
41
#include < boost/bind.hpp>
41
42
#include < boost/exception/all.hpp>
42
43
43
44
#include < cassert>
44
-
45
+ # include < time.h >
45
46
// for debugging
46
47
#include < iostream>
47
48
@@ -366,21 +367,45 @@ Resource* Resource::FindChild( const std::string& name )
366
367
}
367
368
368
369
// try to change the state to "sync"
370
+ <<<<<<< HEAD
369
371
void Resource::Sync ( http::Agent *http, DateTime& sync_time, const Json& options )
372
+ =======
373
+ void Resource::Sync( http::Agent *http, const http::Header& auth, OAuth2& oauth )
374
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
370
375
{
371
376
assert ( m_state != unknown ) ;
372
377
assert ( !IsRoot () || m_state == sync ) ; // root folder is already synced
378
+ std::vector<std::string> m_auth;
379
+ // m_auth.assign(auth.begin(), auth.end());
380
+ time_t t;
381
+ time (&t);
382
+ std::cout << auth << std::endl;
383
+ if (t-oauth.Time () > oauth.ExpiresIn ()-100 ){
384
+ oauth.Refresh ();
385
+ m_auth.clear ();
386
+ oauth.m_time = t;
387
+ }
388
+ m_auth.push_back ( " Authorization: Bearer " + oauth.AccessToken () ) ;
389
+ m_auth.push_back ( " GData-Version: 3.0" ) ;
373
390
391
+ <<<<<<< HEAD
374
392
SyncSelf ( http, options ) ;
375
393
376
394
// we want the server sync time, so we will take the server time of the last file uploaded to store as the sync time
377
395
// m_mtime is updated to server modified time when the file is uploaded
378
396
sync_time = std::max (sync_time, m_mtime);
397
+ =======
398
+ SyncSelf ( http, (const http::Header&)m_auth ) ;
399
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
379
400
380
401
// if myself is deleted, no need to do the childrens
381
402
if ( m_state != local_deleted && m_state != remote_deleted )
382
403
std::for_each ( m_child.begin (), m_child.end (),
404
+ <<<<<<< HEAD
383
405
boost::bind ( &Resource::Sync, _1, http, boost::ref (sync_time), options ) ) ;
406
+ =======
407
+ boost::bind ( &Resource::Sync, _1, http, auth, oauth ) ) ;
408
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
384
409
}
385
410
386
411
void Resource::SyncSelf ( http::Agent* http, const Json& options )
@@ -577,18 +602,45 @@ bool Resource::Create( http::Agent* http )
577
602
}
578
603
}
579
604
605
+ <<<<<<< HEAD
580
606
bool Resource::Upload (
581
607
http::Agent* http,
582
608
const std::string& link,
583
609
bool post)
610
+ =======
611
+ struct WriteThis {
612
+ const char *readptr;
613
+ off_t sizeleft;
614
+ FILE* file;
615
+ bool post;
616
+ };
617
+ bool Resource::Upload ( http::Agent* http, const std::string& link, const http::Header& auth, bool post )
618
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
584
619
{
585
620
assert ( http != 0 ) ;
621
+ const fs::path& path = Path ();
622
+ struct WriteThis pooh;
623
+ struct WriteThis meta_a;
624
+ // TODO: upload in chunks
586
625
626
+ <<<<<<< HEAD
587
627
File file ( Path () ) ;
588
628
std::ostringstream xcontent_len ;
589
629
xcontent_len << " X-Upload-Content-Length: " << file.Size () ;
590
630
591
631
http::Header hdr ;
632
+ =======
633
+ pooh.file = fopen64 (path.string ().c_str ()," r" );
634
+ fseeko (pooh.file , 0 , SEEK_END);
635
+ pooh.sizeleft = ftello (pooh.file );
636
+ rewind (pooh.file );
637
+
638
+ pooh.post = 1 ;
639
+
640
+ std::ostringstream xcontent_len ;
641
+ xcontent_len << " X-Upload-Content-Length: " << pooh.sizeleft ;
642
+ http::Header hdr ( auth ) ;
643
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
592
644
hdr.Add ( " Content-Type: application/atom+xml" ) ;
593
645
hdr.Add ( " X-Upload-Content-Type: application/octet-stream" ) ;
594
646
hdr.Add ( xcontent_len.str () ) ;
@@ -599,12 +651,14 @@ bool Resource::Upload(
599
651
% m_kind
600
652
% xml::Escape (m_name)
601
653
).str () ;
602
-
654
+ meta_a.readptr =meta.c_str ();
655
+ meta_a.sizeleft =meta.size ();
656
+ meta_a.post =post;
603
657
http::StringResponse str ;
604
658
if ( post )
605
659
http->Post ( link , meta, &str, hdr ) ;
606
660
else
607
- http->Put ( link , meta , &str, hdr ) ;
661
+ http->Put ( link , &meta_a , &str, hdr ) ;
608
662
609
663
http::Header uphdr ;
610
664
uphdr.Add ( " Expect:" ) ;
@@ -614,7 +668,12 @@ bool Resource::Upload(
614
668
std::string uplink = http->RedirLocation () ;
615
669
http::XmlResponse xml ;
616
670
671
+ <<<<<<< HEAD
617
672
http->Put ( uplink, &file, &xml, uphdr ) ;
673
+ =======
674
+ http->Put ( uplink, &pooh, &xml, uphdr ) ;
675
+ fclose (pooh.file );
676
+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
618
677
AssignIDs ( Entry ( xml.Response () ) ) ;
619
678
m_mtime = Entry (xml.Response ()).MTime ();
620
679
0 commit comments