Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into s3-getfiles
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.lock
  • Loading branch information
abnegate committed Oct 18, 2023
2 parents cf00a56 + 296a979 commit a2fb79e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Storage/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Utopia\Storage;

use Exception;

abstract class Device
{
/**
Expand Down Expand Up @@ -95,7 +97,7 @@ abstract public function getPath(string $filename, string $prefix = null): strin
* @param array $metadata
* @return int
*
* @throws \Exception
* @throws Exception
*/
abstract public function upload(string $source, string $path, int $chunk = 1, int $chunks = 1, array &$metadata = []): int;

Expand All @@ -113,7 +115,7 @@ abstract public function upload(string $source, string $path, int $chunk = 1, in
* @param array $metadata
* @return int
*
* @throws \Exception
* @throws Exception
*/
abstract public function uploadData(string $data, string $path, string $contentType, int $chunk = 1, int $chunks = 1, array &$metadata = []): int;

Expand Down Expand Up @@ -167,6 +169,10 @@ abstract public function write(string $path, string $data, string $contentType):
*/
public function move(string $source, string $target): bool
{
if ($source === $target) {
return false;
}

if ($this->transfer($source, $target, $this)) {
return $this->delete($source);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Storage/Device/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,15 @@ public function write(string $path, string $data, string $contentType = ''): boo
* @param string $source
* @param string $target
* @return bool
*
* @throws Exception
*/
public function move(string $source, string $target): bool
{
if ($source === $target) {
return false;
}

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));
Expand Down
6 changes: 6 additions & 0 deletions tests/Storage/S3Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ public function testMove()
$this->object->delete($this->object->getPath('text-for-move-new.txt'));
}

public function testMoveIdenticalName()
{
$file = '/kitten-1.jpg';
$this->assertFalse($this->object->move($file, $file));
}

public function testDelete()
{
$this->assertEquals(true, $this->object->write($this->object->getPath('text-for-delete.txt'), 'Hello World', 'text/plain'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Storage/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ public function testExists()
$this->assertEquals(Storage::exists('disk-b'), true);
$this->assertEquals(Storage::exists('disk-c'), false);
}

public function testMoveIdenticalName()
{
$file = '/kitten-1.jpg';
$device = Storage::getDevice('disk-a');
$this->assertFalse($device->move($file, $file));
}
}

0 comments on commit a2fb79e

Please sign in to comment.