2
* Version: MPL 1.1 / GPLv2+ / LGPLv2+
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License or as specified alternatively below. You may obtain a copy of
7
* the License at http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* Major Contributor(s):
15
* Copyright (C) 2013 SUSE <cbosdonnat@suse.com>
16
* 2013 Cao Cuong Ngo <cao.cuong.ngo@gmail.com>
18
* All Rights Reserved.
20
* For minor contributions see the git repository.
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 2 or later (the "GPLv2+"), or
24
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
25
* in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
26
* instead of those above.
29
#include "gdrive-object.hxx"
30
#include "gdrive-property.hxx"
31
#include "gdrive-allowable-actions.hxx"
32
#include "gdrive-repository.hxx"
33
#include "gdrive-utils.hxx"
36
using namespace libcmis;
38
GDriveObject::GDriveObject( GDriveSession* session ) :
39
libcmis::Object( session )
43
GDriveObject::GDriveObject( GDriveSession* session, Json json, string id, string name ) :
44
libcmis::Object( session )
46
initializeFromJson( json, id, name );
49
GDriveObject::GDriveObject( const GDriveObject& copy ) :
50
libcmis::Object( copy )
54
GDriveObject& GDriveObject::operator=( const GDriveObject& copy )
58
libcmis::Object::operator=( copy );
63
void GDriveObject::initializeFromJson ( Json json, string id, string name )
65
Json::JsonObject objs = json.getObjects( );
66
Json::JsonObject::iterator it;
67
for ( it = objs.begin( ); it != objs.end( ); ++it)
71
// in case of a revision, get the ID and name of the original file
72
if ( !id.empty( ) && it->first == "id" )
74
Json idJson( id.c_str( ) );
75
property.reset( new GDriveProperty( "id", idJson ) );
76
m_properties[ property->getPropertyType( )->getId()] = property;
77
property.reset( new GDriveProperty( "revisionId", it->second ) );
78
m_properties[ property->getPropertyType( )->getId()] = property;
80
Json nameJson( name.c_str( ) );
81
property.reset( new GDriveProperty( "cmis:name", nameJson) );
82
m_properties[ property->getPropertyType( )->getId()] = property;
83
property.reset( new GDriveProperty( "cmis:contentStreamFileName", nameJson) );
84
m_properties[ property->getPropertyType( )->getId()] = property;
88
property.reset( new GDriveProperty( it->first, it->second ) );
89
m_properties[ property->getPropertyType( )->getId()] = property;
91
// we map "title" to both "cmis:name" and "cmis:getContentStreamFileName"
92
if ( it->first == "title" )
94
property.reset( new GDriveProperty( "cmis:name", it->second) );
95
m_properties[ property->getPropertyType( )->getId()] = property;
98
// some revision keep the original file name.
99
if ( it->first == "originalFilename" )
101
property.reset( new GDriveProperty( "cmis:name", it->second ) );
102
m_properties[ property->getPropertyType( )->getId()] = property;
103
property.reset( new GDriveProperty( "cmis:contentStreamFileName", it->second) );
104
m_properties[ property->getPropertyType( )->getId()] = property;
107
// In case of a revision, get the modified date as creation date
108
if ( it->first == "modifiedDate" && !id.empty( ) )
110
property.reset( new GDriveProperty( "cmis:creationDate", it->second) );
111
m_properties[ property->getPropertyType( )->getId()] = property;
113
// In case of a revision, get the last modifying user and the creator
114
if ( it->first == "lastModifyingUserName" && !id.empty( ) )
116
property.reset( new GDriveProperty( "cmis:createdBy", it->second) );
117
m_properties[ property->getPropertyType( )->getId()] = property;
121
m_refreshTimestamp = time( NULL );
123
// Create AllowableActions
124
bool isFolder = json["mimeType"].toString( ) == GDRIVE_FOLDER_MIME_TYPE;
125
m_allowableActions.reset( new GdriveAllowableActions( isFolder ) );
128
GDriveSession* GDriveObject::getSession( )
130
return dynamic_cast< GDriveSession* > ( m_session );
133
void GDriveObject::refreshImpl( Json json )
135
m_typeDescription.reset( );
136
m_properties.clear( );
137
initializeFromJson( json );
140
vector< RenditionPtr> GDriveObject::getRenditions( string /* filter */ ) throw ( libcmis::Exception )
142
if ( m_renditions.empty( ) )
144
string downloadUrl = getStringProperty( "downloadUrl" );
145
if ( !downloadUrl.empty( ) )
147
string mimeType = getStringProperty( "cmis:contentStreamMimeType" );
148
if ( !mimeType.empty( ) )
150
RenditionPtr rendition(
151
new Rendition( mimeType, mimeType, mimeType, downloadUrl ));
152
m_renditions.push_back( rendition );
156
vector< string > exportLinks = getMultiStringProperty( "exportLinks" );
157
for ( vector<string>::iterator it = exportLinks.begin( ); it != exportLinks.end( ); ++it)
159
int pos = (*it).find(":\"");
160
if ( pos == -1 ) continue;
161
string mimeType = (*it).substr( 0, pos );
162
string url = (*it).substr( pos + 2, (*it).length( ) - pos - 3 );
163
RenditionPtr rendition(
164
new Rendition( mimeType, mimeType, mimeType, url ) );
165
m_renditions.push_back( rendition );
169
string thumbnailLink = getStringProperty( "thumbnailLink" );
170
if ( !thumbnailLink.empty( ) )
172
string mimeType = "cmis:thumbnail";
173
RenditionPtr rendition(
174
new Rendition( mimeType, mimeType, mimeType, thumbnailLink ));
175
m_renditions.push_back( rendition );
181
libcmis::ObjectPtr GDriveObject::updateProperties(
182
const PropertyPtrMap& properties ) throw ( libcmis::Exception )
184
// Make Json object from properties
185
Json json = GdriveUtils::toGdriveJson( properties );
187
istringstream is( json.toString( ));
189
libcmis::HttpResponsePtr response;
192
vector< string > headers;
193
headers.push_back( "Content-Type: application/json" );
194
response = getSession( )->httpPutRequest( getUrl( ), is, headers );
196
catch ( const CurlException& e )
198
throw e.getCmisException( );
201
string res = response->getStream( )->str( );
202
Json jsonRes = Json::parse( res );
203
libcmis::ObjectPtr updated( new GDriveObject ( getSession( ), jsonRes ) );
205
if ( updated->getId( ) == getId( ) )
206
refreshImpl( jsonRes );
211
void GDriveObject::refresh( ) throw ( libcmis::Exception )
216
res = getSession()->httpGetRequest( getUrl( ) )->getStream( )->str( );
218
catch ( const CurlException& e )
220
throw e.getCmisException( );
222
Json json = Json::parse( res );
226
void GDriveObject::remove( bool /*allVersions*/ ) throw ( libcmis::Exception )
230
getSession( )->httpDeleteRequest( getUrl( ) );
232
catch ( const CurlException& e )
234
throw e.getCmisException( );
238
void GDriveObject::move( FolderPtr /*source*/, FolderPtr destination )
239
throw ( libcmis::Exception )
242
Json parentsValue = GdriveUtils::createJsonFromParentId( destination->getId( ) );
243
parentsJson.add( "parents", parentsValue );
245
istringstream is( parentsJson.toString( ) );
246
libcmis::HttpResponsePtr response;
249
vector< string > headers;
250
headers.push_back( "Content-Type: application/json" );
251
response = getSession( )->httpPutRequest( getUrl( ), is, headers );
253
catch ( const CurlException& e )
255
throw e.getCmisException( );
257
string res = response->getStream( )->str( );
258
Json jsonRes = Json::parse( res );
260
refreshImpl( jsonRes );
263
string GDriveObject::getUrl( )
265
return getSession( )->getBindingUrl( ) + "/files/" + getId( );
268
string GDriveObject::getUploadUrl( )
270
return GDRIVE_UPLOAD_LINKS;
273
vector< string> GDriveObject::getMultiStringProperty( const string& propertyName )
275
vector< string > values;
276
PropertyPtrMap::const_iterator it = getProperties( ).find( string( propertyName ) );
277
if ( it != getProperties( ).end( ) && it->second != NULL && !it->second->getStrings( ).empty( ) )
278
values = it->second->getStrings( );