Skip to content

Commit

Permalink
update Ftp and Downloader handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Steeven Andrian committed May 9, 2019
1 parent 58f6515 commit 50f6e59
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"require": {
"php": "^7.2.0",
"ext-fileinfo": "*",
"o2system/kernel": "*"
},
"autoload": {
Expand Down
25 changes: 2 additions & 23 deletions src/Handlers/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)

$this->fileinfo = pathinfo($filePath);
$this->filesize = filesize($filePath);
$this->filemime = $this->getMime($filePath);
$this->filemime = mime_content_type($filePath);
$this->lastModified = filemtime($filePath);

} elseif ($mode === self::MODE_DATASTREAM) {
Expand All @@ -191,7 +191,7 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)
}

$this->filesize = strlen($this->filedata);
$this->filemime = $this->getMime($this->fileinfo[ 'filename' ]);
$this->filemime = mime_content_type($this->fileinfo[ 'filename' ]);
$this->lastModified = time();

} else {
Expand Down Expand Up @@ -225,27 +225,6 @@ public function __construct($filePath, $mode = self::MODE_FILESTREAM)

// ------------------------------------------------------------------------

/**
* Downloader::getMime
*
* @param string $filePath
*
* @return string
*/
private function getMime($filePath)
{
$file = new File($filePath);
$mime = $file->getMime();

if (is_array($mime)) {
$mime = reset($mime);
}

return $mime;
}

// ------------------------------------------------------------------------

/**
* Downloader::forceDownload
*
Expand Down
55 changes: 41 additions & 14 deletions src/Handlers/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// ------------------------------------------------------------------------

use O2System\Spl\Exceptions\RuntimeException;
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
use O2System\Spl\Traits\Collectors\ErrorCollectorTrait;

/**
* Class Ftp
Expand All @@ -24,6 +26,9 @@
*/
class Ftp
{
use ConfigCollectorTrait;
use ErrorCollectorTrait;

/**
* Passive mode flag
*
Expand Down Expand Up @@ -62,8 +67,16 @@ class Ftp
/**
* Ftp::__construct
*/
public function __construct()
public function __construct(array $config = [])
{
$this->setConfig($config);

// Prep the port
$this->config[ 'port' ] = empty($this->config[ 'port' ]) ? 21 : (int)$this->config[ 'port' ];

// Prep the hostname
$this->config[ 'hostname' ] = preg_replace('|.+?://|', '', $this->config[ 'hostname' ]);

language()
->addFilePath(str_replace('Handlers', '', __DIR__) . DIRECTORY_SEPARATOR)
->loadFile('ftp');
Expand All @@ -76,32 +89,28 @@ public function __construct()
*
* Connect to FTP server.
*
* @param array $config Ftp configuration.
*
* @return bool Returns TRUE on success or FALSE on failure.
* @throws RuntimeException
*/
public function connect(array $config = [])
public function connect()
{
// Prep the port
$config[ 'port' ] = empty($config[ 'port' ]) ? 21 : (int)$config[ 'port' ];

// Prep the hostname
$config[ 'hostname' ] = preg_replace('|.+?://|', '', $config[ 'hostname' ]);

if (false === ($this->handle = @ftp_connect($config[ 'hostname' ], $config[ 'port' ]))) {
if (false === ($this->handle = @ftp_connect($this->config[ 'hostname' ], $this->config[ 'port' ]))) {
if ($this->debugMode === true) {
throw new RuntimeException('FTP_E_UNABLE_TO_CONNECT');
}

$this->addError(1, 'FTP_E_UNABLE_TO_CONNECT');

return false;
}

if (false !== (@ftp_login($this->handle, $config[ 'username' ], $config[ 'password' ]))) {
if (false !== (@ftp_login($this->handle, $this->config[ 'username' ], $this->config[ 'password' ]))) {
if ($this->debugMode === true) {
throw new RuntimeException('FTP_E_UNABLE_TO_LOGIN');
}

$this->addError(2, 'FTP_E_UNABLE_TO_LOGIN');

return false;
}

Expand All @@ -110,8 +119,6 @@ public function connect(array $config = [])
ftp_pasv($this->handle, true);
}

$this->config = $config;

return true;
}

Expand Down Expand Up @@ -151,6 +158,8 @@ public function download($remoteFilePath, $localFilePath, $mode = 'auto')
throw new RuntimeException('FTP_E_UNABLE_TO_DOWNLOAD');
}

$this->addError(3, 'FTP_E_UNABLE_TO_DOWNLOAD');

return false;
}

Expand All @@ -174,6 +183,8 @@ protected function isConnected()
throw new RuntimeException('FTP_E_NO_CONNECTION');
}

$this->addError(4, 'FTP_E_NO_CONNECTION');

return false;
}

Expand Down Expand Up @@ -246,6 +257,8 @@ public function rename($oldFilename, $newFilename)
throw new RuntimeException('FTP_UNABLE_TO_RENAME');
}

$this->addError(5, 'FTP_UNABLE_TO_RENAME');

return false;
}

Expand Down Expand Up @@ -278,6 +291,8 @@ public function move($oldRemoteFilePath, $newRemoteFilePath)
throw new RuntimeException('FTP_UNABLE_TO_MOVE');
}

$this->addError(6, 'FTP_UNABLE_TO_MOVE');

return false;
}

Expand Down Expand Up @@ -309,6 +324,8 @@ public function deleteFile($filePath)
throw new RuntimeException('FTP_E_UNABLE_TO_DELETE');
}

$this->addError(7, 'FTP_E_UNABLE_TO_DELETE');

return false;
}

Expand Down Expand Up @@ -353,6 +370,8 @@ public function deleteDir($remotePath)
throw new RuntimeException('FTP_E_UNABLE_TO_DELETE_DIRECTORY');
}

$this->addError(8, 'FTP_E_UNABLE_TO_DELETE_DIRECTORY');

return false;
}

Expand Down Expand Up @@ -461,6 +480,8 @@ public function changeDir($remotePath, $suppressDebug = false)
throw new RuntimeException('FTP_E_UNABLE_TO_CHANGE_DIRECTORY');
}

$this->addError(9, 'FTP_E_UNABLE_TO_CHANGE_DIRECTORY');

return false;
}

Expand Down Expand Up @@ -493,6 +514,8 @@ public function makeDir($remotePath, $permissions = null)
throw new RuntimeException('FTP_E_UNABLE_TO_MAKE_DIRECTORY');
}

$this->addError(10, 'FTP_E_UNABLE_TO_MAKE_DIRECTORY');

return false;
}

Expand Down Expand Up @@ -528,6 +551,8 @@ public function setChmod($remotePath, $mode)
throw new RuntimeException('FTP_E_UNABLE_TO_CHMOD');
}

$this->addError(11, 'FTP_E_UNABLE_TO_CHMOD');

return false;
}

Expand Down Expand Up @@ -572,6 +597,8 @@ public function upload($localFilePath, $remoteFilePath, $mode = 'auto', $permiss
throw new RuntimeException('FTP_E_UNABLE_TO_UPLOAD');
}

$this->addError(12, 'FTP_E_UNABLE_TO_UPLOAD');

return false;
}

Expand Down

0 comments on commit 50f6e59

Please sign in to comment.