journal-intime/app/Http/Controllers/ImageController.php

70 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Post;
use App\Services\ImageService;
use App\User;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\File;
use Spatie\MediaLibrary\Models\Media;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class ImageController extends Controller
{
/**
* @param int $post_id
* @param string $alias
* @param string $image
* @param ImageService $image_service
*
* @return mixed
*/
public function display($post_id, $alias = '')
{
/** @var Post $post */
$post = Post::find($post_id);
if (Auth::user()->getAuthIdentifier() !== (int)$post->user_id) {
throw new UnauthorizedHttpException('Cette image ne vous appartient pas.');
}
$first_media = $post->getFirstMedia('post_image');
if (!$first_media instanceof Media) {
throw new NotFoundHttpException('Média non trouvé en base.');
}
return Image::make($first_media->getPath($alias))->response($first_media->mime_type);
}
/**
* @param string $alias
* @return mixed
*/
public function avatar($alias = '')
{
/** @var User $user */
$user = User::find(Auth::user()->getAuthIdentifier());
readfile($user->getFirstMediaPath('avatars', $alias));
exit;
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function gallery()
{
$posts_with_image = Post::where('user_id', Auth::user()->getAuthIdentifier())
->where('image', '!=', 'null')
->orderBy('created_at', 'desc')
2020-03-17 09:52:23 +01:00
->paginate(9);
return view('gallery.my', [
'posts' => $posts_with_image,
]);
}
}