Skip to content

Commit

Permalink
Save user sent metadata (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p authored and ankitpokhrel committed Aug 10, 2019
1 parent ccc96bb commit 3d250bb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/File.php
Expand Up @@ -46,6 +46,9 @@ class File
/** @var int */
protected $fileSize;

/** @var string[] */
private $uploadMetadata = [];

/**
* File constructor.
*
Expand Down Expand Up @@ -246,6 +249,18 @@ public function getFilePath() : string
return $this->filePath;
}

/**
* @param string[] $metadata
*
* @return File
*/
public function setUploadMetadata(array $metadata) : self
{
$this->uploadMetadata = $metadata;

return $this;
}

/**
* Get input stream.
*
Expand All @@ -272,6 +287,7 @@ public function details() : array
'checksum' => $this->checksum,
'location' => $this->location,
'file_path' => $this->filePath,
'metadata' => $this->uploadMetadata,
'created_at' => $now->format($this->cache::RFC_7231),
'expires_at' => $now->addSeconds($this->cache->getTtl())->format($this->cache::RFC_7231),
];
Expand Down Expand Up @@ -486,7 +502,7 @@ public function copy(string $source, string $destination) : bool
$status = @copy($source, $destination);

if (false === $status) {
throw new FileException('Cannot copy source to destination.');
throw new FileException(sprintf('Cannot copy source (%s) to destination (%s).', $source, $destination));
}

return $status;
Expand Down
25 changes: 25 additions & 0 deletions src/Request.php
Expand Up @@ -155,6 +155,31 @@ public function extractMeta(string $requestedKey) : string
return '';
}

/**
* Extracts all meta data from the request header.
*
* @return string[]
*/
public function extractAllMeta() : array
{
$uploadMetaData = $this->request->headers->get('Upload-Metadata');

if (empty($uploadMetaData)) {
return [];
}

$uploadMetaDataChunks = explode(',', $uploadMetaData);

$result = [];
foreach ($uploadMetaDataChunks as $chunk) {
list($key, $value) = explode(' ', $chunk);

$result[$key] = base64_decode($value);
}

return $result;
}

/**
* Extract partials from header.
*
Expand Down
6 changes: 3 additions & 3 deletions src/Tus/Server.php
Expand Up @@ -373,7 +373,7 @@ protected function handlePost() : HttpResponse
'size' => $this->getRequest()->header('Upload-Length'),
'file_path' => $filePath,
'location' => $location,
])->setKey($uploadKey)->setChecksum($checksum);
])->setKey($uploadKey)->setChecksum($checksum)->setUploadMetadata($this->getRequest()->extractAllMeta());

$this->cache->set($uploadKey, $file->details() + ['upload_type' => $uploadType]);

Expand Down Expand Up @@ -414,7 +414,7 @@ protected function handleConcatenation(string $fileName, string $filePath) : Htt
'size' => 0,
'file_path' => $filePath,
'location' => $location,
])->setFilePath($filePath)->setKey($uploadKey);
])->setFilePath($filePath)->setKey($uploadKey)->setUploadMetadata($this->getRequest()->extractAllMeta());

$file->setOffset($file->merge($files));

Expand Down Expand Up @@ -466,7 +466,7 @@ protected function handlePatch() : HttpResponse
return $this->response->send(null, $status);
}

$file = $this->buildFile($meta);
$file = $this->buildFile($meta)->setUploadMetadata($meta['metadata'] ?? []);
$checksum = $meta['checksum'];

try {
Expand Down
2 changes: 1 addition & 1 deletion tests/FileTest.php
Expand Up @@ -491,7 +491,7 @@ public function it_deletes_all_files_and_folder(string $path)
* @covers ::merge
*
* @expectedException \TusPhp\Exception\FileException
* @expectedExceptionMessage Cannot copy source to destination.
* @expectedExceptionMessageRegExp /Cannot copy source \(.+\) to destination \(.+\)\./
*/
public function it_throws_file_exception_if_it_cannot_copy_file()
{
Expand Down
112 changes: 108 additions & 4 deletions tests/Tus/ServerTest.php
Expand Up @@ -892,7 +892,8 @@ public function it_handles_post_for_partial_request()

$this->tusServerMock
->shouldReceive('getRequest')
->times(6)
->atLeast()
->once()
->andReturn($requestMock);

$this->tusServerMock
Expand Down Expand Up @@ -935,6 +936,9 @@ public function it_handles_post_for_partial_request()
'checksum' => $checksum,
'location' => $location,
'file_path' => "$baseDir/$folder/$fileName",
'metadata' => [
'filename' => $fileName,
],
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
'upload_type' => 'partial',
Expand Down Expand Up @@ -1006,7 +1010,8 @@ public function it_handles_post_request()

$this->tusServerMock
->shouldReceive('getRequest')
->times(6)
->atLeast()
->once()
->andReturn($requestMock);

$this->tusServerMock
Expand Down Expand Up @@ -1049,6 +1054,9 @@ public function it_handles_post_request()
'checksum' => $checksum,
'location' => $location,
'file_path' => dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
'upload_type' => 'normal',
Expand Down Expand Up @@ -1126,7 +1134,8 @@ public function it_handles_concatenation_request()

$this->tusServerMock
->shouldReceive('getRequest')
->times(4)
->atLeast()
->once()
->andReturn($requestMock);

$cacheMock = m::mock(FileStore::class);
Expand Down Expand Up @@ -1204,6 +1213,14 @@ public function it_handles_concatenation_request()
->with(30)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('merge')
->once()
Expand Down Expand Up @@ -1283,7 +1300,8 @@ public function it_throws_460_for_checksum_mismatch_in_concatenation_request()

$this->tusServerMock
->shouldReceive('getRequest')
->times(4)
->atLeast()
->once()
->andReturn($requestMock);

$cacheMock = m::mock(FileStore::class);
Expand Down Expand Up @@ -1339,6 +1357,14 @@ public function it_throws_460_for_checksum_mismatch_in_concatenation_request()
->with(30)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('merge')
->once()
Expand Down Expand Up @@ -1417,6 +1443,9 @@ public function it_returns_409_for_upload_offset_mismatch()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1479,6 +1508,9 @@ public function it_returns_422_for_file_exception()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1513,6 +1545,14 @@ public function it_returns_422_for_file_exception()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -1571,6 +1611,9 @@ public function it_returns_416_for_corrupt_upload()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1605,6 +1648,14 @@ public function it_returns_416_for_corrupt_upload()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -1663,6 +1714,9 @@ public function it_returns_100_for_aborted_upload()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1691,6 +1745,14 @@ public function it_returns_100_for_aborted_upload()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -1753,6 +1815,9 @@ public function it_returns_403_for_patch_request_against_final_upload()
'size' => $fileSize,
'offset' => 0,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1809,6 +1874,9 @@ public function it_returns_460_for_corrupt_upload()
'offset' => 0,
'checksum' => $checksum,
'file_path' => __DIR__ . '/../Fixtures/empty.txt',
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1843,6 +1911,14 @@ public function it_returns_460_for_corrupt_upload()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -1902,6 +1978,9 @@ public function it_handles_final_patch_request()
'offset' => 0,
'checksum' => $checksum,
'file_path' => __DIR__ . '/../Fixtures/empty.txt',
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -1936,6 +2015,14 @@ public function it_handles_final_patch_request()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -2015,6 +2102,9 @@ public function it_handles_patch_request()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -2049,6 +2139,14 @@ public function it_handles_patch_request()
->with($checksum)
->andReturnSelf();

$fileMock
->shouldReceive('setUploadMetadata')
->once()
->with([
'filename' => $fileName,
])
->andReturnSelf();

$fileMock
->shouldReceive('getFileSize')
->once()
Expand Down Expand Up @@ -2314,6 +2412,9 @@ public function it_handles_download_request()
'size' => $fileSize,
'offset' => 0,
'file_path' => __DIR__ . '/../Fixtures/empty.txt',
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -3056,6 +3157,9 @@ public function it_returns_415_for_content_type_mismatch()
'offset' => 0,
'checksum' => $checksum,
'file_path' => dirname(__DIR__) . DIRECTORY_SEPARATOR . $fileName,
'metadata' => [
'filename' => $fileName,
],
'location' => $location,
'created_at' => 'Fri, 08 Dec 2017 00:00:00 GMT',
'expires_at' => $expiresAt,
Expand Down

0 comments on commit 3d250bb

Please sign in to comment.