diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 779415b..fdecf4d 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\AddPost; use App\Post; use App\PostsTag; use App\Services\ImageService; @@ -11,6 +12,9 @@ use DateTime; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Redirect; +use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist; +use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist; +use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig; class DashboardController extends Controller { @@ -72,39 +76,35 @@ class DashboardController extends Controller /** * Edit today's entry + * @param AddPost $request + * @param TagDetectorService $tag_detector + * @return + * @throws \Exception */ - public function edit(TagDetectorService $tag_detector) + public function edit(AddPost $request, TagDetectorService $tag_detector) { /** @var Post $today_post */ $today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d')) ->where('user_id', Auth::user()->getAuthIdentifier()) ->firstOrFail(); - $data = request()->validate([ - 'message' => 'required' - ]); + $data = $request->validated(); - $today_post->deleteTags(); - - $tags = $tag_detector->detectFrom($data['message']); - - $today_post->content = $data['message']; - $today_post->save(); - $all_tags = Tag::all(); - $all_tags_names = []; - foreach ($all_tags as $tag) { - $all_tags_names[$tag->id] = $tag->name; - } - - foreach ($tags as $tag) { - if (in_array($tag, $all_tags_names)) { - PostsTag::create([ - 'post_id' => $today_post->id, - 'tag_id' => array_search($tag, $all_tags_names), - ]); + if (!empty($validated_data['file'])) { + try { + $today_post->addMediaFromRequest('file')->toMediaCollection('post_image'); + } catch (DiskDoesNotExist $e) { + } catch (FileDoesNotExist $e) { + } catch (FileIsTooBig $e) { } } + $today_post->deleteTags(); + $today_post->content = $data['message']; + $today_post->save(); + + $this->setPostsTagsForPost($today_post, $tag_detector->detectFrom($data['message'])); + return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.'); } @@ -127,37 +127,42 @@ class DashboardController extends Controller } /** + * @param AddPost $request * @param TagDetectorService $tag_detector * @return mixed * @throws \Exception */ - public function store(TagDetectorService $tag_detector, ImageService $image_service) + public function store(AddPost $request, TagDetectorService $tag_detector) { - $user_id = Auth::user()->getAuthIdentifier(); + $validated_data = $request->validated(); - $validated_data = request()->validate([ - 'message' => 'required', - 'file' => 'file|mimes:jpeg,png,jpg,gif,svg', - ]); - - $data = [ - 'user_id' => $user_id, + /** @var Post $post */ + $post = Post::create([ + 'user_id' => Auth::user()->getAuthIdentifier(), 'date_post' => new DateTime(), 'content' => $validated_data['message'], - ]; + ]); if (!empty($validated_data['file'])) { - /** @var UploadedFile $uploaded_document */ - $uploaded_document = $validated_data['file']; - $file_name = $uploaded_document->getClientOriginalName(); - $uploaded_document->storeAs(Auth::user()->getFolder(), $file_name); - $image_service->makeThumbnail($uploaded_document->getRealPath(), ['width' => 300]); - $data['image'] = $file_name; + try { + $post->addMediaFromRequest('file')->toMediaCollection('post_image'); + } catch (DiskDoesNotExist $e) { + } catch (FileDoesNotExist $e) { + } catch (FileIsTooBig $e) { + } } - $tags = $tag_detector->detectFrom($data['content']); + $this->setPostsTagsForPost($post, $tag_detector->detectFrom($validated_data['message'])); - $check = Post::create($data); + return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.'); + } + + /** + * @param Post $post + * @param $tags + */ + private function setPostsTagsForPost(Post $post, $tags) + { $all_tags = Tag::all(); $all_tags_names = []; foreach ($all_tags as $tag) { @@ -165,14 +170,12 @@ class DashboardController extends Controller } foreach ($tags as $tag) { - if (in_array($tag, $all_tags_names)) { + if (in_array($tag, $all_tags_names, true)) { PostsTag::create([ - 'post_id' => $check->id, + 'post_id' => $post->id, 'tag_id' => array_search($tag, $all_tags_names), ]); } } - - return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.'); } } diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index 6db93c4..14a8021 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -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; } /** diff --git a/app/Http/Requests/AddPost.php b/app/Http/Requests/AddPost.php new file mode 100644 index 0000000..6cfe5cb --- /dev/null +++ b/app/Http/Requests/AddPost.php @@ -0,0 +1,32 @@ + 'required', + 'file' => 'file|mimes:jpeg,png,jpg,gif,svg', + ]; + } +} diff --git a/app/Post.php b/app/Post.php index 03b790b..a5d4495 100644 --- a/app/Post.php +++ b/app/Post.php @@ -3,9 +3,15 @@ namespace App; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Log; +use Spatie\Image\Exceptions\InvalidManipulation; +use Spatie\MediaLibrary\HasMedia\HasMedia; +use Spatie\MediaLibrary\HasMedia\HasMediaTrait; +use Spatie\MediaLibrary\Models\Media; -class Post extends Model +class Post extends Model implements HasMedia { + use HasMediaTrait; /** * The attributes that are mass assignable. * @@ -52,4 +58,18 @@ class Post extends Model ->orderBy('date_post') ->get(); } + + /** + * @param Media|null $media + */ + public function registerMediaConversions(Media $media = null) + { + try { + $this->addMediaConversion('list') + ->width(300) + ->optimize(); + } catch (InvalidManipulation $e) { + Log::alert(sprintf('Error while manipulating Post Image for %s (%s)', $this->id, $e->getMessage())); + } + } } diff --git a/app/User.php b/app/User.php index 5425326..875f044 100644 --- a/app/User.php +++ b/app/User.php @@ -72,13 +72,4 @@ class User extends Authenticatable implements HasMedia Log::alert(sprintf('Error while manipulating Avatar for %s (%s)', $this->email, $e->getMessage())); } } - - /** - * @param string $alias - * @return string - */ - public function getAvatar($alias = 'thumb'): string - { - return $this->getFirstMediaUrl('avatars', $alias); - } } diff --git a/resources/views/gallery/my.blade.php b/resources/views/gallery/my.blade.php index fe22ae5..a223216 100644 --- a/resources/views/gallery/my.blade.php +++ b/resources/views/gallery/my.blade.php @@ -21,7 +21,7 @@

- +

diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index f7b2b3f..0a853ea 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -126,8 +126,7 @@
- @if($post->image != '')

@endif -

{!! $tag_detector->linkTagFrom($post->content) !!}

+ @if($post->hasMedia('post_image'))

@endif

{!! $tag_detector->linkTagFrom($post->content) !!}