Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/src/commands/directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FTPDirectory {
Future<String> currentDirectory() async {
FTPReply sResponse = await _socket.sendCommand('PWD');
if (!sResponse.isSuccessCode()) {
throw FTPConnectException(
throw FTPUnableToGetCWDException(
'Failed to get current working directory', sResponse.message);
}

Expand All @@ -61,7 +61,8 @@ class FTPDirectory {
//some server return two lines 125 and 226 for transfer finished
bool isTransferCompleted = response.isSuccessCode();
if (!isTransferCompleted && response.code != 125 && response.code != 150) {
throw FTPConnectException('Connection refused. ', response.message);
throw FTPConnectionRefusedException(
'Connection refused. ', response.message);
}

List<int> lstDirectoryListing = [];
Expand All @@ -74,7 +75,7 @@ class FTPDirectory {
if (!isTransferCompleted) {
response = await _socket.readResponse();
if (!response.isSuccessCode()) {
throw FTPConnectException('Transfer Error.', response.message);
throw FTPTransferException('Transfer Error.', response.message);
}
}

Expand Down
35 changes: 19 additions & 16 deletions lib/src/commands/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ class FTPFile {
}

Future<int> size(String? sFilename) async {
FTPReply sResponse = await (_socket.sendCommand('SIZE $sFilename'));
if (!sResponse.isSuccessCode() &&
_socket.transferType != TransferType.binary) {
//check if ascii mode get refused
//change to binary mode if ascii mode refused
final _socketTransferTypeBackup = _socket.transferType;
await _socket.setTransferType(TransferType.binary);
sResponse = await (_socket.sendCommand('SIZE $sFilename'));
//back to default mode
await _socket.setTransferType(_socketTransferTypeBackup);
}
try {
FTPReply sResponse = await (_socket.sendCommand('SIZE $sFilename'));
if (!sResponse.isSuccessCode() &&
_socket.transferType != TransferType.binary) {
//check if ascii mode get refused
//change to binary mode if ascii mode refused
final _socketTransferTypeBackup = _socket.transferType;
await _socket.setTransferType(TransferType.binary);
sResponse = await (_socket.sendCommand('SIZE $sFilename'));
//back to default mode
await _socket.setTransferType(_socketTransferTypeBackup);
}
return int.parse(sResponse.message.replaceAll('213 ', ''));
} catch (e) {
return -1;
Expand All @@ -65,7 +65,8 @@ class FTPFile {
int fileSize = 0;
fileSize = await FTPFile(_socket).size(sRemoteName);
if (fileSize == -1) {
throw FTPConnectException('Remote File $sRemoteName does not exist!');
throw FTPFileNotExistsException(
'Remote File $sRemoteName does not exist!');
}

// Enter passive mode
Expand All @@ -84,7 +85,8 @@ class FTPFile {
//some server return two lines 125 and 226 for transfer finished
bool isTransferCompleted = response.isSuccessCode();
if (!isTransferCompleted && response.code != 125 && response.code != 150) {
throw FTPConnectException('Connection refused. ', response.message);
throw FTPConnectionRefusedException(
'Connection refused. ', response.message);
}

// Changed to listen mode instead so that it's possible to send information back on downloaded amount
Expand All @@ -111,7 +113,7 @@ class FTPFile {
//Test if All data are well transferred
response = await _socket.readResponse();
if (!response.isSuccessCode()) {
throw FTPConnectException('Transfer Error.', response.message);
throw FTPTransferException('Transfer Error.', response.message);
}
}

Expand Down Expand Up @@ -148,7 +150,8 @@ class FTPFile {
//some server return two lines 125 and 226 for transfer finished
bool isTransferCompleted = response.isSuccessCode();
if (!isTransferCompleted && response.code != 125 && response.code != 150) {
throw FTPConnectException('Connection refused. ', response.message);
throw FTPConnectionRefusedException(
'Connection refused. ', response.message);
}

_socket.logger.log('Start uploading...');
Expand Down Expand Up @@ -180,7 +183,7 @@ class FTPFile {
// Test if All data are well transferred
response = await _socket.readResponse();
if (!response.isSuccessCode()) {
throw FTPConnectException('Transfer Error.', response.message);
throw FTPTransferException('Transfer Error.', response.message);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/src/ftp_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class FTPEntry {

factory FTPEntry.parse(String responseLine, ListCommand cmd) {
if (responseLine.trim().isEmpty) {
throw FTPConnectException("Can't parse a null or blank response line");
throw FTPParsingErrorException(
"Can't parse a null or blank response line");
}
if (cmd == ListCommand.LIST) {
return FTPEntry._parseListCommand(responseLine);
Expand Down Expand Up @@ -152,7 +153,7 @@ class FTPEntry {
} else if (regexpLISTSiiServers.hasMatch(responseLine)) {
return FTPEntry._parseLISTiis(responseLine);
} else {
throw FTPConnectException(
throw FTPParsingErrorException(
'Invalid format <$responseLine> for LIST command response !');
}
}
Expand Down
180 changes: 180 additions & 0 deletions lib/src/ftp_exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,183 @@ class FTPConnectException implements Exception {
return 'FTPConnectException: $message (Response: $response)';
}
}

class FTPParsingErrorException implements Exception {
final String message;
final String? response;

FTPParsingErrorException(this.message, [this.response]);

@override
String toString() {
return 'FTPParsingErrorException: $message (Response: $response)';
}
}

class FTPConnectionTimeoutException implements Exception {
final String message;
final String? response;

FTPConnectionTimeoutException(this.message, [this.response]);

@override
String toString() {
return 'FTPConnectionTimeoutException: $message (Response: $response)';
}
}

class FTPIllegalReplyException implements Exception {
final String message;
final String? response;

FTPIllegalReplyException(this.message, [this.response]);

@override
String toString() {
return 'FTPIllegalReplyException: $message (Response: $response)';
}
}

class FTPESConnectException implements Exception {
final String message;
final String? response;

FTPESConnectException(this.message, [this.response]);

@override
String toString() {
return 'FTPESConnectException: $message (Response: $response)';
}
}

class FTPAccountRequiredException implements Exception {
final String message;
final String? response;

FTPAccountRequiredException(this.message, [this.response]);

@override
String toString() {
return 'FTPAccountRequiredException: $message (Response: $response)';
}
}

class FTPWrongCredentialsException implements Exception {
final String message;
final String? response;

FTPWrongCredentialsException(this.message, [this.response]);

@override
String toString() {
return 'FTPWrongCredentialsException: $message (Response: $response)';
}
}

class FTPUnablePassiveModeException implements Exception {
final String message;
final String? response;

FTPUnablePassiveModeException(this.message, [this.response]);

@override
String toString() {
return 'FTPUnablePassiveModeException: $message (Response: $response)';
}
}

class FTPCannotChangeDirectoryException implements Exception {
final String message;
final String? response;

FTPCannotChangeDirectoryException(this.message, [this.response]);

@override
String toString() {
return 'FTPCannotChangeDirectoryException: $message (Response: $response)';
}
}

class FTPCannotDeleteFolderException implements Exception {
final String message;
final String? response;

FTPCannotDeleteFolderException(this.message, [this.response]);

@override
String toString() {
return 'FTPCannotDeleteFolderException: $message (Response: $response)';
}
}

class FTPCannotDeleteFileException implements Exception {
final String message;
final String? response;

FTPCannotDeleteFileException(this.message, [this.response]);

@override
String toString() {
return 'FTPCannotDeleteFileException: $message (Response: $response)';
}
}

class FTPCannotDownloadException implements Exception {
final String message;
final String? response;

FTPCannotDownloadException(this.message, [this.response]);

@override
String toString() {
return 'FTPCannotDownloadException: $message (Response: $response)';
}
}

class FTPFileNotExistsException implements Exception {
final String message;
final String? response;

FTPFileNotExistsException(this.message, [this.response]);

@override
String toString() {
return 'FTPFileNotExistsException: $message (Response: $response)';
}
}

class FTPConnectionRefusedException implements Exception {
final String message;
final String? response;

FTPConnectionRefusedException(this.message, [this.response]);

@override
String toString() {
return 'FTPConnectionRefusedException: $message (Response: $response)';
}
}

class FTPTransferException implements Exception {
final String message;
final String? response;

FTPTransferException(this.message, [this.response]);

@override
String toString() {
return 'FTPTransferException: $message (Response: $response)';
}
}

class FTPUnableToGetCWDException implements Exception {
final String message;
final String? response;

FTPUnableToGetCWDException(this.message, [this.response]);

@override
String toString() {
return 'FTPUnableToGetCWDException: $message (Response: $response)';
}
}
Loading