Added upload move method
This commit is contained in:
parent
e36dd43a03
commit
feb90b65f9
3 changed files with 86 additions and 3 deletions
|
@ -77,6 +77,18 @@ abstract class Device
|
||||||
*/
|
*/
|
||||||
abstract public function write(string $path, string $data):bool;
|
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.
|
* Delete file in given path, Return true on success and false on failure.
|
||||||
*
|
*
|
||||||
|
|
|
@ -115,13 +115,44 @@ class Local extends Device
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $data
|
* @param string $data
|
||||||
*
|
*
|
||||||
* @return string
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function write(string $path, string $data):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);
|
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.
|
* Delete file in given path, Return true on success and false on failure.
|
||||||
*
|
*
|
||||||
|
|
|
@ -44,6 +44,46 @@ class LocalTest extends TestCase
|
||||||
$this->assertEquals($this->object->getPath('y'), '/usr/share/nginx/html/tests/resources/disk-a/y/x/x/x/y');
|
$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()
|
public function testFileSize()
|
||||||
{
|
{
|
||||||
$this->assertEquals($this->object->getFileSize(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), 599639);
|
$this->assertEquals($this->object->getFileSize(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'), 599639);
|
||||||
|
@ -68,8 +108,8 @@ class LocalTest extends TestCase
|
||||||
|
|
||||||
public function testDirectorySize()
|
public function testDirectorySize()
|
||||||
{
|
{
|
||||||
$this->assertEquals($this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-a/'), 731597);
|
$this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-a/'));
|
||||||
$this->assertEquals($this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-b/'), 3728550);
|
$this->assertGreaterThan(0, $this->object->getDirectorySize(__DIR__ . '/../../../resources/disk-b/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPartitionFreeSpace()
|
public function testPartitionFreeSpace()
|
||||||
|
|
Loading…
Reference in a new issue