Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Fix drive cmdline-sample #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
Expand Down Expand Up @@ -149,11 +148,11 @@ public static void main(String[] args) {
/** Uploads a file using either resumable or direct media upload. */
private static File uploadFile(boolean useDirectUpload) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle(UPLOAD_FILE.getName());
fileMetadata.setName(UPLOAD_FILE.getName());

FileContent mediaContent = new FileContent("image/jpeg", UPLOAD_FILE);

Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent);
Drive.Files.Create insert = drive.files().create(fileMetadata, mediaContent);
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());
Expand All @@ -163,7 +162,7 @@ private static File uploadFile(boolean useDirectUpload) throws IOException {
/** Updates the name of the uploaded file to have a "drivetest-" prefix. */
private static File updateFileWithTestSuffix(String id) throws IOException {
File fileMetadata = new File();
fileMetadata.setTitle("drivetest-" + UPLOAD_FILE.getName());
fileMetadata.setName("drivetest-" + UPLOAD_FILE.getName());

Drive.Files.Update update = drive.files().update(id, fileMetadata);
return update.execute();
Expand All @@ -177,12 +176,13 @@ private static void downloadFile(boolean useDirectDownload, File uploadedFile)
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getTitle()));
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()));

MediaHttpDownloader downloader =
new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
Drive.Files.Get get = drive.files().get(uploadedFile.getId());
MediaHttpDownloader downloader = get.getMediaHttpDownloader();
downloader.setDirectDownloadEnabled(useDirectDownload);
downloader.setProgressListener(new FileDownloadProgressListener());
downloader.download(new GenericUrl(uploadedFile.getDownloadUrl()), out);

get.executeMediaAndDownloadTo(out);
}
}