grive2/libgrive/src/drive/Drive.cc

228 lines
5.8 KiB
C++
Raw Normal View History

2012-04-25 20:13:17 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
2012-04-25 20:13:17 +04:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Drive.hh"
2012-05-06 18:27:52 +04:00
#include "CommonUri.hh"
#include "File.hh"
#include "http/HTTP.hh"
2012-05-06 13:13:48 +04:00
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "protocol/JsonResponse.hh"
#include "protocol/OAuth2.hh"
#include "util/Crypt.hh"
#include "util/DateTime.hh"
#include "util/OS.hh"
2012-05-01 13:44:17 +04:00
#include "util/Path.hh"
2012-05-06 13:13:48 +04:00
#include "xml/Node.hh"
2012-04-25 20:13:17 +04:00
// standard C++ library
#include <algorithm>
2012-04-26 20:55:10 +04:00
#include <cassert>
#include <fstream>
2012-04-26 20:55:10 +04:00
#include <map>
2012-04-29 06:22:58 +04:00
#include <sstream>
2012-04-25 20:13:17 +04:00
// for debugging only
#include <iostream>
namespace gr {
Drive::Drive( OAuth2& auth ) :
m_auth( auth ),
2012-04-26 20:55:10 +04:00
m_root( ".", "https://docs.google.com/feeds/default/private/full/folder%3Aroot" )
2012-04-25 20:13:17 +04:00
{
m_http_hdr.push_back( "Authorization: Bearer " + m_auth.AccessToken() ) ;
m_http_hdr.push_back( "GData-Version: 3.0" ) ;
http::Agent http ;
ConstructDirTree( &http ) ;
2012-05-06 14:14:36 +04:00
http::JsonResponse str ;
http.Get( root_url + "?alt=json&showfolders=true", &str, m_http_hdr ) ;
Json resp = str.Response() ;
Json resume_link ;
if ( resp["feed"]["link"].FindInArray( "rel", "http://schemas.google.com/g/2005#resumable-create-media", resume_link ) )
m_resume_link = resume_link["href"].As<std::string>() ;
2012-05-06 14:14:36 +04:00
bool has_next = false ;
do
{
2012-05-06 14:14:36 +04:00
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
for ( Json::Array::iterator i = entries.begin() ; i != entries.end() ; ++i )
2012-04-26 20:55:10 +04:00
{
2012-05-06 14:14:36 +04:00
if ( !Collection::IsCollection( *i ) )
{
UpdateFile( *i ) ;
}
2012-04-26 20:55:10 +04:00
}
2012-05-06 14:14:36 +04:00
Json next ;
has_next = resp["feed"]["link"].FindInArray( "rel", "next", next ) ;
if ( has_next )
{
http.Get( next["href"].Str(), &str, m_http_hdr ) ;
resp = str.Response() ;
}
2012-05-06 14:14:36 +04:00
} while ( has_next ) ;
}
Drive::~Drive( )
{
}
struct SortCollectionByHref
{
bool operator()( const Collection& c1, const Collection& c2 ) const
{
return c1.Href() < c2.Href() ;
}
} ;
Drive::FolderListIterator Drive::FindFolder( const std::string& href )
{
// try to find the parent by its href
std::vector<Collection>::iterator it =
std::lower_bound(
m_coll.begin(),
m_coll.end(),
2012-04-26 20:55:10 +04:00
Collection( "", href ),
SortCollectionByHref() ) ;
2012-04-26 20:55:10 +04:00
return (it != m_coll.end() && it->Href() == href) ? it : m_coll.end() ;
}
2012-04-25 20:13:17 +04:00
void Drive::ConstructDirTree( http::Agent *http )
{
http::JsonResponse jrsp ;
http->Get( root_url + "/-/folder?alt=json", &jrsp, m_http_hdr ) ;
Json resp = jrsp.Response() ;
2012-04-26 20:55:10 +04:00
assert( m_coll.empty() ) ;
2012-05-06 14:14:36 +04:00
2012-04-26 20:55:10 +04:00
std::map<std::string, std::string> parent_href ;
2012-05-06 14:14:36 +04:00
while ( true )
2012-04-25 20:13:17 +04:00
{
2012-05-06 14:14:36 +04:00
Json::Array entries = resp["feed"]["entry"].As<Json::Array>() ;
// first, get all collections from the query result
for ( Json::Array::const_iterator i = entries.begin() ; i != entries.end() ; ++i )
2012-04-26 20:55:10 +04:00
{
2012-05-06 14:14:36 +04:00
if ( Collection::IsCollection( *i ) )
{
m_coll.push_back( Collection( *i ) ) ;
parent_href.insert(
std::make_pair(
m_coll.back().Href(),
Collection::ParentHref( *i ) ) ) ;
}
2012-04-26 20:55:10 +04:00
}
2012-05-06 14:14:36 +04:00
assert( m_coll.size() == parent_href.size() ) ;
Json next ;
if ( !resp["feed"]["link"].FindInArray( "rel", "next", next ) )
break ;
http->Get( next["href"].Str(), &jrsp, m_http_hdr ) ;
resp = jrsp.Response() ;
}
2012-04-25 20:13:17 +04:00
// second, build up linkage between parent and child
std::sort( m_coll.begin(), m_coll.end(), SortCollectionByHref() ) ;
for ( FolderListIterator i = m_coll.begin() ; i != m_coll.end() ; ++i )
2012-04-25 20:13:17 +04:00
{
2012-04-26 20:55:10 +04:00
assert( parent_href.find( i->Href() ) != parent_href.end() ) ;
std::string parent = parent_href[i->Href()] ;
if ( parent.empty() )
m_root.AddChild( &*i ) ;
else
{
2012-04-26 20:55:10 +04:00
FolderListIterator pit = FindFolder( parent ) ;
if ( pit != m_coll.end() )
2012-04-26 20:55:10 +04:00
{
// it shouldn't happen, just in case
if ( &*i == &*pit )
std::cout
<< "the parent of folder " << i->Title()
<< " is itself. ignored" << std::endl ;
else
pit->AddChild( &*i ) ;
}
}
2012-04-25 20:13:17 +04:00
}
// lastly, iterating from the root, create the directories in the local file system
2012-04-26 20:55:10 +04:00
assert( m_root.Parent() == 0 ) ;
2012-05-01 13:44:17 +04:00
m_root.CreateSubDir( Path() ) ;
}
2012-04-26 20:55:10 +04:00
void Drive::UpdateFile( const Json& entry )
2012-04-25 20:13:17 +04:00
{
// only handle uploaded files
if ( entry.Has( "docs$suggestedFilename" ) )
2012-04-25 20:13:17 +04:00
{
File file( entry ) ;
bool changed = true ;
Path path = Path() / file.Filename() ;
2012-04-26 20:55:10 +04:00
// determine which folder the file belongs to
if ( !file.Parent().empty() )
2012-04-26 20:55:10 +04:00
{
FolderListIterator pit = FindFolder( file.Parent() ) ;
2012-04-26 20:55:10 +04:00
if ( pit != m_coll.end() )
path = pit->Dir() / file.Filename() ;
2012-04-26 20:55:10 +04:00
}
2012-05-01 13:44:17 +04:00
// compare checksum first if file exists
std::ifstream ifile( path.Str().c_str(), std::ios::binary | std::ios::in ) ;
if ( ifile && file.ServerMD5() == crypt::MD5(ifile.rdbuf()) )
changed = false ;
2012-04-25 20:13:17 +04:00
// if the checksum is different, file is changed and we need to update
if ( changed )
{
DateTime remote = file.ServerModified() ;
DateTime local = ifile ? os::FileMTime( path ) : DateTime() ;
// remote file is newer, download file
2012-04-29 06:22:58 +04:00
if ( !ifile || remote > local )
{
2012-04-26 20:55:10 +04:00
std::cout << "downloading " << path << std::endl ;
file.Download( path, m_http_hdr ) ;
}
else
{
std::cout << "local " << path << " is newer" << std::endl ;
// re-reading the file
ifile.seekg(0) ;
2012-05-05 18:54:36 +04:00
if ( !file.Upload( ifile.rdbuf(), m_http_hdr ) )
std::cout << path << " is read only" << std::endl ;
}
}
2012-04-25 20:13:17 +04:00
}
}
} // end of namespace