Skip to content

Commit

Permalink
Merge pull request #83 from utopia-php/feat-list-files-local
Browse files Browse the repository at this point in the history
Feat: public getFiles in local device
  • Loading branch information
eldadfux authored Aug 21, 2023
2 parents c2d1761 + 0b2ada4 commit efec537
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/Storage/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ abstract public function getPartitionFreeSpace(): float;
*/
abstract public function getPartitionTotalSpace(): float;

/**
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
abstract public function getFiles(string $dir): array;

/**
* Get the absolute path by resolving strings like ../, .., //, /\ and so on.
*
Expand Down
24 changes: 14 additions & 10 deletions src/Storage/Device/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,23 @@ public function deletePath(string $path): bool
{
$path = realpath($this->getRoot().DIRECTORY_SEPARATOR.$path);

if (\is_dir($path)) {
$files = $this->getFiles($path);
if (! file_exists($path) || ! is_dir($path)) {
return false;
}

foreach ($files as $file) {
$files = $this->getFiles($path);

foreach ($files as $file) {
if (is_dir($file)) {
$this->deletePath(\ltrim($file, $this->getRoot().DIRECTORY_SEPARATOR));
} else {
$this->delete($file, true);
}

\rmdir($path);

return true;
}

return false;
\rmdir($path);

return true;
}

/**
Expand Down Expand Up @@ -498,12 +502,12 @@ public function getPartitionTotalSpace(): float
}

/**
* Get all files inside a directory.
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
private function getFiles(string $dir): array
public function getFiles(string $dir): array
{
if (! (\str_ends_with($dir, DIRECTORY_SEPARATOR))) {
$dir .= DIRECTORY_SEPARATOR;
Expand Down
11 changes: 11 additions & 0 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,17 @@ public function getPartitionTotalSpace(): float
return -1;
}

/**
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
public function getFiles(string $dir): array
{
throw new Exception('Not implemented.');
}

/**
* Get file info
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Storage/Device/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,37 @@ public function testDeletePath()
$this->assertEquals(false, $this->object->exists($path2));
$this->assertEquals(false, $this->object->exists($path3));
}

public function testGetFiles()
{
$dir = DIRECTORY_SEPARATOR.'get-files-test';

$this->assertTrue($this->object->createDirectory($dir));

$files = $this->object->getFiles($dir);
$this->assertEquals(0, \count($files));

$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file-two.txt', 'Hello World');

$files = $this->object->getFiles($dir);
$this->assertEquals(2, \count($files));
}

public function testNestedDeletePath()
{
$dir = $this->object->getPath('nested-delete-path-test');
$dir2 = $dir.DIRECTORY_SEPARATOR.'dir2';
$dir3 = $dir2.DIRECTORY_SEPARATOR.'dir3';

$this->assertTrue($this->object->createDirectory($dir));
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir2));
$this->object->write($dir2.DIRECTORY_SEPARATOR.'new-file-2.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir3));
$this->object->write($dir3.DIRECTORY_SEPARATOR.'new-file-3.txt', 'Hello World');

$this->assertTrue($this->object->deletePath('nested-delete-path-test'));
$this->assertFalse($this->object->exists($dir));
}
}

0 comments on commit efec537

Please sign in to comment.