@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }
protected InputStream doGetInputStream() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); driveService.files().get( id ).executeMediaAndDownloadTo( out ); ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() ); return in; }
GenericUrl getFileUrlBuilder = driveService.files().get(fileId).buildHttpRequestUrl(); String imageUrl = getFileUrlBuilder .set("access_token", credential.getAccessToken())
return drive.files().get(gFile.getId()).executeMediaAsInputStream(); } catch (Exception ex) { throw new RuntimeException("Error downloading file " + gFile.getId(), ex);
.setFields("parents") .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")) .execute(); for(String parent : reference.getParents()) { previousParents.append(parent);
public GoogleDriveGetResult getResource(GoogleDriveGetParameters parameters) throws IOException { String fileId = parameters.getResourceId(); File file = getMetadata(fileId, "id,mimeType,fileExtension"); String fileMimeType = file.getMimeType(); String outputFileExt = "." + file.getFileExtension(); LOG.debug("[getResource] Found fileName `{}` [id: {}, mime: {}, ext: {}]", parameters.getResourceId(), fileId, fileMimeType, file.getFileExtension()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); /* Google Apps types */ if (GoogleDriveMimeTypes.GOOGLE_DRIVE_APPS.contains(fileMimeType)) { String exportFormat = parameters.getMimeType().get(fileMimeType).getMimeType(); outputFileExt = parameters.getMimeType().get(fileMimeType).getExtension(); drive.files().export(fileId, exportFormat).executeMediaAndDownloadTo(outputStream); } else { /* Standard fileName */ drive.files().get(fileId).executeMediaAndDownloadTo(outputStream); } byte[] content = outputStream.toByteArray(); if (parameters.isStoreToLocal()) { String localFile = parameters.getOutputFileName(); if (parameters.isAddExt()) { localFile = localFile + ((localFile.endsWith(outputFileExt)) ? "" : outputFileExt); } LOG.info(messages.getMessage("message.writing.resource", parameters.getResourceId(), localFile)); try (FileOutputStream fout = new FileOutputStream(localFile)) { fout.write(content); fout.close(); } } return new GoogleDriveGetResult(fileId, content); }
public GoogleDriveGetResult getResource(GoogleDriveGetParameters parameters) throws IOException { String fileId = parameters.getResourceId(); File file = getMetadata(fileId, "id,mimeType,fileExtension"); String fileMimeType = file.getMimeType(); String outputFileExt = "." + file.getFileExtension(); LOG.debug("[getResource] Found fileName `{}` [id: {}, mime: {}, ext: {}]", parameters.getResourceId(), fileId, fileMimeType, file.getFileExtension()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); /* Google Apps types */ if (GoogleDriveMimeTypes.GOOGLE_DRIVE_APPS.contains(fileMimeType)) { String exportFormat = parameters.getMimeType().get(fileMimeType).getMimeType(); outputFileExt = parameters.getMimeType().get(fileMimeType).getExtension(); drive.files().export(fileId, exportFormat).executeMediaAndDownloadTo(outputStream); } else { /* Standard fileName */ drive.files().get(fileId).executeMediaAndDownloadTo(outputStream); } byte[] content = outputStream.toByteArray(); if (parameters.isStoreToLocal()) { String localFile = parameters.getOutputFileName(); if (parameters.isAddExt()) { localFile = localFile + ((localFile.endsWith(outputFileExt)) ? "" : outputFileExt); } LOG.info(messages.getMessage("message.writing.resource", parameters.getResourceId(), localFile)); try (FileOutputStream fout = new FileOutputStream(localFile)) { fout.write(content); fout.close(); } } return new GoogleDriveGetResult(fileId, content); }
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) { Map<String, Object> results = new HashMap<String, Object>(); String documentPath = (String) workItem.getParameter("DocumentPath"); try { RequiredParameterValidator.validate(this.getClass(), workItem); Drive drive = auth.getDriveService(appName, clientSecret); Drive.Files.Get request = drive.files().get(documentPath); request.getMediaHttpDownloader().setProgressListener(new MediaDownloadProgressListener()); request.getMediaHttpDownloader().setDirectDownloadEnabled(true); InputStream docInputStream = request.executeMediaAsInputStream(); Document doc = new DocumentImpl(); String docBaseName = FilenameUtils.getBaseName(documentPath); String docExtension = FilenameUtils.getExtension(documentPath); doc.setName(docBaseName + "." + docExtension); doc.setIdentifier(documentPath); doc.setLastModified(new Date()); doc.setContent(IOUtils.toByteArray(docInputStream)); results.put(RESULTS_DOCUMENT, doc); workItemManager.completeWorkItem(workItem.getId(), results); } catch (Exception e) { handleException(e); } }
public InputStream downloadFile(SyncItem syncItem) { Drive drive = driveFactory.getDrive(this.credential); try { File remoteFile = syncItem.getRemoteFile().get(); GenericUrl genericUrl = null; if(isGoogleAppsDocumentforExport(remoteFile)){ Optional<String> exportMimeType = supportedGooglMimeType.get(remoteFile.getMimeType()); Export export = drive.files().export(remoteFile.getId(), exportMimeType.get()); genericUrl = export.buildHttpRequestUrl(); }else{ genericUrl = drive.files().get(remoteFile.getId()).set("alt", "media").buildHttpRequestUrl(); } if (genericUrl != null) { HttpRequest httpRequest = drive.getRequestFactory().buildGetRequest(genericUrl); LOGGER.log(Level.FINE, "Downloading file " + remoteFile.getId() + "."); if (!options.isDryRun()) { HttpResponse httpResponse = executeWithRetry(options, () -> httpRequest.execute()); return httpResponse.getContent(); } } else { LOGGER.log(Level.SEVERE, "No download URL for file " + remoteFile); } } catch (Exception e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to download file: " + e.getMessage(), e); } return new ByteArrayInputStream(new byte[0]); }
@Before public void setUp() { try { InputStream testInputStream = IOUtils.toInputStream("test doc content", "UTF-8"); MediaHttpUploader mediaHttpUploader = PowerMockito.mock(MediaHttpUploader.class); MediaHttpDownloader mediaHttpDownloader = PowerMockito.mock(MediaHttpDownloader.class); Drive.Files.Insert gdriveFilesInsert = PowerMockito.mock(Drive.Files.Insert.class); Drive.Files.Get gdriveFilesGet = PowerMockito.mock(Drive.Files.Get.class); when(auth.getDriveService(anyString(), anyString())).thenReturn(gdriveService); when(gdriveService.files()).thenReturn(gdriveFiles); when(gdriveFiles.insert(any(File.class), any(FileContent.class))).thenReturn(gdriveFilesInsert); when(gdriveFiles.get(anyString())).thenReturn(gdriveFilesGet); when(gdriveFilesInsert.getMediaHttpUploader()).thenReturn(mediaHttpUploader); when(gdriveFilesInsert.execute()).thenReturn(new File()); when(gdriveFilesGet.getMediaHttpDownloader()).thenReturn(mediaHttpDownloader); when(gdriveFilesGet.executeMediaAsInputStream()).thenReturn(testInputStream); } catch (Exception e) { fail(e.getMessage()); } }
private File getFileImpl(String fileId, int retry) { try { logger.trace("getFile(" + fileId + ")"); // control we are not exceeding number of requests/second bandwidthController.newRequest(); // get file from google File file = drive.files().get(fileId) .setFields(REQUEST_FILE_FIELDS).execute(); logger.trace("getFile(" + fileId + ") = " + file.getName()); return file; } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == 404) { return null; } throw new RuntimeException("Error while getting list of files", e); } catch (Exception e) { if (retry > 0) { try { Thread.sleep(1000); } catch (InterruptedException e1) { throw new RuntimeException(e1); } logger.warn("Getting file data failed. Retrying... '" + fileId); return getFileImpl(fileId, --retry); } throw new RuntimeException(e); } }
/** * Blocking version of {@link #loadGameState(String, ILoadGameStateResponseListener)} * * @param fileId * @return game state data * @throws IOException */ public byte[] loadGameStateSync(String fileId) throws IOException { InputStream stream = null; byte[] data = null; try { File remoteFile = findFileByNameSync(fileId); if (remoteFile != null) { stream = GApiGateway.drive.files().get(remoteFile.getId()).executeMediaAsInputStream(); data = StreamUtils.copyStreamToByteArray(stream); } } finally { StreamUtils.closeQuietly(stream); } return data; }
@Override public Map<String, String> getMetadata(final Path file) throws BackgroundException { try { final String fileid = this.fileid.getFileid(file, new DisabledListProgressListener()); final Map<String, String> properties = session.getClient().files().get(fileid).setFields("properties") .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute().getProperties(); if(null == properties) { return Collections.emptyMap(); } return properties; } catch(IOException e) { throw new DriveExceptionMappingService().map("Failure to read attributes of {0}", e, file); } }
public File getFile(String id) { Drive drive = driveFactory.getDrive(this.credential); try { File file = executeWithRetry(options, () -> drive.files().get(id).execute()); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "Got file : " + file.getId() + ":" + file.getName()); } return file; } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to execute get file request: " + e.getMessage(), e); } }
@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }