3636#include " xml/Node.hh"
3737#include " xml/NodeSet.hh"
3838#include " xml/String.hh"
39+ #include " protocol/OAuth2.hh"
3940
4041#include < boost/bind.hpp>
4142#include < boost/exception/all.hpp>
4243
4344#include < cassert>
44-
45+ # include < time.h >
4546// for debugging
4647#include < iostream>
4748
@@ -366,21 +367,45 @@ Resource* Resource::FindChild( const std::string& name )
366367}
367368
368369// try to change the state to "sync"
370+ <<<<<<< HEAD
369371void 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
370375{
371376 assert ( m_state != unknown ) ;
372377 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" ) ;
373390
391+ <<<<<<< HEAD
374392 SyncSelf ( http, options ) ;
375393
376394 // we want the server sync time, so we will take the server time of the last file uploaded to store as the sync time
377395 // m_mtime is updated to server modified time when the file is uploaded
378396 sync_time = std::max (sync_time, m_mtime);
397+ =======
398+ SyncSelf ( http, (const http::Header&)m_auth ) ;
399+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
379400
380401 // if myself is deleted, no need to do the childrens
381402 if ( m_state != local_deleted && m_state != remote_deleted )
382403 std::for_each ( m_child.begin (), m_child.end (),
404+ <<<<<<< HEAD
383405 boost::bind ( &Resource::Sync, _1, http, boost::ref (sync_time), options ) ) ;
406+ =======
407+ boost::bind ( &Resource::Sync, _1, http, auth, oauth ) ) ;
408+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
384409}
385410
386411void Resource::SyncSelf ( http::Agent* http, const Json& options )
@@ -577,18 +602,45 @@ bool Resource::Create( http::Agent* http )
577602 }
578603}
579604
605+ <<<<<<< HEAD
580606bool Resource::Upload (
581607 http::Agent* http,
582608 const std::string& link,
583609 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
584619{
585620 assert ( http != 0 ) ;
621+ const fs::path& path = Path ();
622+ struct WriteThis pooh;
623+ struct WriteThis meta_a;
624+ // TODO: upload in chunks
586625
626+ <<<<<<< HEAD
587627 File file ( Path () ) ;
588628 std::ostringstream xcontent_len ;
589629 xcontent_len << " X-Upload-Content-Length: " << file.Size () ;
590630
591631 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
592644 hdr.Add ( " Content-Type: application/atom+xml" ) ;
593645 hdr.Add ( " X-Upload-Content-Type: application/octet-stream" ) ;
594646 hdr.Add ( xcontent_len.str () ) ;
@@ -599,12 +651,14 @@ bool Resource::Upload(
599651 % m_kind
600652 % xml::Escape (m_name)
601653 ).str () ;
602-
654+ meta_a.readptr =meta.c_str ();
655+ meta_a.sizeleft =meta.size ();
656+ meta_a.post =post ;
603657 http::StringResponse str ;
604658 if ( post )
605659 http->Post ( link, meta, &str, hdr ) ;
606660 else
607- http->Put ( link, meta , &str, hdr ) ;
661+ http->Put ( link, &meta_a , &str, hdr ) ;
608662
609663 http::Header uphdr ;
610664 uphdr.Add ( " Expect:" ) ;
@@ -614,7 +668,12 @@ bool Resource::Upload(
614668 std::string uplink = http->RedirLocation () ;
615669 http::XmlResponse xml ;
616670
671+ <<<<<<< HEAD
617672 http->Put ( uplink, &file, &xml, uphdr ) ;
673+ =======
674+ http->Put ( uplink, &pooh, &xml, uphdr ) ;
675+ fclose (pooh.file );
676+ >>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
618677 AssignIDs ( Entry ( xml.Response () ) ) ;
619678 m_mtime = Entry (xml.Response ()).MTime ();
620679
0 commit comments