Skip to content

Instantly share code, notes, and snippets.

@yccheok
Last active December 27, 2018 19:14
Show Gist options
  • Select an option

  • Save yccheok/11bcbaf0dbc1f2bd19c2c139ab975577 to your computer and use it in GitHub Desktop.

Select an option

Save yccheok/11bcbaf0dbc1f2bd19c2c139ab975577 to your computer and use it in GitHub Desktop.
// Code to download.
Drive drive = getDriveService(googleSignInAccount);
com.google.api.services.drive.model.File driveFile = searchFromGoogleDrive(drive);
File downloadedZipFile = downloadFromAppFolder(drive, driveFile);
// Code to upload.
Drive drive = getDriveService(googleSignInAccount);
com.google.api.services.drive.model.File driveFile = searchFromGoogleDrive(drive);
uploadToAppFolder(drive, driveFile, zipFile)
// All Google Drive API related functions.
private static com.google.api.services.drive.model.File searchFromGoogleDrive(Drive drive) {
try {
// TODO: Why "nextPageToken, files(id, name)"?
// TODO: Why 10?
Drive.Files.List request = drive.files().list().setSpaces("appDataFolder")
.setFields("nextPageToken, files(id, name)")
.setPageSize(10);
do {
FileList fileList = request.execute();
com.google.api.services.drive.model.File file = null;
for (com.google.api.services.drive.model.File f : fileList.getFiles()) {
if (isNullOrEmpty(f.getId())) {
continue;
}
file = f;
break;
}
if (file != null) {
return file;
}
request.setPageToken(fileList.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
} catch (UserRecoverableAuthIOException e) {
// TODO: We should have a better way to handle UserRecoverableAuthIOException.
Log.e(TAG, "", e);
return null;
} catch (IOException e) {
Log.e(TAG, "", e);
return null;
}
return null;
}
private static File downloadFromAppFolder(Drive drive, com.google.api.services.drive.model.File file) {
try {
java.io.File outputFile = null;
OutputStream outputStream = null;
try {
outputFile = File.createTempFile(getWeNoteUUID(), ".zip");
outputFile.deleteOnExit();
outputStream = new FileOutputStream(outputFile);
drive.files().get(file.getId())
.executeMediaAndDownloadTo(outputStream);
} catch (IOException ex) {
Log.e(TAG, "", ex);
} finally {
close(outputStream);
}
return outputFile;
} catch (Exception e) {
Log.e(TAG, "", e);
}
return null;
}
private static boolean uploadToAppFolder(Drive drive, com.google.api.services.drive.model.File existingDriveFile, java.io.File file) {
com.google.api.services.drive.model.File driveFile = new com.google.api.services.drive.model.File();
driveFile.setName(getWeNoteZipFileTitle());
driveFile.setParents(Collections.singletonList(APP_DATA_FOLDER));
driveFile.setMimeType(APPLICATION_ZIP);
FileContent fileContent = new FileContent(APPLICATION_ZIP, file);
try {
if (existingDriveFile != null) {
// OVERWRITE EXISTING FILE
drive.files().update(existingDriveFile.getId(), driveFile, fileContent);
} else {
// CREATING NEW FILE
drive.files().create(driveFile, fileContent)
.execute();
}
} catch (IOException e) {
Log.e(TAG, "", e);
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment