private FileStatus toFileStatus(File metadata) { return new FileStatus(metadata.getSize() == null ? 0L : metadata.getSize(), FOLDER_MIME_TYPE.equals(metadata.getMimeType()), -1, -1, metadata.getModifiedTime().getValue(), new Path(metadata.getId())); }
if (FOLDER_MIME_TYPE.equals(file.getMimeType())) { folders.add(new BlobbyStorageContainerResource(file.getName(), file.getId(), null, null)); } else if (FUSION_TABLE_MIME_TYPE.equals(file.getMimeType())) { monitor.info(() -> "Exporting of fusion tables is not yet supported: " + file); } else if (MAP_MIME_TYPE.equals(file.getMimeType())) { monitor.info(() -> "Exporting of maps is not yet supported: " + file); } else { try { InputStream inputStream; String newMimeType = file.getMimeType(); if (EXPORT_FORMATS.containsKey(file.getMimeType())) { newMimeType = EXPORT_FORMATS.get(file.getMimeType()); inputStream = driveInterface new DtpDigitalDocument( file.getName(), file.getModifiedTime().toStringRfc3339(), newMimeType), file.getMimeType(), file.getId())); } catch (Exception e) {
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 resolveFileMetadata() throws Exception { String parentId = null; if ( getName().getParent() != null ) { File parent = searchFile( getName().getParent().getBaseName(), null ); if ( parent != null ) { FileType mime = MIME_TYPES.get( parent.getMimeType() ); if ( mime.equals( FileType.FOLDER ) ) { parentId = parent.getId(); } } } String fileName = getName().getBaseName(); File file = searchFile( fileName, parentId ); if ( file != null ) { mimeType = MIME_TYPES.get( file.getMimeType() ); id = file.getId(); } else { if ( getName().getURI().equals( GoogleDriveFileProvider.SCHEME + ":///" ) ) { mimeType = FileType.FOLDER; } } }
public boolean isDirectory(File file) { if (MIME_TYPE_FOLDER.equals(file.getMimeType())) { return true; } return false; }
private static Stream<File> listSpreadsheets(final Stream<File> all) { return all.filter(f -> Objects.equals(f.getMimeType(), MIME_TYPE_GOOGLE_SPREADSHEET)); }
private static Stream<File> listSpreadsheets(final Stream<File> all) { return all.filter(f -> Objects.equals(f.getMimeType(), MIME_TYPE_GOOGLE_SPREADSHEET)); }
public boolean isGoogleAppsDocument(File file) { String mimeType = file.getMimeType(); if (mimeType != null && mimeType.startsWith("application/vnd.google-apps") && !mimeType.equals("application/vnd.google-apps.folder")) { LOGGER.log(Level.FINE, "Not touching file " + file.getId() + " because it is a Google Apps document."); return true; } return false; }
public boolean isGoogleAppsDocumentforExport(File file) { String mimeType = file.getMimeType(); if (mimeType != null && supportedGooglMimeType.containsKey(mimeType)) { LOGGER.log(Level.FINE, "exporting file " + file.getId() + " because it is a Google Apps document."); return true; } return false; }
public static DriveOverview create(final SessionInfo sessionInfo, final Drive driveService, final Sheets sheetService) throws IOException { final String folderName = getFolderName(sessionInfo); LOGGER.debug("Listing all files in the root of Google Drive, looking up folder: {}.", folderName); final List<File> files = getFilesInFolder(driveService, "root").collect(Collectors.toList()); final Optional<File> result = files.stream() .peek(f -> LOGGER.debug("Found '{}' ({}) as {}.", f.getName(), f.getMimeType(), f.getId())) .filter(f -> Objects.equals(f.getMimeType(), MIME_TYPE_FOLDER)) .filter(f -> Objects.equals(f.getName(), folderName)) .findFirst(); if (result.isPresent()) { return create(sessionInfo, driveService, sheetService, result.get()); } else { return new DriveOverview(sessionInfo, driveService, sheetService); } }
public static DriveOverview create(final SessionInfo sessionInfo, final Drive driveService, final Sheets sheetService) throws IOException { final String folderName = getFolderName(sessionInfo); LOGGER.debug("Listing all files in the root of Google Drive, looking up folder: {}.", folderName); final List<File> files = getFilesInFolder(driveService, "root").collect(Collectors.toList()); final Optional<File> result = files.stream() .peek(f -> LOGGER.debug("Found '{}' ({}) as {}.", f.getName(), f.getMimeType(), f.getId())) .filter(f -> Objects.equals(f.getMimeType(), MIME_TYPE_FOLDER)) .filter(f -> Objects.equals(f.getName(), folderName)) .findFirst(); if (result.isPresent()) { return create(sessionInfo, driveService, sheetService, result.get()); } else { return new DriveOverview(sessionInfo, driveService, sheetService); } }
private FileStatus toFileStatus(File metadata) { return new FileStatus(metadata.getSize() == null ? 0L : metadata.getSize(), FOLDER_MIME_TYPE.equals(metadata.getMimeType()), -1, -1, metadata.getModifiedTime().getValue(), new Path(metadata.getId())); }
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())); }
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())); }
public List<File> listChildren(String parentId) { List<File> resultList = new LinkedList<File>(); Drive drive = driveFactory.getDrive(this.credential); try { Drive.Files.List request = drive.files().list().setFields("nextPageToken, files"); request.setQ("trashed = false and '" + parentId + "' in parents"); request.setPageSize(1000); LOGGER.log(Level.FINE, "Listing children of folder " + parentId + "."); do { FileList fileList = executeWithRetry(options, () -> request.execute()); List<File> items = fileList.getFiles(); resultList.addAll(items); request.setPageToken(fileList.getNextPageToken()); } while (request.getPageToken() != null && request.getPageToken().length() > 0); if (LOGGER.isLoggable(Level.FINE)) { for (File file : resultList) { LOGGER.log(Level.FINE, "Child of " + parentId + ": " + file.getId() + ";" + file.getName() + ";" + file.getMimeType()); } } removeDuplicates(resultList); return resultList; } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to execute list request: " + e.getMessage(), e); } }
/** * @param sourceFolderId source folder ID * @param destinationFolderId folder ID where to copy the sourceFolderId's content * @param newName folder name to assign * @return created folder ID * @throws IOException when operation fails */ public String copyFolder(String sourceFolderId, String destinationFolderId, String newName) throws IOException { LOG.debug("[copyFolder] sourceFolderId: {}; destinationFolderId: {}; newName: {}", sourceFolderId, destinationFolderId, newName); // create a new folder String newFolderId = createFolder(destinationFolderId, newName); // Make a recursive copy of all files/folders inside the source folder String query = format(Q_IN_PARENTS, sourceFolderId) + Q_AND + Q_NOT_TRASHED; FileList originals = drive.files().list().setQ(query).execute(); LOG.debug("[copyFolder] Searching for copy {}", query); for (File file : originals.getFiles()) { if (file.getMimeType().equals(MIME_TYPE_FOLDER)) { copyFolder(file.getId(), newFolderId, file.getName()); } else { copyFile(file.getId(), newFolderId, file.getName(), false); } } return newFolderId; }
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]); }
private GFile create(File file) { GFile newFile = new GFile(file.getName() != null ? file.getName() : file.getOriginalFilename()); newFile.setId(file.getId()); newFile.setLastModified(file.getModifiedTime() != null ? file.getModifiedTime().getValue() : 0); newFile.setDirectory(GFile.MIME_TYPE.GOOGLE_FOLDER.getValue().equals(file.getMimeType())); newFile.setSize(file.getSize() != null ? file.getSize() : 0); // null for directories newFile.setMimeType(file.getMimeType()); newFile.setMd5Checksum(file.getMd5Checksum()); if (file.getParents() != null) { Set<String> newParents = new HashSet<>(); for (String newParent : file.getParents()) { newParents.add(newParent.equals(ROOT_FOLDER_ID) ? "root" : newParent); } newFile.setParents(newParents); } else { // does this happen? newFile.setParents(Collections.singleton("root")); } newFile.setTrashed(file.getTrashed() != null && file.getTrashed()); return newFile; }
private IndexedRecord convertSearchResultToIndexedRecord(File file) { // Main record IndexedRecord main = new GenericData.Record(schema); main.put(0, file.getId()); main.put(1, file.getName()); main.put(2, file.getMimeType()); main.put(3, file.getModifiedTime().getValue()); main.put(4, file.getSize()); main.put(5, file.getKind()); main.put(6, file.getTrashed()); main.put(7, file.getParents().toString()); // TODO This should be a List<String> main.put(8, file.getWebViewLink()); return main; }
private IndexedRecord convertSearchResultToIndexedRecord(File file) { // Main record IndexedRecord main = new GenericData.Record(schema); main.put(0, file.getId()); main.put(1, file.getName()); main.put(2, file.getMimeType()); main.put(3, file.getModifiedTime().getValue()); main.put(4, file.getSize()); main.put(5, file.getKind()); main.put(6, file.getTrashed()); main.put(7, file.getParents().toString()); // TODO This should be a List<String> main.put(8, file.getWebViewLink()); return main; }