Skip to content

Commit

Permalink
Fetch completed uploads from storage a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
TCB13 committed Oct 21, 2019
1 parent 9fd0752 commit 7e5d4e9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
15 changes: 13 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function setStorageBackend($backend)
return $this;
}

public function getStorageBackend()
{
return $this->backend;
}

/**
* Fetch a finished upload from the current backend storage.
* This method abstracts backend storage file retrivel in a way that the programmer doen't
Expand All @@ -84,12 +89,18 @@ public function setStorageBackend($backend)
*
* @param string $filename
* @param string $destinationDirectory
* @param bool $removeAfter
*
* @return bool
*/
public function fetchFromStorage(string $filename, string $destinationDirectory): bool
public function fetchFromStorage(string $filename, string $destinationDirectory, bool $removeAfter = true): bool
{
return $this->backend->fetchFromStorage($filename, $destinationDirectory, $removeAfter);
}

public function streamFromStorage(string $filename, bool $removeAfter = true)
{
return $this->backend->fetchFromStorage($filename, $destinationDirectory);
return $this->backend->streamFromStorage($filename, $removeAfter);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Store/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ public function delete(string $name): bool
return true;
}

public function fetchFromStorage(string $name, string $destinationDirectory): bool
public function fetchFromStorage(string $name, string $destinationDirectory, bool $removeAfter = true): bool
{
$destinationDirectory = self::normalizePath($destinationDirectory);
if ($destinationDirectory === $this->uploadDir) {
return true;
}
return rename($this->uploadDir . $name, $destinationDirectory . $name);

if ($removeAfter) {
return rename($this->uploadDir . $name, $destinationDirectory . $name);
} else {
return copy($this->uploadDir . $name, $destinationDirectory . $name);
}
}

public function supportsCrossCheck(): bool
Expand Down
32 changes: 28 additions & 4 deletions src/Store/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function delete(string $name): bool
return true;
}

public function fetchFromStorage(string $name, string $destinationDirectory): bool
public function fetchFromStorage(string $name, string $destinationDirectory, bool $removeAfter = true): bool
{
$parts = $this->get($name, true);
if (empty($parts) || $parts === null) {
Expand All @@ -77,17 +77,41 @@ public function fetchFromStorage(string $name, string $destinationDirectory): bo
foreach ($parts as $part) {
$stream = $this->bucket->openDownloadStream($part);
while (!feof($stream)) {
fwrite($file, fread($stream, 5000000));
fwrite($file, fread($stream, 10000000));
}
fclose($stream);
// Delete from mongodb
$this->bucket->delete($part);
// Delete part from mongodb
if ($removeAfter) {
$this->bucket->delete($part);
}
}

fclose($file);
return true;
}

public function streamFromStorage(string $name, bool $removeAfter = true)
{
$parts = $this->get($name, true);
if (empty($parts) || $parts === null) {
return false;
}
$parts = array_column($parts, "_id");

$final = fopen("php://temp", "r+");
foreach ($parts as $part) {
$partStream = $this->bucket->openDownloadStream($part);
stream_copy_to_stream($partStream, $final);
fclose($partStream);
// Delete part from mongodb
if ($removeAfter) {
$this->bucket->delete($part);
}
}

return $final;
}


public function containerExists(string $name): bool
{
Expand Down
6 changes: 4 additions & 2 deletions src/Store/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function delete(string $name): bool
return $this->client->del([self::$prefix . $name]);
}

public function fetchFromStorage(string $name, string $destinationDirectory): bool
public function fetchFromStorage(string $name, string $destinationDirectory, bool $removeAfter = true): bool
{
$data = $this->get($name);
if ($data === null) {
Expand All @@ -76,7 +76,9 @@ public function fetchFromStorage(string $name, string $destinationDirectory): bo
return false;
}

$this->delete($name);
if ($removeAfter) {
$this->delete($name);
}
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Store/StorageBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function getCrossCheckAlgoritms(): array
return [];
}

public function streamFromStorage(string $name, bool $removeAfter = true)
{
throw new ThunderTUSException("The " . static::class . " storage backend hasn't implemented 'streamFromStorage'. Please use 'fetchFromStorage' to fetch the complete file into the local filesystem.");
}

public static function normalizePath(string $path): string
{
return rtrim(realpath($path), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR;
Expand Down
3 changes: 2 additions & 1 deletion src/Store/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public function create(string $name): bool;
public function getSize(string $name): int;
public function append(string $name, $data): bool;
public function delete(string $name): bool;
public function fetchFromStorage(string $name, string $destinationDirectory): bool;
public function fetchFromStorage(string $name, string $destinationDirectory, bool $removeAfter = true): bool;
public function streamFromStorage(string $name, bool $removeAfter = true);

public function supportsCrossCheck(): bool;
public function crossCheck(string $name, string $algo, string $expectedHash): bool;
Expand Down

0 comments on commit 7e5d4e9

Please sign in to comment.