getFolderPathAsIds method

Future<List<FolderPathBit>> getFolderPathAsIds(
  1. String path
)

Converts a folder path, like, "path/like/this" to a set of folder id's. If a specified folder doesn't exist, returns null for that Path Bit's ID.

Implementation

Future<List<FolderPathBit>> getFolderPathAsIds(final String path) async {
  print('DEBUG: Looking for $path');
  int depth = 0;
  final pathBits = path.split('/')..removeWhere((element) => element == "");
  final folderIds = <FolderPathBit>[];
  bool exists = false;
  for (final bit in pathBits) {
    print('DEBUG: Looking for $bit');
    if (folderIds.isNotEmpty) {
      if (folderIds.last.id == null) {
        // Last time in the loop, we didn't get a good hit
        folderIds.add(FolderPathBit(bit, null, depth));
        continue; // skip this iteration, so the function from this point will just put entries in with no id's
      }
    }
    final folder = await files.list(
      q: GoogleDriveQueryHelper.fileQuery(
        bit,
        mimeType: MimeType.folder,
        parent: folderIds.isEmpty ? null : folderIds.last.id,
      ),
    );
    if (folder.files == null) {
      print('DEBUG: When searching for $bit, no folders were returned. Are you logged in?');
      throw ('Files are null.');
    }
    // if (folder.files!.isEmpty) {
    //   break;
    // }
    if (folder.files!.length > 1) {
      print('DEBUG: More than one result found, picking the first one!');
    }
    if (folder.files!.isNotEmpty) {
      folderIds.add(FolderPathBit(folder.files![0].name!, folder.files![0].id!, depth));
    } else {
      folderIds.add(FolderPathBit(bit, null, depth));
    }
    depth++;
  }
  return folderIds;
}