-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8fcbcc
commit 20e677e
Showing
7 changed files
with
131 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import 'dart:io'; | ||
|
||
import '../ftpexceptions.dart'; | ||
import '../ftpsocket.dart'; | ||
import '../transfermode.dart'; | ||
import '../debug/debuglog.dart'; | ||
import '../util/transferutil.dart'; | ||
|
||
class FileDownload { | ||
final FTPSocket _socket; | ||
final TransferMode _mode; | ||
final DebugLog _log; | ||
|
||
/// File Download Command | ||
FileDownload(this._socket, this._mode, this._log); | ||
|
||
void downloadFile(String sRemoteName, File fLocalFile) { | ||
_log.log('Download $sRemoteName to ${fLocalFile.path}'); | ||
|
||
// Transfer Mode | ||
TransferUtil.setTransferMode(_socket, _mode); | ||
|
||
// 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); | ||
|
||
_socket.sendCommand('RETR $sRemoteName'); | ||
sResponse = _socket.readResponse(true); | ||
if (sResponse.startsWith('550 ')) { | ||
throw FTPException('Remote File $sRemoteName does not exist!', sResponse); | ||
} | ||
|
||
// Transfer file | ||
RandomAccessFile fRAFile = fLocalFile.openSync(mode: FileMode.writeOnly); | ||
|
||
// Data Transfer Socket | ||
_log.log('Opening DataSocket to Port $iPort'); | ||
RawSynchronousSocket dataSocket = RawSynchronousSocket.connectSync(_socket.host, iPort); | ||
|
||
int iToRead = 0; | ||
int iRead = 0; | ||
|
||
do { | ||
if (iToRead > 0) { | ||
List<int> buffer = List<int>(iToRead); | ||
dataSocket.readIntoSync(buffer); | ||
fRAFile.writeFromSync(buffer); | ||
iRead += iToRead; | ||
} | ||
|
||
iToRead = dataSocket.available(); | ||
|
||
if (iToRead == 0 || iRead == 0) { | ||
sleep(Duration(milliseconds: 500)); | ||
iToRead = dataSocket.available(); | ||
} | ||
} while(iToRead > 0 || iRead == 0); | ||
|
||
_log.log('Downloaded: $iRead B'); | ||
|
||
dataSocket.closeSync(); | ||
fRAFile.flushSync(); | ||
fRAFile.closeSync(); | ||
|
||
_socket.readResponse(); | ||
|
||
_log.log('File Downloaded!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import '../ftpsocket.dart'; | ||
import '../transfermode.dart'; | ||
|
||
class TransferUtil { | ||
/// Set the Transfer mode on [socket] to [mode] | ||
static void setTransferMode(FTPSocket socket, TransferMode mode) { | ||
switch(mode) { | ||
case TransferMode.ascii: | ||
// Set to ASCII mode | ||
socket.sendCommand('TYPE A'); | ||
socket.readResponse(); | ||
break; | ||
case TransferMode.binary: | ||
// Set to BINARY mode | ||
socket.sendCommand('TYPE I'); | ||
socket.readResponse(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
/// Parse the Passive Mode Port from the Servers [sResponse] | ||
static int parsePort(String sResponse) { | ||
int iParOpen = sResponse.indexOf('('); | ||
int iParClose = sResponse.indexOf(')'); | ||
|
||
String sParameters = sResponse.substring(iParOpen + 1, iParClose); | ||
List<String> lstParameters = sParameters.split(','); | ||
|
||
int iPort1 = int.parse(lstParameters[lstParameters.length - 2]); | ||
int iPort2 = int.parse(lstParameters[lstParameters.length - 1]); | ||
|
||
return (iPort1 * 256) + iPort2; | ||
} | ||
} |