protected InputStream doGetInputStream() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); driveService.files().get( id ).executeMediaAndDownloadTo( out ); ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() ); return in; }
protected void doDelete() throws Exception { driveService.files().delete( id ).execute(); id = null; mimeType = null; }
private File searchFile( String fileName, String parentId ) throws Exception { File file = null; StringBuffer fileQuery = new StringBuffer(); fileQuery.append( "name = '" + fileName + "'" ); if ( parentId != null ) { fileQuery.append( " and '" + parentId + "' in parents and trashed=false" ); } FileList fileList = driveService.files().list().setQ( fileQuery.toString() ).execute(); if ( !fileList.getFiles().isEmpty() ) { file = fileList.getFiles().get( 0 ); } return file; }
private void setUp() { client = mock(Drive.class); files = mock(Files.class); when(client.files()).thenReturn(files); }
@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)))); }
@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }
protected String[] doListChildren() throws Exception { String[] children = null; if ( isFolder() ) { id = id == null ? "root" : id; String fileQuery = "'" + id + "' in parents and trashed=false"; FileList files = driveService.files().list().setQ( fileQuery ).execute(); List<String> fileNames = new ArrayList<String>(); for ( File file : files.getFiles() ) { fileNames.add( file.getName() ); } children = fileNames.toArray( new String[0] ); } return children; }
@Override public boolean delete(Path path, boolean recursive) throws IOException { Preconditions.checkArgument(recursive, "Non-recursive is not supported."); String fileId = toFileId(path); LOG.debug("Deleting file: " + fileId); try { client.files().delete(fileId).execute(); } catch (GoogleJsonResponseException e) { GoogleJsonError error = e.getDetails(); if (404 == error.getCode()) { //Non-existing file id return false; } throw e; } return true; }
@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(); }
Drive.Files.List request = client.files() .list() .setFields("files/id,files/mimeType,files/modifiedTime,files/size,files/permissions")
private String importSingleFolder( UUID jobId, Drive driveInterface, String folderName, String folderId, String parentId) throws IOException { File newFolder = new File() .setName(folderName) .setMimeType(DriveExporter.FOLDER_MIME_TYPE); if (!Strings.isNullOrEmpty(parentId)) { newFolder.setParents(ImmutableList.of(parentId)); } File resultFolder = driveInterface.files().create(newFolder).execute(); DriveFolderMapping mapping = new DriveFolderMapping(folderId, resultFolder.getId()); jobStore.update(jobId, folderId, mapping); return resultFolder.getId(); }
protected void doCreateFolder() throws Exception { if ( !getName().getBaseName().isEmpty() ) { File folder = new File(); folder.setName( getName().getBaseName() ); folder.setMimeType( MIME_TYPES.FOLDER.mimeType ); folder = driveService.files().create( folder ).execute(); if ( folder != null ) { id = folder.getId(); mimeType = MIME_TYPES.get( folder.getMimeType() ); } } }
private void importSingleFile( UUID jobId, Drive driveInterface, DigitalDocumentWrapper file, String parentId) throws IOException { InputStreamContent content = new InputStreamContent( null, jobStore.getStream(jobId, file.getCachedContentId())); DtpDigitalDocument dtpDigitalDocument = file.getDtpDigitalDocument(); File driveFile = new File().setName(dtpDigitalDocument.getName()); if (!Strings.isNullOrEmpty(parentId)) { driveFile.setParents(ImmutableList.of(parentId)); } if (!Strings.isNullOrEmpty(dtpDigitalDocument.getDateModified())) { driveFile.setModifiedTime(DateTime.parseRfc3339(dtpDigitalDocument.getDateModified())); } if (!Strings.isNullOrEmpty(file.getOriginalEncodingFormat()) && file.getOriginalEncodingFormat().startsWith("application/vnd.google-apps.")) { driveFile.setMimeType(file.getOriginalEncodingFormat()); } driveInterface.files().create( driveFile, content ).execute(); }
public void close() throws IOException { File file = new File(); file.setName( getName().getBaseName() ); if ( parent != null ) { file.setParents( Collections.singletonList( parent.getId() ) ); } ByteArrayContent fileContent = new ByteArrayContent( "application/octet-stream", toByteArray() ); if ( count > 0 ) { driveService.files().create( file, fileContent ).execute(); ( (GoogleDriveFileSystem) getFileSystem() ).clearFileFromCache( getName() ); } } };
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 FSDataInputStream open(Path path, int bufferSize) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream(), bufferSize)))); }
@Override public FSDataInputStream open(Path path) throws IOException { return closer.register(new FSDataInputStream( new SeekableFSInputStream( new BufferedInputStream( client.files().get(toFileId(path)).executeMediaAsInputStream())))); }
private static Stream<File> getFilesInFolder(final Drive driveService, final String parentId) throws IOException { LOGGER.debug("Listing files in folder {}.", parentId); return driveService.files().list() .setQ("'" + parentId + "' in parents and trashed = false") .setFields("nextPageToken, files(" + getFields("mimeType") + ")") .execute() .getFiles() .stream() .peek(f -> LOGGER.debug("Found '{}' ({}) as {}.", f.getName(), f.getMimeType(), f.getId())); }