listFiles method
Uploads a file from a localPath
to a remotePath
in the cloud.
Implementation
@override
Future<List<CloudFile>> listFiles(
{String path = '', bool recursive = false}) {
return _executeRequest(() async {
final folder = await _getFolderByPath(path);
if (folder == null || folder.id == null) {
return []; // Return empty list if path does not exist.
}
final List<CloudFile> cloudFiles = [];
String? pageToken;
// Loop to handle paginated results from the Drive API.
do {
final fileList = await driveApi.files.list(
spaces: MultiCloudStorage.cloudAccess == CloudAccessType.appStorage
? 'appDataFolder'
: 'drive',
q: "'${folder.id}' in parents and trashed = false",
$fields:
'nextPageToken, files(id, name, size, modifiedTime, mimeType, parents)',
pageToken: pageToken,
);
if (fileList.files != null) {
for (final file in fileList.files!) {
String currentItemPath = join(path, file.name ?? '');
if (path == '/' || path.isEmpty) currentItemPath = file.name ?? '';
// Convert Google Drive file object to a generic CloudFile.
cloudFiles.add(CloudFile(
path: currentItemPath,
name: file.name ?? 'Unnamed',
size: file.size == null ? null : int.tryParse(file.size!),
modifiedTime: file.modifiedTime ?? DateTime.now(),
isDirectory:
file.mimeType == 'application/vnd.google-apps.folder',
metadata: {
'id': file.id,
'mimeType': file.mimeType,
'parents': file.parents
},
));
}
}
pageToken = fileList.nextPageToken;
} while (pageToken != null);
// If recursive is true, fetch files from all subdirectories.
if (recursive) {
final List<CloudFile> subFolderFiles = [];
for (final cf in cloudFiles) {
if (cf.isDirectory) {
subFolderFiles
.addAll(await listFiles(path: cf.path, recursive: true));
}
}
cloudFiles.addAll(subFolderFiles);
}
return cloudFiles;
});
}