2020-03-10 13:09:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Post;
|
2020-03-12 17:31:51 +01:00
|
|
|
use App\Services\ImageService;
|
2020-03-19 12:11:21 +01:00
|
|
|
use App\User;
|
2020-03-10 13:09:05 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Intervention\Image\Facades\Image;
|
2020-03-19 12:11:21 +01:00
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Spatie\MediaLibrary\Models\Media;
|
2020-03-10 13:09:05 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
|
|
|
|
|
|
|
class ImageController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param int $post_id
|
2020-03-19 12:11:21 +01:00
|
|
|
* @param string $alias
|
2020-03-10 13:09:05 +01:00
|
|
|
* @param string $image
|
2020-03-12 17:31:51 +01:00
|
|
|
* @param ImageService $image_service
|
2020-03-10 13:09:05 +01:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-03-19 12:11:21 +01:00
|
|
|
public function display($post_id, $alias = '')
|
2020-03-10 13:09:05 +01:00
|
|
|
{
|
2020-03-19 12:11:21 +01:00
|
|
|
/** @var Post $post */
|
2020-03-10 13:09:05 +01:00
|
|
|
$post = Post::find($post_id);
|
|
|
|
|
|
|
|
if (Auth::user()->getAuthIdentifier() !== (int)$post->user_id) {
|
|
|
|
throw new UnauthorizedHttpException('Cette image ne vous appartient pas.');
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
$first_media = $post->getFirstMedia('post_image');
|
|
|
|
if (!$first_media instanceof Media) {
|
|
|
|
throw new NotFoundHttpException('Média non trouvé en base.');
|
2020-03-10 13:09:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 12:11:21 +01:00
|
|
|
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;
|
2020-03-10 13:09:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2020-03-10 13:09:05 +01:00
|
|
|
|
|
|
|
return view('gallery.my', [
|
|
|
|
'posts' => $posts_with_image,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|