Skip to content
Merged
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -139,6 +149,8 @@ $upload = $cdn->uploadByURL('https://example.com/image.png');
echo $upload->getOriginal()->getUrl();
```

> Also supports the `deleteAfter` parameter.

---

## 🧱 Models
Expand All @@ -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`

Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/HamroCDNContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
23 changes: 16 additions & 7 deletions src/HamroCDN.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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,
],
]);

Expand Down
79 changes: 59 additions & 20 deletions tests/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use HamroCDN\Exceptions\HamroCDNException;
use HamroCDN\HamroCDN;
use HamroCDN\Models\File;
use HamroCDN\Models\Upload;
use HamroCDN\Models\User;

Expand All @@ -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');

Expand Down Expand Up @@ -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 () {
Expand Down