From feb90b65f941b815043afa486ede9186c67ac4c6 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sat, 28 Dec 2019 00:46:19 +0200 Subject: [PATCH] Added upload move method --- src/Storage/Device.php | 12 +++++++ src/Storage/Devices/Local.php | 33 +++++++++++++++++- tests/unit/Storage/Devices/LocalTest.php | 44 ++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/src/Storage/Device.php b/src/Storage/Device.php index 3015069b4..1e841c2d8 100644 --- a/src/Storage/Device.php +++ b/src/Storage/Device.php @@ -77,6 +77,18 @@ abstract class Device */ abstract public function write(string $path, string $data):bool; + /** + * Move file from given source to given path, Return true on success and false on failure. + * + * @see http://php.net/manual/en/function.filesize.php + * + * @param string $source + * @param string $target + * + * @return bool + */ + abstract public function move(string $source, string $target):bool; + /** * Delete file in given path, Return true on success and false on failure. * diff --git a/src/Storage/Devices/Local.php b/src/Storage/Devices/Local.php index 44652ed83..2c922d2af 100644 --- a/src/Storage/Devices/Local.php +++ b/src/Storage/Devices/Local.php @@ -115,13 +115,44 @@ class Local extends Device * @param string $path * @param string $data * - * @return string + * @return bool */ public function write(string $path, string $data):bool { + if (!file_exists(dirname($path))) { // Checks if directory path to file exists + if (!@mkdir(dirname($path), 0755, true)) { + throw new Exception('Can\'t create directory '.dirname($path)); + } + } + return file_put_contents($path, $data); } + /** + * Move file from given source to given path, Return true on success and false on failure. + * + * @see http://php.net/manual/en/function.filesize.php + * + * @param string $source + * @param string $target + * + * @return bool + */ + public function move(string $source, string $target):bool + { + if (!file_exists(dirname($target))) { // Checks if directory path to file exists + if (!@mkdir(dirname($target), 0755, true)) { + throw new Exception('Can\'t create directory '.dirname($target)); + } + } + + if (rename($source, $target)) { + return true; + } + + throw new Exception('Upload failed'); + } + /** * Delete file in given path, Return true on success and false on failure. * diff --git a/tests/unit/Storage/Devices/LocalTest.php b/tests/unit/Storage/Devices/LocalTest.php index 4e979a915..08fd84000 100644 --- a/tests/unit/Storage/Devices/LocalTest.php +++ b/tests/unit/Storage/Devices/LocalTest.php @@ -43,6 +43,46 @@ class LocalTest extends TestCase $this->assertEquals($this->object->getPath('x.png'), '/usr/share/nginx/html/tests/resources/disk-a/x/./p/n/x.png'); $this->assertEquals($this->object->getPath('y'), '/usr/share/nginx/html/tests/resources/disk-a/y/x/x/x/y'); } + + public function testWrite() + { + $this->assertEquals($this->object->write($this->object->getPath('text.txt'), 'Hello World'), true); + $this->assertEquals(file_exists($this->object->getPath('text.txt')), true); + $this->assertEquals(is_readable($this->object->getPath('text.txt')), true); + + $this->object->delete($this->object->getPath('text.txt')); + } + + public function testRead() + { + $this->assertEquals($this->object->write($this->object->getPath('text-for-read.txt'), 'Hello World'), true); + $this->assertEquals($this->object->read($this->object->getPath('text-for-read.txt')), 'Hello World'); + + $this->object->delete($this->object->getPath('text-for-read.txt')); + } + + public function testMove() + { + $this->assertEquals($this->object->write($this->object->getPath('text-for-move.txt'), 'Hello World'), true); + $this->assertEquals($this->object->read($this->object->getPath('text-for-move.txt')), 'Hello World'); + $this->assertEquals($this->object->move($this->object->getPath('text-for-move.txt'), $this->object->getPath('text-for-move-new.txt')), true); + $this->assertEquals($this->object->read($this->object->getPath('text-for-move-new.txt')), 'Hello World'); + $this->assertEquals(file_exists($this->object->getPath('text-for-move.txt')), false); + $this->assertEquals(is_readable($this->object->getPath('text-for-move.txt')), false); + $this->assertEquals(file_exists($this->object->getPath('text-for-move-new.txt')), true); + $this->assertEquals(is_readable($this->object->getPath('text-for-move-new.txt')), true); + + $this->object->delete($this->object->getPath('text-for-move-new.txt')); + } + + public function testDelete() + { + $this->assertEquals($this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World'), true); + $this->assertEquals($this->object->read($this->object->getPath('text-for-delete.txt')), 'Hello World'); + $this->assertEquals($this->object->delete($this->object->getPath('text-for-delete.txt')), true); + $this->assertEquals(file_exists($this->object->getPath('text-for-delete.txt')), false); + $this->assertEquals(is_readable($this->object->getPath('text-for-delete.txt')), false); + } public function testFileSize() { @@ -68,8 +108,8 @@ class LocalTest extends TestCase public function testDirectorySize() { - $this->assertEquals($this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-a/'), 731597); - $this->assertEquals($this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-b/'), 3728550); + $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-a/')); + $this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-b/')); } public function testPartitionFreeSpace()