Skip to content

Commit

Permalink
Added invalid root exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Jul 23, 2019
1 parent e6174c9 commit 04550ef
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 33 deletions.
66 changes: 43 additions & 23 deletions src/Storage/Engine/FtpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

namespace Origin\Storage\Engine;

use Origin\Storage\Engine\BaseEngine;

use Origin\Exception\NotFoundException;
use Origin\Exception\Exception;
use Origin\Exception\NotFoundException;
use Origin\Exception\InvalidArgumentException;

class FtpEngine extends BaseEngine
{
protected $defaultConfig =[
protected $defaultConfig = [
'host' => null,
'username' => null,
'password' => null,
Expand All @@ -31,7 +29,7 @@ class FtpEngine extends BaseEngine
'timeout' => 10,
'ssl' => false,
'passive' => true, // passive is the default mode used. e.g. WinSCP
'root' => null
'root' => null,
];

protected $connection = null;
Expand All @@ -48,8 +46,11 @@ public function initialize(array $config)
if ($this->config('root') === null) {
$this->config('root', ftp_pwd($this->connection));
}
}

if (! @ftp_chdir($this->connection, $this->config('root'))) {
throw new InvalidArgumentException(sprintf('Invalid root %s.', $this->config('root')));
}
}

protected function login()
{
Expand All @@ -61,11 +62,12 @@ protected function login()
$this->connection = @ftp_connect($host, $port, $timeout);
}

if (!$this->connection) {
if (! $this->connection) {
throw new Exception(sprintf('Error connecting to %s.', $this->config('host') . ':'. $this->config('port')));
}

if (!@ftp_login($this->connection, $username, $password)) {
if (! @ftp_login($this->connection, $username, $password)) {
$this->disconnect();
throw new Exception('Invalid username or password.');
}
ftp_pasv($this->connection, $passive);
Expand All @@ -83,14 +85,14 @@ public function read(string $name)

$stream = fopen('php://temp', 'w+b'); // +b force binary
$result = @ftp_fget($this->connection, $stream, $filename, FTP_BINARY);
rewind($stream);
$data = stream_get_contents($stream);
fclose($stream);

if ($result) {
rewind($stream);
$data = stream_get_contents($stream);
fclose($stream);

return $data;
}

throw new NotFoundException(sprintf('File %s does not exist', $name));
}

Expand All @@ -106,14 +108,16 @@ public function write(string $name, string $data)
$filename = $this->addPathPrefix($name);

$path = pathinfo($filename, PATHINFO_DIRNAME);
if (!@ftp_chdir($this->connection, $path)) {

if (! @ftp_chdir($this->connection, $path)) {
$this->mkdir($path);
}
$stream = fopen('php://temp', 'w+b'); // +b force binary
fwrite($stream, $data);
rewind($stream);
$result = @ftp_fput($this->connection, $filename, $stream, FTP_BINARY);
fclose($stream);

return $result;
/*
$tmpfile = sys_get_temp_dir() . DS . uniqid();
Expand All @@ -139,11 +143,11 @@ public function delete(string $name)
if ($this->isDir($filename)) {
return $this->rmdir($filename, true);
}
if ($this->fileExists($filename)) {
return ftp_delete($this->connection, $filename);
if (! @ftp_delete($this->connection, $filename)) {
throw new NotFoundException(sprintf('%s does not exist', $name));
}

throw new NotFoundException(sprintf('%s does not exist', $name));
return true;
}

/**
Expand All @@ -163,6 +167,18 @@ public function exists(string $name)
return $this->fileExists($filename);
}

/**
* Disconnects from server
*
* @return void
*/
public function disconnect()
{
if ($this->connection) {
ftp_close($this->connection);
}
$this->connection = null;
}

/**
* Gets a list of items on the disk
Expand All @@ -173,12 +189,14 @@ public function list(string $name = null)
{
$directory = $this->addPathPrefix($name);

if (!$this->isDir($directory)) {
if (! $this->isDir($directory)) {
throw new NotFoundException('directory does not exist');
}

ftp_chdir($this->connection, $this->config('root'));

$this->base = $this->addPathPrefix($name);

return $this->scandir($name);
}

Expand All @@ -189,10 +207,10 @@ protected function fileExists(string $filename)
if (is_array($list) and in_array($filename, $list)) {
return true;
}

return false;
}


/**
* Creates directories recrusively
*
Expand All @@ -201,17 +219,17 @@ protected function fileExists(string $filename)
*/
protected function mkdir(string $path)
{
@ftp_chdir($this->connection, $this->config('root'));
ftp_chdir($this->connection, $this->config('root'));

$parts = array_filter(explode('/', $path));
foreach ($parts as $part) {
if (!@ftp_chdir($this->connection, $part)) {
if (! @ftp_chdir($this->connection, $part)) {
ftp_mkdir($this->connection, $part);
ftp_chmod($this->connection, 0744, $part);
ftp_chdir($this->connection, $part);
}
}
@ftp_chdir($this->connection, $this->config('root'));
ftp_chdir($this->connection, $this->config('root'));
}

/**
Expand All @@ -222,10 +240,11 @@ protected function mkdir(string $path)
*/
protected function isDir(string $directory)
{
if (!@ftp_chdir($this->connection, $directory)) {
if (! @ftp_chdir($this->connection, $directory)) {
return false;
}
ftp_chdir($this->connection, $this->config('root'));

return true;
}

Expand Down Expand Up @@ -255,7 +274,7 @@ protected function scandir(string $directory = null)
$files[] = [
'name' => ltrim(str_replace($this->base . DS, '', $location . DS . $file), '/'),
'timestamp' => ftp_mdtm($this->connection, $location . DS . $file),
'size' => $result[4]
'size' => $result[4],
];
}
}
Expand Down Expand Up @@ -283,6 +302,7 @@ protected function rmdir(string $directory, bool $recursive = true)
ftp_delete($this->connection, $filename);
}
}

return @ftp_rmdir($this->connection, $directory);
}

Expand Down
26 changes: 17 additions & 9 deletions src/Storage/Engine/LocalEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@

namespace Origin\Storage\Engine;

use Origin\Storage\Engine\BaseEngine;
use \RecursiveDirectoryIterator;
use \RecursiveIteratorIterator;
use \RecursiveDirectoryIterator;
use Origin\Exception\NotFoundException;
use Origin\Exception\InvalidArgumentException;

class LocalEngine extends BaseEngine
{
protected $defaultConfig =[
'root' => APP . DS . 'storage'
protected $defaultConfig = [
'root' => APP . DS . 'storage',
];

public function initialize(array $config)
{
if (! file_exists($this->config('root')) and ! is_dir($this->config('root'))) {
throw new InvalidArgumentException(sprintf('Invalid root %s.', $this->config('root')));
}
}

/**
Expand Down Expand Up @@ -57,9 +60,10 @@ public function write(string $name, string $data)
$filename = $this->addPathPrefix($name);

$folder = pathinfo($filename, PATHINFO_DIRNAME);
if (!file_exists($folder)) {
if (! file_exists($folder)) {
mkdir($folder, 0744, true);
}

return (bool) file_put_contents($filename, $data, LOCK_EX);
}

Expand All @@ -82,6 +86,7 @@ public function delete(string $name)
if (is_dir($filename)) {
return $this->rmdir($filename, true);
}

return unlink($filename);
}
throw new NotFoundException(sprintf('%s does not exist', $name));
Expand All @@ -96,6 +101,7 @@ public function delete(string $name)
public function exists(string $name)
{
$filename = $this->addPathPrefix($name);

return file_exists($filename);
}

Expand All @@ -111,18 +117,18 @@ public function list(string $name = null)
if (file_exists($directory)) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));


$files = [];
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$files[] = [
$files[] = [
'name' => str_replace($directory . DS, '', $file->getPathname()),
'timestamp' => $file->getMTime(),
'size' => $file->getSize()
'timestamp' => $file->getMTime(),
'size' => $file->getSize(),
];
}

return $files;
}
throw new NotFoundException('directory does not exist');
Expand All @@ -146,6 +152,7 @@ protected function rmdir(string $directory, bool $recursive = true)
unlink($directory . DS . $filename);
}
}

return @rmdir($directory);
}

Expand All @@ -161,6 +168,7 @@ protected function addPathPrefix(string $path = null)
if ($path) {
$location .= DS . $path;
}

return $location;
}
}
4 changes: 4 additions & 0 deletions src/Storage/Engine/SftpEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function initialize(array $config)
if ($this->config('root') === null) {
$this->config('root', $this->connection->pwd());
}

if (! $this->connection->is_dir($this->config('root'))) {
throw new InvalidArgumentException(sprintf('Invalid root %s.', $this->config('root')));
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/Storage/Engine/FtpEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function engine()

return $this->engine;
}

public function testInvalidRoot()
{
$this->expectException(InvalidArgumentException::class);
$engine = new FtpEngine([
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'root' => '/some-directory/that-does-not-exist',
]);
}

public function testConfig()
{
$config = $this->engine()->config();
Expand Down
12 changes: 11 additions & 1 deletion tests/TestCase/Storage/Engine/LocalEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Origin\Test\Storage\Engine;

use Origin\Storage\Engine\LocalEngine;
use Origin\Exception\InvalidArgumentException;

include_once 'EngineTestTrait.php'; // @todo recreate test with providers maybe

Expand All @@ -25,12 +26,21 @@ class LocalEngineTest extends \PHPUnit\Framework\TestCase
public function engine()
{
if ($this->engine === null) {
$this->engine = new LocalEngine();
$this->engine = new LocalEngine();
}

return $this->engine;
}
public function testConfig()
{
$this->assertEquals(APP . DS . 'storage', $this->engine()->config('root'));
}

public function testInvalidRoot()
{
$this->expectException(InvalidArgumentException::class);
$engine = new LocalEngine([
'root' => '/some-directory/that-does-not-exist',
]);
}
}
11 changes: 11 additions & 0 deletions tests/TestCase/Storage/Engine/SftpEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function testConfig()
$this->assertEquals(10, $config['timeout']);
}

public function testInvalidRoot()
{
$this->expectException(InvalidArgumentException::class);
$engine = new SftpEngine([
'host' => env('SFTP_HOST'),
'username' => env('SFTP_USERNAME'),
'password' => env('SFTP_PASSWORD'),
'root' => '/some-directory/that-does-not-exist',
]);
}

public function testNotFoundPrivateKey()
{
$this->expectException(NotFoundException::class);
Expand Down

0 comments on commit 04550ef

Please sign in to comment.