Skip to content

Commit 98f61e1

Browse files
committed
update readme with better examples, update route
1 parent a8afc68 commit 98f61e1

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

readme.md

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ Laravel File API is good way to handle files with Laravel Storage.
88

99
1. Install File API
1010

11-
```php
1211
composer require unisharp/laravel-fileapi
13-
```
1412

1513
1. Set service provider in `config/app.php`
1614

@@ -20,78 +18,120 @@ Laravel File API is good way to handle files with Laravel Storage.
2018

2119
1. publish config file
2220

23-
```php
2421
php artisan vendor:publish --tag=fileapi_config
25-
```
2622

2723
### Config
2824

29-
1. fill in the storage path, it will generate routes for your files.
25+
in `config/fileapi.php`
26+
27+
1. fill in the storage path, which make routes for you.
28+
29+
```php
30+
'path' => ['/images/event/', '/images/article/'],
31+
```
3032

31-
'path' => ['/images/event/'],
33+
it will generate routes like below :
34+
35+
```php
36+
Route::get('/images/event/{filename}', function ($filename) {
37+
$entry = new \Unisharp\FileApi\FileApi('/images/event/');
38+
return $entry->getResponse($filename);
39+
});
40+
41+
Route::get('/images/article/{filename}', function ($filename) {
42+
$entry = new \Unisharp\FileApi\FileApi('/images/article/');
43+
return $entry->getResponse($filename);
44+
});
45+
```
3246

3347
1. set default thumb sizes(by key and value)
3448

49+
```php
3550
'default_thumbs' => ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'],
51+
```
3652

3753
### Initialize File API
3854

55+
```php
3956
use \Unisharp\FileApi\FileApi;
4057

4158
$fa = new FileApi();
59+
```
4260

4361
or
4462

45-
$fa = new FileApi('/images'); # initialize it by giving a base path
63+
```php
64+
$fa = new FileApi('/images/event/'); # initialize it by giving a base path
65+
$fa_article = new FileApi('/images/article/'); # initiate another instance
66+
```
4667

4768

4869
### Save to Storage By Giving Uploaded File
4970

5071
* Default Usage : get unique filename
5172

52-
$file = $fa->save(\Input::file('image')); // => wfj412.jpg
73+
```php
74+
$file = $fa->save(\Input::file('main_image')); // => wfj412.jpg
75+
```
5376

5477
* Custimize your upload file name
5578

56-
$file = $fa->save(\Input::file('image'), 'custimized_filename'); // => custimized_filename.jpg
79+
```php
80+
$file = $fa->save(\Input::file('main_image'), 'custimized_filename'); // => custimized_filename.jpg
81+
```
5782

5883
### Save thumbnails
5984

6085
* By default will set three thumbs(equal scaling)
6186

62-
$file = $fa->save(\Input::file('image'));
87+
```php
88+
$file = $fa->save(\Input::file('main_image'));
89+
```
6390

6491
* Set custom thumb sizes
6592

93+
```php
6694
$file = $fa
6795
->thumbs(['S' => '150x100', 'M' => '300x200', 'L' => '450x300'])
68-
->save(\Input::file('image'));
96+
->save(\Input::file('main_image'));
97+
```
6998

7099
* make cropped thumbs
71100

72-
$file = $fa->crop()->save(\Input::file('image'));
101+
```php
102+
$file = $fa->crop()->save(\Input::file('main_image'));
103+
```
73104

74105
### Get file fullpath (abstract path from Laravel Storage)
75106

76-
$fa->getPath('wfj412.jpg'); // => '/images/wfj412.jpg'
107+
```php
108+
$fa->getPath('wfj412.jpg'); // => '/images/event/wfj412.jpg'
109+
```
77110

78111
### Parse File Path to URL
79112
if you store your file into cloud storage and you want to get url cloud site,
80113
you can use url() method to get it
81114

115+
```php
82116
echo $fa->getUrl('wfjsdf.jpg'); // => "https://s3-ap-northeast-1.amazonaws.com/xxx/xxx/55c1e027caa62L.png"
117+
```
83118

84119
### Work with Laravel Storage
85120

86121
* Get file content
87122

123+
```php
88124
\Storage::get($fa->getPath('wfj412.jpg'));
125+
```
89126

90127
* Write files
91128

129+
```php
92130
\Storage::put($fa->getPath('wfj412.jpg'));
131+
```
93132

94133
* Get Mime Type
95134

135+
```php
96136
\Storage::mimeType($fa->getPath('wfj412.jpg'));
97-
137+
```

src/route.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
if (!empty($paths)) {
66
foreach ($paths as $path) {
77
Route::get($path.'{filename}', function ($filename) use ($path) {
8-
try {
9-
$file = \Storage::get($path . $filename);
10-
return response($file, 200)->header('Content-Type', \Storage::mimeType($file));
11-
} catch (\League\Flysystem\FileNotFoundException $e) {
12-
abort(404);
13-
}
8+
$entry = new \Unisharp\FileApi\FileApi($path);
9+
return $entry->getResponse($filename);
1410
});
1511
}
1612
}

0 commit comments

Comments
 (0)