diff --git a/README.md b/README.md index 6300d09..fa5de50 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ echo "Fetched: " . $fetched->getOriginal()->getUrl() . PHP_EOL; ## 🚀 Usage +This SDK make use of [public API](https://hamrocdn.com/docs/api) provided by HamroCDN. To get your API key, sign up at [hamrocdn.com](https://hamrocdn.com/dashboard) and navigate to *Edit Profile* page in your dashboard. + ### 1. List Uploads #### 1.1 Paginated @@ -129,6 +131,14 @@ $upload = $cdn->upload('/path/to/image.png'); echo $upload->getNanoId(); // nano ID of the uploaded file ``` +> To delete the file after a certain time, use the `deleteAfter` parameter (in seconds): + +```php +$upload = $cdn->upload('/path/to/image.png', deleteAfter: 3600); // Deletes after 1 hour +``` + +> This will set the `deleteAt` property on the returned `Upload` model. + --- ### 4. Upload by Remote URL @@ -139,6 +149,8 @@ $upload = $cdn->uploadByURL('https://example.com/image.png'); echo $upload->getOriginal()->getUrl(); ``` +> Also supports the `deleteAfter` parameter. + --- ## 🧱 Models @@ -149,13 +161,13 @@ echo $upload->getOriginal()->getUrl(); |------------|--------------------|--------------------------------------| | `nanoId` | `string` | Unique identifier of the upload | | `user` | `User` or `null` | Owner of the file (if authenticated) | -| `deleteAt` | `string` or `null` | Deletion timestamp if temporary | +| `deleteAt` | `Carbon` or `null` | Deletion timestamp if temporary | | `original` | `File` | File information (URL, size) | #### Methods - `getNanoId()`: `string` - `getUser()`: `?User` -- `getDeleteAt()`: `?string` +- `getDeleteAt()`: `?Carbon` - `getOriginal()`: `File` - `toArray()`: `array` diff --git a/src/Contracts/HamroCDNContract.php b/src/Contracts/HamroCDNContract.php index 2dc7904..b29eaf9 100644 --- a/src/Contracts/HamroCDNContract.php +++ b/src/Contracts/HamroCDNContract.php @@ -45,10 +45,10 @@ public function fetch(string $nanoId): Upload; /** * Upload a file to HamroCDN. */ - public function upload(string $filePath): Upload; + public function upload(string $filePath, ?int $deleteAfter): Upload; /** * Upload a file to HamroCDN by URL. */ - public function uploadByURL(string $url): Upload; + public function uploadByURL(string $url, ?int $deleteAfter = null): Upload; } diff --git a/src/HamroCDN.php b/src/HamroCDN.php index 1f2ba29..d2bfafc 100644 --- a/src/HamroCDN.php +++ b/src/HamroCDN.php @@ -101,20 +101,28 @@ public function fetch(string $nanoId): Upload /** * @throws HamroCDNException */ - public function upload(string $filePath): Upload + public function upload(string $filePath, ?int $deleteAfter = null): Upload { if (! file_exists($filePath)) { throw HamroCDNException::fileError($filePath); } $response = $this->post('uploads', [ - 'multipart' => [ + 'multipart' => array_merge( [ - 'name' => 'file', - 'contents' => fopen($filePath, 'r'), - 'filename' => basename($filePath), + [ + 'name' => 'file', + 'contents' => fopen($filePath, 'r'), + 'filename' => basename($filePath), + ], ], - ], + $deleteAfter !== null ? [ + [ + 'name' => 'delete_after', + 'contents' => (string) $deleteAfter, + ], + ] : [] + ), ]); return Upload::fromArray($response['data']); @@ -123,11 +131,12 @@ public function upload(string $filePath): Upload /** * @throws HamroCDNException */ - public function uploadByURL(string $url): Upload + public function uploadByURL(string $url, ?int $deleteAfter = null): Upload { $response = $this->post('upload-from-url', [ 'json' => [ 'url' => $url, + 'delete_after' => $deleteAfter, ], ]); diff --git a/tests/UploadTest.php b/tests/UploadTest.php index fe05ae3..ba5c69c 100644 --- a/tests/UploadTest.php +++ b/tests/UploadTest.php @@ -4,6 +4,7 @@ use HamroCDN\Exceptions\HamroCDNException; use HamroCDN\HamroCDN; +use HamroCDN\Models\File; use HamroCDN\Models\Upload; use HamroCDN\Models\User; @@ -12,15 +13,14 @@ $upload = $this->value; expect($upload) - ->toBeInstanceOf(Upload::class); - - expect($upload) + ->toBeInstanceOf(Upload::class) ->toHaveKey('nanoId') ->toHaveKey('user') ->toHaveKey('delete_at') ->toHaveKey('original'); expect($upload->getOriginal()) + ->toBeInstanceOf(File::class) ->toHaveKey('url') ->toHaveKey('size'); @@ -87,33 +87,72 @@ }); }); -it('uploads a file and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); +describe('uploads a file and returns a HamroCDN object', function () { + test('without delete_after', function () { + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + + $filePath = __DIR__.'/test.png'; + $upload = $client->upload($filePath); - $filePath = __DIR__.'/test.png'; - $upload = $client->upload($filePath); + expect($upload)->toBeUploadObject(); - expect($upload)->toBeUploadObject(); + $fetchedUpload = $client->fetch($upload->getNanoId()); + + expect($fetchedUpload) + ->toBeUploadObject(); + + expect($fetchedUpload->getNanoId())->toBe($upload->getNanoId()); + }); + + test('with delete_after', function () { + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + + $filePath = __DIR__.'/test.png'; + $deleteAfter = 100; + $upload = $client->upload($filePath, $deleteAfter); - $fetchedUpload = $client->fetch($upload->getNanoId()); + expect($upload)->toBeUploadObject(); + expect($upload->getDeleteAt())->not->toBeNull(); - expect($fetchedUpload) - ->toBeUploadObject(); + $fetchedUpload = $client->fetch($upload->getNanoId()); - expect($fetchedUpload->getNanoId())->toBe($upload->getNanoId()); + expect($fetchedUpload) + ->toBeUploadObject(); + expect($fetchedUpload->getNanoId())->toBe($upload->getNanoId()); + expect($fetchedUpload->getDeleteAt())->not->toBeNull(); + }); }); -it('uploads a file by URL and returns a HamroCDN object', function () { - $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); +describe('uploads a file by URL and returns a HamroCDN object', function () { + test('without delete_after', function () { + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + + $fileUrl = 'https://placehold.co/1000x1000/000000/FFFFFF?text=HamroCDN'; - $fileUrl = 'https://placehold.co/1000x1000/000000/FFFFFF?text=HamroCDN'; + $upload = $client->uploadByURL($fileUrl); + expect($upload)->toBeUploadObject(); - $upload = $client->uploadByURL($fileUrl); - expect($upload)->toBeUploadObject(); + $fetchedUpload = $client->fetch($upload->getNanoId()); + expect($fetchedUpload) + ->toBeUploadObject(); + }); - $fetchedUpload = $client->fetch($upload->getNanoId()); - expect($fetchedUpload) - ->toBeUploadObject(); + test('with delete_after', function () { + $client = new HamroCDN('test-api-key', 'https://hamrocdn.com/api'); + + $fileUrl = 'https://placehold.co/1000x1000/000000/FFFFFF?text=HamroCDN'; + $deleteAfter = 100; + + $upload = $client->uploadByURL($fileUrl, $deleteAfter); + + expect($upload)->toBeUploadObject(); + expect($upload->getDeleteAt())->not->toBeNull(); + + $fetchedUpload = $client->fetch($upload->getNanoId()); + expect($fetchedUpload) + ->toBeUploadObject(); + expect($fetchedUpload->getDeleteAt())->not->toBeNull(); + }); }); describe('exception', function () {