Unicode C
Unicode C
Google Drive - Build a Local Cache of Metadata
See more Google Drive Examples
This example demonstrates how to download the metadata for all files in a Google Drive account to create a local filesystem cache with the information. The cache can be used to fetch information without having to query Google Drive.Chilkat Unicode C Downloads
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkCacheW.h>
#include <C_CkDateTimeW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkHashtableW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkStringArrayW.h>
void ChilkatSample(void)
{
BOOL success;
HCkAuthGoogleW gAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkCacheW gdCache;
int numCacheFilesDeleted;
HCkDateTimeW dtExpire;
const wchar_t *allFields;
HCkJsonObjectW jsonMaster;
HCkJsonArrayW jsonMasterArr;
HCkJsonArrayW jsonMasterPaths;
HCkJsonObjectW json;
HCkJsonObjectW jsonFileMetadata;
int i;
int numFiles;
const wchar_t *jsonResponse;
int pageNumber;
const wchar_t *pageToken;
BOOL bContinueLoop;
BOOL bHasMorePages;
HCkHashtableW hashTable;
HCkStringBuilderW sbPathForFileId;
HCkStringArrayW saFileInfo;
const wchar_t *fileId;
HCkStringBuilderW sbPath;
BOOL bFinished;
const wchar_t *strJsonMaster;
success = FALSE;
success = TRUE;
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
gAuth = CkAuthGoogleW_Create();
CkAuthGoogleW_putAccessToken(gAuth,L"GOOGLE_DRIVE_ACCESS_TOKEN");
rest = CkRestW_Create();
// Connect using TLS.
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
CkRestW_SetAuthGoogle(rest,gAuth);
// -------------------------------------------------------------------
// Initialize our cache object. Indicate the location of the root cache directory, and how many cache levels are to exist.
// For small caches (level 0) all cache files are in the root directory.
// For medium caches (level 1) cache files are located in 256 sub-directories from the root.
// For large caches (level 2) cache files are located in 256x256 sub-directories two levels down from the root.
gdCache = CkCacheW_Create();
CkCacheW_putLevel(gdCache,0);
// Use a root directory that makes sense on your operating system..
CkCacheW_AddRoot(gdCache,L"C:/ckCache/googleDrive");
// If we are re-building the cache, we can first delete the entire contents of the cache.
numCacheFilesDeleted = CkCacheW_DeleteAll(gdCache);
// Create a date/time object with an time 7 days from the current date/time.
dtExpire = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(dtExpire);
CkDateTimeW_AddDays(dtExpire,7);
// Indicate that we want ALL possible fields.
// If no fields are indicated, then only the basic fields are returned.
allFields = L"appProperties,capabilities,contentHints,createdTime,description,explicitlyTrashed,fileExtension,folderColorRgb,fullFileExtension,headRevisionId,iconLink,id,imageMediaMetadata,isAppAuthorized,kind,lastModifyingUser,md5Checksum,mimeType,modifiedByMeTime,modifiedTime,name,originalFilename,ownedByMe,owners,parents,permissions,properties,quotaBytesUsed,shared,sharedWithMeTime,sharingUser,size,spaces,starred,thumbnailLink,trashed,version,videoMediaMetadata,viewedByMe,viewedByMeTime,viewersCanCopyContent,webContentLink,webViewLink,writersCanShare";
// We're going to keep a master list of fileId's as we iterate over all the files in this Google Drive account.
// This master list will also be saved to the cache under the key "AllGoogleDriveFileIds".
jsonMaster = CkJsonObjectW_Create();
jsonMasterArr = CkJsonArrayW_Create();
CkJsonObjectW_AppendArray2(jsonMaster,L"fileIds",jsonMasterArr);
// Also keep a list of file paths.
jsonMasterPaths = CkJsonArrayW_Create();
CkJsonObjectW_AppendArray2(jsonMaster,L"filePaths",jsonMasterPaths);
// The default page size is 100, with a max of 1000.
CkRestW_AddQueryParam(rest,L"pageSize",L"200");
json = CkJsonObjectW_Create();
// Send the request for the 1st page.
jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
pageNumber = 1;
bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);
while (bContinueLoop == TRUE) {
wprintf(L"---- Page %d ----\n",pageNumber);
CkJsonObjectW_Load(json,jsonResponse);
numFiles = CkJsonObjectW_SizeOfArray(json,L"files");
i = 0;
while (i < numFiles) {
// Add this file ID to the master list.
CkJsonObjectW_putI(json,i);
CkJsonArrayW_AddStringAt(jsonMasterArr,-1,CkJsonObjectW_stringOf(json,L"files[i].id"));
i = i + 1;
}
// Get the next page of files.
// If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
// for the next request. If no "nextPageToken" was present, then this was the last page of files.
pageToken = CkJsonObjectW_stringOf(json,L"nextPageToken");
bContinueLoop = FALSE;
bHasMorePages = CkJsonObjectW_getLastMethodSuccess(json);
if (bHasMorePages == TRUE) {
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"pageSize",L"200");
CkRestW_AddQueryParam(rest,L"pageToken",pageToken);
jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);
pageNumber = pageNumber + 1;
}
}
// Check to see if the above loop exited with errors...
if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkCacheW_Dispose(gdCache);
CkDateTimeW_Dispose(dtExpire);
CkJsonObjectW_Dispose(jsonMaster);
CkJsonArrayW_Dispose(jsonMasterArr);
CkJsonArrayW_Dispose(jsonMasterPaths);
CkJsonObjectW_Dispose(json);
return;
}
// Check to see if the above loop exited with errors...
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response JSON: %s\n",jsonResponse);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkCacheW_Dispose(gdCache);
CkDateTimeW_Dispose(dtExpire);
CkJsonObjectW_Dispose(jsonMaster);
CkJsonArrayW_Dispose(jsonMasterArr);
CkJsonArrayW_Dispose(jsonMasterPaths);
CkJsonObjectW_Dispose(json);
return;
}
// Iterate over the file IDs and download the metadata for each, saving each to the cache...
// Also, keep in-memory hash entries of the name and parent[0] so we can quickly
// build the path-->fileId cache entries. (Given that the Google Drive REST API uses
// fileIds, this gives us an easy way to lookup a fileId based on a filePath.)
hashTable = CkHashtableW_Create();
// Set the capacity of the hash table to something reasonable for the number of files
// to be hashed.
CkHashtableW_ClearWithNewCapacity(hashTable,521);
sbPathForFileId = CkStringBuilderW_Create();
// Used for storing the file name and parents[0] in the hashTable.
saFileInfo = CkStringArrayW_Create();
CkStringArrayW_putUnique(saFileInfo,FALSE);
numFiles = CkJsonObjectW_SizeOfArray(jsonMaster,L"fileIds");
i = 0;
while (i < numFiles) {
CkJsonObjectW_putI(jsonMaster,i);
fileId = CkJsonObjectW_stringOf(jsonMaster,L"fileIds[i]");
CkStringBuilderW_SetString(sbPathForFileId,L"/drive/v3/files/");
CkStringBuilderW_Append(sbPathForFileId,fileId);
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"fields",allFields);
jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",CkStringBuilderW_getAsString(sbPathForFileId));
if ((CkRestW_getLastMethodSuccess(rest) != TRUE) || (CkRestW_getResponseStatusCode(rest) != 200)) {
// Force an exit of this loop..
numFiles = 0;
}
// Save this file's metadata to the local cache.
// The lookup key is the fileId.
CkCacheW_SaveTextDt(gdCache,fileId,dtExpire,L"",jsonResponse);
// Get this file's name and parent[0], and put this information
// in our in-memory hashtable to be used below..
CkJsonObjectW_Load(json,jsonResponse);
CkStringArrayW_Clear(saFileInfo);
CkStringArrayW_Append(saFileInfo,CkJsonObjectW_stringOf(json,L"name"));
CkStringArrayW_Append(saFileInfo,CkJsonObjectW_stringOf(json,L"parents[0]"));
CkHashtableW_AddStr(hashTable,fileId,CkStringArrayW_serialize(saFileInfo));
wprintf(L"%s, %s\n",CkJsonObjectW_stringOf(json,L"name"),CkJsonObjectW_stringOf(json,L"parents[0]"));
i = i + 1;
}
// Check to see if the above loop exited with errors...
if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkCacheW_Dispose(gdCache);
CkDateTimeW_Dispose(dtExpire);
CkJsonObjectW_Dispose(jsonMaster);
CkJsonArrayW_Dispose(jsonMasterArr);
CkJsonArrayW_Dispose(jsonMasterPaths);
CkJsonObjectW_Dispose(json);
CkHashtableW_Dispose(hashTable);
CkStringBuilderW_Dispose(sbPathForFileId);
CkStringArrayW_Dispose(saFileInfo);
return;
}
// Check to see if the above loop exited with errors...
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response JSON: %s\n",jsonResponse);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkCacheW_Dispose(gdCache);
CkDateTimeW_Dispose(dtExpire);
CkJsonObjectW_Dispose(jsonMaster);
CkJsonArrayW_Dispose(jsonMasterArr);
CkJsonArrayW_Dispose(jsonMasterPaths);
CkJsonObjectW_Dispose(json);
CkHashtableW_Dispose(hashTable);
CkStringBuilderW_Dispose(sbPathForFileId);
CkStringArrayW_Dispose(saFileInfo);
return;
}
// Now that all the fileId's are in the cache, let's build the directory path
// for each fileID.
// (Technically, a fileId can have multiple parents, which means it can be in multiple directories
// at once. This is only going to build directory paths following the 0'th parent ID in the parents list.)
// The directory path for files in "My Drive" will be just the filename.
// For files in sub-directories, the path will be relative, such as "subdir1/subdir2/something.pdf"
//
wprintf(L"---- building paths ----\n");
sbPath = CkStringBuilderW_Create();
numFiles = CkJsonObjectW_SizeOfArray(jsonMaster,L"fileIds");
i = 0;
while (i < numFiles) {
CkJsonObjectW_putI(jsonMaster,i);
CkStringBuilderW_Clear(sbPath);
fileId = CkJsonObjectW_stringOf(jsonMaster,L"fileIds[i]");
bFinished = FALSE;
while ((bFinished == FALSE)) {
CkStringArrayW_Clear(saFileInfo);
CkStringArrayW_AppendSerialized(saFileInfo,CkHashtableW_lookupStr(hashTable,fileId));
// Append this file or directory name.
CkStringBuilderW_Prepend(sbPath,CkStringArrayW_getString(saFileInfo,0));
// Get the parent fileId
fileId = CkStringArrayW_getString(saFileInfo,1);
// If this fileId is not in the hashtable, then it's the fileId for "My Drive", and we are finished.
if (CkHashtableW_Contains(hashTable,fileId) == FALSE) {
bFinished = TRUE;
}
else {
CkStringBuilderW_Prepend(sbPath,L"/");
}
}
wprintf(L"%d: %s\n",i,CkStringBuilderW_getAsString(sbPath));
// Store the filePath --> fileId mapping in our local cache.
fileId = CkJsonObjectW_stringOf(jsonMaster,L"fileIds[i]");
CkCacheW_SaveTextDt(gdCache,CkStringBuilderW_getAsString(sbPath),dtExpire,L"",fileId);
CkJsonArrayW_AddStringAt(jsonMasterPaths,-1,CkStringBuilderW_getAsString(sbPath));
i = i + 1;
}
// Save the master list of file IDs and file paths to the local cache.
CkJsonObjectW_putEmitCompact(jsonMaster,FALSE);
strJsonMaster = CkJsonObjectW_emit(jsonMaster);
CkCacheW_SaveTextNoExpire(gdCache,L"AllGoogleDriveFileIds",L"",strJsonMaster);
wprintf(L"JSON Master Record:\n");
wprintf(L"%s\n",strJsonMaster);
// The JSON Master Cache Record looks something like this:
// An application can load the JSON master record and iterate over all the files
// in Google Drive by file ID, or by path.
// {
// "fileIds": [
// "0B53Q6OSTWYolQlExSlBQT1phZXM",
// "0B53Q6OSTWYolVHRPVkxtYWFtZkk",
// "0B53Q6OSTWYolRGZEV3ZGUTZfNFk",
// "0B53Q6OSTWYolS2FXSjliMXQxSU0",
// "0B53Q6OSTWYolZUhxckMzb0dRMzg",
// "0B53Q6OSTWYolbUF6WS1Gei1oalk",
// "0B53Q6OSTWYola296ODZUSm5GYU0",
// "0B53Q6OSTWYolbTE3c3J5RHBUcHM",
// "0B53Q6OSTWYolTmhybWJSUGd5Q2c",
// "0B53Q6OSTWYolY2tPU1BnYW02T2c",
// "0B53Q6OSTWYolTTBBR2NvUE81Zzg",
// ],
// "filePaths": [
// "testFolder/abc/123/pigs.json",
// "testFolder/starfish20.jpg",
// "testFolder/penguins2.jpg",
// "testFolder/starfish.jpg",
// "testFolder/abc/123/starfish.jpg",
// "testFolder/abc/123/penguins.jpg",
// "testFolder/abc/123",
// "testFolder/abc",
// "testFolder/testHello.txt",
// "testFolder",
// "helloWorld.txt",
// ]
// }
wprintf(L"Entire cache rebuilt...\n");
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkCacheW_Dispose(gdCache);
CkDateTimeW_Dispose(dtExpire);
CkJsonObjectW_Dispose(jsonMaster);
CkJsonArrayW_Dispose(jsonMasterArr);
CkJsonArrayW_Dispose(jsonMasterPaths);
CkJsonObjectW_Dispose(json);
CkHashtableW_Dispose(hashTable);
CkStringBuilderW_Dispose(sbPathForFileId);
CkStringArrayW_Dispose(saFileInfo);
CkStringBuilderW_Dispose(sbPath);
}