protected InputStream doGetInputStream() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); driveService.files().get( id ).executeMediaAndDownloadTo( out ); ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() ); return in; }
@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }
@Override public FSDataInputStream open(Path path, int bufferSize) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream(), bufferSize)))); }
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 getMetadata(String id, String metadata) throws IOException { LOG.debug("[getMetadata] id={}, metadata={}.", id, metadata); return drive.files().get(id).setFields(metadata).execute(); }
public File getMetadata(String id, String metadata) throws IOException { LOG.debug("[getMetadata] id={}, metadata={}.", id, metadata); return drive.files().get(id).setFields(metadata).execute(); }
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); } }
public static com.google.api.services.drive.model.File getFile(final Drive driveService, final String id) throws IOException { LOGGER.debug("Loading file: {}.", id); return driveService.files().get(id) .setFields("id,name,modifiedTime") .execute(); }
@Override public FileStatus getFileStatus(Path p) throws IOException { Preconditions.checkNotNull(p); String fileId = toFileId(p); File metadata = client.files().get(fileId) .setFields("id,mimeType,modifiedTime,size,permissions") .execute(); return toFileStatus(metadata); }
@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }
@Override public FSDataInputStream open(Path path, int bufferSize) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream(), bufferSize)))); }
public static com.google.api.services.drive.model.File getFile(final Drive driveService, final String id) throws IOException { LOGGER.debug("Loading file: {}.", id); return driveService.files().get(id) .setFields("id,name,modifiedTime") .execute(); }
driveInterface .files() .get(file.getId()) .setAlt("media") .executeMedia()
@Override public FileStatus getFileStatus(Path p) throws IOException { Preconditions.checkNotNull(p); String fileId = toFileId(p); File metadata = client.files().get(fileId) .setFields("id,mimeType,modifiedTime,size,permissions") .execute(); return toFileStatus(metadata); }
public void closeTest() throws IOException, FileBasedHelperException { State state = new State(); setUp(); GoogleDriveFsHelper fsHelper = new GoogleDriveFsHelper(state, client, Closer.create()); Get getResult = mock(Get.class); InputStream is = mock(InputStream.class); when(client.files()).thenReturn(files); when(files.get(anyString())).thenReturn(getResult); when(getResult.executeMediaAsInputStream()).thenReturn(is); fsHelper.getFileStream("test"); fsHelper.close(); verify(is, times(1)).close(); }