listDirectory method
Implementation
Future<void> listDirectory(String path) async {
if (!openDataConnection()) {
return;
}
String fullPath = _getFullPath(path);
logger.generalLog('Listing directory: $fullPath');
if (!_isPathAllowed(fullPath)) {
sendResponse('550 Access denied');
return;
}
Directory dir = Directory(fullPath);
if (await dir.exists()) {
List<FileSystemEntity> entities = dir.listSync();
for (FileSystemEntity entity in entities) {
var stat = await entity.stat();
String permissions = _formatPermissions(stat);
String fileSize = stat.size.toString();
String modificationTime = _formatModificationTime(stat.modified);
String fileName = entity.path.split('/').last;
if (Platform.isWindows) {
fileName = fileName.split("\\").last;
}
String entry =
'$permissions 1 ftp ftp $fileSize $modificationTime $fileName\r\n';
dataSocket!.write(entry);
}
dataSocket!.close();
dataSocket = null;
sendResponse('226 Transfer complete');
} else {
sendResponse('550 Directory not found $fullPath');
}
}