Java Code Examples for com.google.api.services.drive.model.FileList#getFiles()

The following examples show how to use com.google.api.services.drive.model.FileList#getFiles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 6 votes vote down vote up
/**
 * Blocking version of {@link #fetchGameStatesSync()}
 *
 * @return game states
 * @throws IOException
 */
public Array<String> fetchGameStatesSync() throws IOException {

    if (!driveApiEnabled)
        throw new UnsupportedOperationException();

    Array<String> games = new Array<String>();

    FileList l = GApiGateway.drive.files().list()
            .setSpaces("appDataFolder")
            .setFields("files(name)")
            .execute();

    for (File f : l.getFiles()) {
        games.add(f.getName());
    }

    return games;
}
 
Example 2
Source File: GoogleDriveAdapter.java    From jdrivesync with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 3
Source File: GoogleDriveUtils.java    From components with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 4
Source File: GoogleDriveAdapter.java    From jdrivesync with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 5
Source File: GoogleDriveFileObject.java    From hop with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: GoogleDriveAbstractListReader.java    From components with Apache License 2.0 5 votes vote down vote up
private boolean processFolder() throws IOException {
    if (folderId == null && !subFolders.isEmpty()) {
        folderId = subFolders.get(0);
        subFolders.remove(0);
        request.setQ(format(query, folderId));
        LOG.debug("query = {} {}.", query, folderId);
    }
    searchResults.clear();
    FileList files = request.execute();
    for (File file : files.getFiles()) {
        if (canAddSubFolder(file.getMimeType())) {
            subFolders.add(file.getId());
        }
        if (canAddFile(file.getMimeType())) {
            searchResults.add(file);
            result.totalCount++;
        }
    }
    request.setPageToken(files.getNextPageToken());
    searchCount = searchResults.size();
    // finished for folderId
    if (StringUtils.isEmpty(request.getPageToken()) || searchCount == 0) {
        folderId = null;
    }

    return searchCount > 0;
}
 
Example 7
Source File: GoogleDriveFileObject.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
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;
}