Skip to content

Commit

Permalink
Merge pull request #3 from Nexific/feature/#2_list_content_of_current…
Browse files Browse the repository at this point in the history
…_directory

Feature/#2 list content of current directory
  • Loading branch information
virtualmarc authored Feb 20, 2020
2 parents be6e21c + e2f15bb commit 81606be
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# IntelliJ
.idea/
*.iml

# Files and directories created by pub
.dart_tool/
.packages
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.5.0

-
- List files and directories in current directory

## 0.4.0

Expand Down
1 change: 1 addition & 0 deletions lib/ftpclient.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library ftpclient;

export 'src/dto/FTPEnty.dart';
export 'src/ftpclient_base.dart';
export 'src/ftpexceptions.dart';
export 'src/transfermode.dart';
68 changes: 68 additions & 0 deletions lib/src/commands/directory.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import 'dart:io';

import 'package:ftpclient/ftpclient.dart';
import 'package:ftpclient/src/dto/FTPEnty.dart';
import 'package:ftpclient/src/util/transferutil.dart';

import '../ftpexceptions.dart';
import '../ftpsocket.dart';

Expand Down Expand Up @@ -43,4 +49,66 @@ class FTPDirectory {

return sResponse.substring(iStart, iEnde);
}

List<FTPEntry> listDirectoryContent() {
// Transfer mode
TransferUtil.setTransferMode(_socket, TransferMode.ascii);

// Enter passive mode
_socket.sendCommand('PASV');

String sResponse = _socket.readResponse();
if (!sResponse.startsWith('227')) {
throw FTPException('Could not start Passive Mode', sResponse);
}

int iPort = TransferUtil.parsePort(sResponse);

// Directoy content listing
_socket.sendCommand('MLSD');

// Data transfer socket
RawSynchronousSocket dataSocket =
RawSynchronousSocket.connectSync(_socket.host, iPort);

sResponse = _socket.readResponse();
if (!sResponse.startsWith('150')) {
throw FTPException('Can\'t get content of directory.', sResponse);
}

int iToRead = 0;
List<int> lstDirectoryListing = List<int>();

do {
if (iToRead > 0) {
List<int> buffer = List<int>(iToRead);
dataSocket.readIntoSync(buffer);
buffer.forEach(lstDirectoryListing.add);
}

iToRead = dataSocket.available();

if (iToRead == 0 || lstDirectoryListing.isEmpty) {
sleep(Duration(milliseconds: 500));
iToRead = dataSocket.available();
}
} while (iToRead > 0 || lstDirectoryListing.isEmpty);

dataSocket.closeSync();

sResponse = _socket.readResponse();
if (!sResponse.startsWith('226')) {
throw FTPException('Can\'t get content of directory.', sResponse);
}

// Convert MLSD response into FTPEntry
List<FTPEntry> lstFTPEntries = List<FTPEntry>();
String.fromCharCodes(lstDirectoryListing).split('\n').forEach((line) {
if (line.trim().isNotEmpty) {
lstFTPEntries.add(FTPEntry(line));
}
});

return lstFTPEntries;
}
}
93 changes: 93 additions & 0 deletions lib/src/dto/FTPEnty.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'package:ftpclient/ftpclient.dart';

class FTPEntry {
final String name;
final DateTime modifyTime;
final String persmission;
final String type;
final int size;
final String unique;
final String group;
final String mode;
final String owner;

// Hide constructor
FTPEntry._(this.name, this.modifyTime, this.persmission, this.type, this.size,
this.unique, this.group, this.mode, this.owner);

factory FTPEntry(final String sMlsdResponseLine) {
if (sMlsdResponseLine == null || sMlsdResponseLine.trim().isEmpty) {
throw FTPException('Can\'t create instance from empty information');
}

String _name;
DateTime _modifyTime;
String _persmission;
String _type;
int _size = 0;
String _unique;
String _group;
String _mode;
String _owner;

// Split and trim line
sMlsdResponseLine.trim().split(';').forEach((property) {
final prop = property
.split('=')
.map((part) => part.trim())
.toList(growable: false);

if (prop.length == 1) {
// Name
_name = prop[0];
} else {
// Other attributes
switch (prop[0].toLowerCase()) {
case 'modify':
final String date =
prop[1].substring(0, 8) + 'T' + prop[1].substring(8);
_modifyTime = DateTime.parse(date);
break;
case 'perm':
_persmission = prop[1];
break;
case 'size':
_size = int.parse(prop[1]);
break;
case 'type':
_type = prop[1];
break;
case 'unique':
_unique = prop[1];
break;
case 'unix.group':
_group = prop[1];
break;
case 'unix.mode':
_mode = prop[1];
break;
case 'unix.owner':
_owner = prop[1];
break;
default:
throw FTPException('Unknown FTPEntry property \'$property\'');
}
}
});

return FTPEntry._(
_name,
_modifyTime,
_persmission,
_type,
_size,
_unique,
_group,
_mode,
_owner);
}

@override
String toString() =>
'name=$name;modifyTime=$modifyTime;permission=$persmission;type=$type;size=$size;unique=$unique;group=$group;mode=$mode;owner=$owner';
}
8 changes: 7 additions & 1 deletion lib/src/ftpclient_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:ftpclient/src/debug/nooplog.dart';
import 'package:ftpclient/src/debug/printlog.dart';

import 'commands/directory.dart';
import 'dto/FTPEnty.dart';
import 'ftpsocket.dart';
import 'transfermode.dart';

Expand Down Expand Up @@ -48,7 +49,7 @@ class FTPClient {
_socket.connect(_user, _pass);
}

// Disconnect from the FTP Server
/// Disconnect from the FTP Server
void disconnect() {
_socket.disconnect();
}
Expand Down Expand Up @@ -95,6 +96,11 @@ class FTPClient {
return FTPDirectory(_socket).currentDirectory();
}

/// Returns the content of the current directory
List<FTPEntry> listDirectoryContent() {
return FTPDirectory(_socket).listDirectoryContent();
}

/// Rename a file (or directory) from [sOldName] to [sNewName]
bool rename(String sOldName, String sNewName) {
return FTPFile(_socket).rename(sOldName, sNewName);
Expand Down

0 comments on commit 81606be

Please sign in to comment.