🚚 Passe toutes les images sous MediaLibrary

Fix #14
This commit is contained in:
2020-03-19 12:11:21 +01:00
parent 9ec28afa4a
commit 3ab7e495e9
10 changed files with 131 additions and 73 deletions

View File

@@ -4,10 +4,11 @@ namespace App\Http\Controllers;
use App\Post;
use App\Services\ImageService;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Intervention\Image\Constraint;
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;
@@ -15,28 +16,40 @@ class ImageController extends Controller
{
/**
* @param int $post_id
* @param string|array $options
* @param string $alias
* @param string $image
* @param ImageService $image_service
*
* @return mixed
*/
public function display($post_id, $options = 'o:full', $image, ImageService $image_service)
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.');
}
$original = sprintf('%s/%s/%s', storage_path('app'), Auth::user()->getFolder(), $image);
if ('o:full' === $options) {
$img = $image_service->makeThumbnail($original);
} else {
$img = $image_service->makeThumbnail($original, ['width' => 300]);
$first_media = $post->getFirstMedia('post_image');
if (!$first_media instanceof Media) {
throw new NotFoundHttpException('Média non trouvé en base.');
}
return $img->response(File::extension($original));
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;
}
/**