receiveFolder method
Retrieves a folder from a Google Drive location to the local device If the remote folder does not exist, function will throw
Implementation
Stream<FolderTransferProgress> receiveFolder(String remoteFolderId, final Directory localDirectory) async* {
print('Copying ID ${remoteFolderId} to ${localDirectory.path}...');
final filesInFolder = await files.list(q: GoogleDriveQueryHelper.fileListQuery(remoteFolderId));
if (filesInFolder.files != null) {
for (final entity in filesInFolder.files!) {
// Could either be a file or a folder
if (entity.mimeType == MimeType.folder) {
final childDirectory = Directory(p.join(localDirectory.path, entity.name));
if (!(await childDirectory.exists())) {
await childDirectory.create(recursive: true);
}
if (entity.id == null) {
print('ID of found google drive folder is null? This is a bug, please open a bug report');
} else {
print('Found ${entity.name} folder, (ID: ${entity.id}). Recursively copying.');
yield* receiveFolder(entity.id!, childDirectory);
}
} else {
print('Processing file copy for ${entity.name} (with mimetype of ${entity.mimeType}, copying to directory ${localDirectory.path}');
final file = await files.get(entity.id!, downloadOptions: drive.DownloadOptions.fullMedia);
if (file is drive.Media) {
final filePath = p.join(localDirectory.path, entity.name);
final destinationFile = File(filePath);
await for (final bytes in file.stream) {
// calling append on a nonexistant file creates that file.
destinationFile.writeAsBytes(bytes, mode: FileMode.append);
}
} else {
print("WARNING: Received a file that wasn't of type media. Instead, it was of type ${file.runtimeType.toString()}");
}
}
}
}
// final folderIdTree = ['root', ...remoteFolderIds.map((e) => e.id)];
}