-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change representation of failed uploads (possible BC break)
FileUploadChunk has been unusable for most of the upload failures due to the missing content range and size information.
- Loading branch information
Showing
11 changed files
with
168 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Nepada\FileUploadControl\Storage; | ||
|
||
use Nette; | ||
|
||
final class FailedUpload | ||
{ | ||
|
||
use Nette\SmartObject; | ||
|
||
public readonly Nette\Http\FileUpload $fileUpload; | ||
|
||
public readonly ?ContentRange $contentRange; | ||
|
||
private function __construct(Nette\Http\FileUpload $fileUpload, ?ContentRange $contentRange) | ||
{ | ||
if ($fileUpload->isOk()) { | ||
throw new \InvalidArgumentException("Expected failed file upload, but upload of '{$fileUpload->getUntrustedName()}' has been successful."); | ||
} | ||
|
||
$this->fileUpload = $fileUpload; | ||
$this->contentRange = $contentRange; | ||
} | ||
|
||
public static function of(Nette\Http\FileUpload $fileUpload, ?ContentRange $contentRange = null): self | ||
{ | ||
if ($contentRange === null && $fileUpload->getSize() > 0) { | ||
$contentRange = ContentRange::ofSize($fileUpload->getSize()); | ||
} | ||
|
||
return new self($fileUpload, $contentRange); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace NepadaTests\FileUploadControl\Storage; | ||
|
||
use Nepada\FileUploadControl\Storage\ContentRange; | ||
use Nepada\FileUploadControl\Storage\FailedUpload; | ||
use NepadaTests\FileUploadControl\FileUploadFactory; | ||
use NepadaTests\TestCase; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
|
||
/** | ||
* @testCase | ||
*/ | ||
class FailedUploadTest extends TestCase | ||
{ | ||
|
||
public function testCreateWithKnownContentRange(): void | ||
{ | ||
$fileUpload = FileUploadFactory::create('name', 0, 'tmp', UPLOAD_ERR_NO_TMP_DIR); | ||
$contentRange = ContentRange::fromHttpHeaderValue('bytes 0-9/42'); | ||
$failedUpload = FailedUpload::of($fileUpload, $contentRange); | ||
Assert::same($fileUpload, $failedUpload->fileUpload); | ||
Assert::same($contentRange, $failedUpload->contentRange); | ||
} | ||
|
||
public function testCreateWithContentRangeDerivedFromSize(): void | ||
{ | ||
$fileUpload = FileUploadFactory::create('name', 42, 'tmp', UPLOAD_ERR_PARTIAL); | ||
$failedUpload = FailedUpload::of($fileUpload); | ||
Assert::same($fileUpload, $failedUpload->fileUpload); | ||
Assert::notNull($failedUpload->contentRange); | ||
Assert::same(42, $failedUpload->contentRange->getSize()); | ||
} | ||
|
||
public function testCreateWithoutContent(): void | ||
{ | ||
$fileUpload = FileUploadFactory::create('name', 0, 'tmp', UPLOAD_ERR_NO_TMP_DIR); | ||
$failedUpload = FailedUpload::of($fileUpload); | ||
Assert::same($fileUpload, $failedUpload->fileUpload); | ||
Assert::null($failedUpload->contentRange); | ||
} | ||
|
||
public function testOkFileUploadIsRejected(): void | ||
{ | ||
$fileUpload = FileUploadFactory::create('name', 42, 'tmp', UPLOAD_ERR_OK); | ||
Assert::exception( | ||
function () use ($fileUpload): void { | ||
FailedUpload::of($fileUpload); | ||
}, | ||
\InvalidArgumentException::class, | ||
"Expected failed file upload, but upload of 'name' has been successful.", | ||
); | ||
} | ||
|
||
} | ||
|
||
|
||
(new FailedUploadTest())->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters