Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a controller to manage media over API
- Loading branch information
1 parent
0da3256
commit 8cd4ff6
Showing
5 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\MediaLibrary; | ||
use App\Media; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\Media as MediaResource; | ||
use Illuminate\Http\Request; | ||
use App\Http\Requests\Admin\MediaLibraryRequest; | ||
|
||
class MediaController extends Controller | ||
{ | ||
/** | ||
* Return the comments. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Request $request) | ||
{ | ||
return MediaResource::collection( | ||
MediaLibrary::first()->media()->paginate($request->input('limit', 20)) | ||
); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param MediaLibraryRequest $request | ||
* @return Response | ||
*/ | ||
public function store(MediaLibraryRequest $request) | ||
{ | ||
$this->authorize('store', Media::class); | ||
|
||
$image = $request->file('image'); | ||
$name = $image->getClientOriginalName(); | ||
|
||
if ($request->filled('name')) { | ||
$name = $request->input('name'); | ||
} | ||
|
||
return new MediaResource( | ||
MediaLibrary::first() | ||
->addMedia($image) | ||
->usingName($name) | ||
->toMediaCollection() | ||
); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param Media $medium | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Media $medium) | ||
{ | ||
$this->authorize('delete', $medium); | ||
|
||
$medium->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\Resource; | ||
|
||
class Media extends Resource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
'url' => url($this->getUrl()), | ||
'thumb_url' => url($this->getUrl('thumb')), | ||
]; | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Media; | ||
use App\User; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class MediaPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
/** | ||
* Determine whether the user is admin for all authorization. | ||
*/ | ||
public function before(User $user) | ||
{ | ||
if ($user->isAdmin()) { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Determine whether the user can store a medium. | ||
* | ||
* @param User $user | ||
* @return bool | ||
*/ | ||
public function store(User $user): bool | ||
{ | ||
return $user->isAdmin(); | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the medium. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Media $medium | ||
* @return mixed | ||
*/ | ||
public function delete(User $user, Media $medium) | ||
{ | ||
return $user->isAdmin(); | ||
} | ||
} |
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