Skip to content

Commit 1e17ac3

Browse files
committed
add documentation
1 parent aa0ac71 commit 1e17ac3

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

docs/events.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Events
2+
3+
The system currently supports the following events:
4+
5+
| Name | Passed params | Description |
6+
|------|---------------|-------------|
7+
| AfterMediaSaved | Media | Triggered after a media entity has been successfully saved. |
8+
| AfterMediaUploaded | Media | Triggered upon the successful upload. |

docs/getting-url.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Getting a file URL
2+
3+
```php
4+
// Retrieve the media instance from the specified relation in the model.
5+
6+
$media = $user->avatar;
7+
```
8+
9+
```php
10+
// Gets the URL to the original file.
11+
echo $media->original_url;
12+
13+
// Gets the URL to the preview version of the file.
14+
echo $media->preview_url;
15+
16+
// Gets the relative path to the original file.
17+
echo $media->original_relative_path;
18+
19+
// Gets the absolute path to the original file.
20+
echo $media->original_absolute_path;
21+
22+
// Gets the relative path to the preview version of the file.
23+
echo $media->preview_relative_path;
24+
25+
// Gets the absolute path to the preview version of the file.
26+
echo $media->preview_absolute_path;
27+
```

docs/installation.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Installation
2+
3+
### 1. Install the package via Composer:
4+
5+
```
6+
composer require imahmood/laravel-file-storage
7+
```
8+
9+
### 2. Publish the configuration file:
10+
11+
```bash
12+
php artisan vendor:publish --provider="Imahmood\FileStorage\FileStorageServiceProvider"
13+
```
14+
15+
### 3. Run migrations:
16+
17+
```bash
18+
php artisan migrate
19+
```

docs/methods.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Methods
2+
3+
The `\Imahmood\FileStorage\FileStorage` class offers several methods:
4+
5+
```php
6+
// Specify the disk where the file will be stored.
7+
8+
$this->fileStorage->onDisk($disk);
9+
10+
$this->fileStorage
11+
->onDisk($disk)
12+
->create($type, $relatedTo, $uploadedFile);
13+
```
14+
15+
```php
16+
// Create and associate a new file.
17+
18+
$this->fileStorage->create($type, $relatedTo, $uploadedFile);
19+
```
20+
21+
```php
22+
// Update the existing file.
23+
24+
$this->fileStorage->update($type, $relatedTo, $media, $uploadedFile);
25+
```
26+
27+
```php
28+
// Create a new file or update an existing one.
29+
30+
$this->fileStorage->updateOrCreate($type, $relatedTo, $media, $uploadedFile);
31+
```
32+
33+
```php
34+
// Remove the media instance from both the database and the disk.
35+
36+
$this->fileStorage->delete($media);
37+
```

docs/quick-start.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Quick Start
2+
3+
### 1. Define File Types
4+
Create an enumeration FileType to represent different types of files.
5+
6+
```php
7+
<?php
8+
namespace App\Enums;
9+
10+
use Imahmood\FileStorage\Contracts\MediaTypeInterface;
11+
12+
enum FileType: int implements MediaTypeInterface
13+
{
14+
case USER_AVATAR = 1;
15+
16+
public function identifier(): int
17+
{
18+
return $this->value;
19+
}
20+
}
21+
```
22+
23+
### 2. Preparing your model
24+
Ensure your model correctly implements the `MediaAwareInterface` and define a relation to retrieve the uploaded file.
25+
26+
```php
27+
<?php
28+
namespace App\Models;
29+
30+
use App\Enums\FileType;
31+
use Illuminate\Database\Eloquent\Model;
32+
use Illuminate\Database\Eloquent\Relations\MorphOne;
33+
use Imahmood\FileStorage\Contracts\MediaAwareInterface;
34+
use Imahmood\FileStorage\MediaAwareTrait;
35+
use Imahmood\FileStorage\Models\Media;
36+
37+
class User extends Model implements MediaAwareInterface
38+
{
39+
use MediaAwareTrait;
40+
41+
public function avatar(): MorphOne
42+
{
43+
return $this
44+
->morphOne(Media::class, 'model')
45+
->where(['type' => FileType::USER_AVATAR]);
46+
}
47+
}
48+
```
49+
50+
### 3. Uploading files
51+
52+
```php
53+
<?php
54+
namespace App\Http\Controllers;
55+
56+
use App\Models\User;
57+
use Illuminate\Http\Request;
58+
use Imahmood\FileStorage\FileStorage;
59+
60+
class UsersController extends Controller
61+
{
62+
public function __construct(
63+
private readonly FileStorage $fileStorage,
64+
) {
65+
}
66+
67+
public final store(Request $request, int $userId)
68+
{
69+
$user = User::query()->findOrFail($userId);
70+
71+
$media = $this->fileStorage->create(
72+
type: FileType::USER_AVATAR,
73+
relatedTo: $user,
74+
media: $user->avatar,
75+
uploadedFile: $request->file('avatar'),
76+
);
77+
78+
// ...
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)