|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Andaletech\Inbox\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Andaletech\Inbox\Libs\Utils; |
| 7 | +use Illuminate\Support\Facades\Auth; |
| 8 | +use Illuminate\Support\Facades\Storage; |
| 9 | +use Symfony\Component\HttpFoundation\HeaderUtils; |
| 10 | +use Andaletech\Inbox\Libs\Security\PolicyActionsConstants; |
| 11 | + |
| 12 | +class MediaController |
| 13 | +{ |
| 14 | + protected $contentMediaPolicy; |
| 15 | + |
| 16 | + public function __construct() |
| 17 | + { |
| 18 | + } |
| 19 | + #region content media |
| 20 | + |
| 21 | + /** |
| 22 | + * Handle request fo single content media |
| 23 | + * |
| 24 | + * @param Request $request |
| 25 | + * @param \Spatie\MediaLibrary\MediaCollections\Models\Media $contentMedia |
| 26 | + * @return Response |
| 27 | + */ |
| 28 | + public function getContentMedia(Request $request, $contentMedia = null) |
| 29 | + { |
| 30 | + if (empty($contentMedia)) { |
| 31 | + return response(null, 404); |
| 32 | + } |
| 33 | + if (!$this->authorizeContentMediaOperation(PolicyActionsConstants::VIEW, $contentMedia)) { |
| 34 | + return response(null, 403); |
| 35 | + } |
| 36 | + |
| 37 | + if (0 == strcasecmp($request->get('disposition'), 'download')) { |
| 38 | + return $contentMedia; |
| 39 | + } |
| 40 | + |
| 41 | + return response()->stream( |
| 42 | + function () use ($contentMedia) { |
| 43 | + echo Storage::disk($contentMedia->disk)->get(Utils::getMediaRelativePath($contentMedia, true)); |
| 44 | + // echo Storage::disk($contentMedia->disk)->get($contentMedia->getPath()); |
| 45 | + }, |
| 46 | + 200, |
| 47 | + [ |
| 48 | + 'Content-Type' => $contentMedia->mime_type, |
| 49 | + 'Content-Length' => $contentMedia->size, |
| 50 | + 'Last-Modified' => $contentMedia->updated_at->format('D, d M Y H:i:s T'), |
| 51 | + 'Content-Disposition' => HeaderUtils::DISPOSITION_INLINE, |
| 52 | + ] |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function authorizeContentMediaOperation($operation = PolicyActionsConstants::VIEW, $contentMedia = null) |
| 57 | + { |
| 58 | + $contentMediaPolicy = Utils::getContentMediaPolicy(); |
| 59 | + if ($contentMediaPolicy) { |
| 60 | + try { |
| 61 | + return $contentMediaPolicy->{$operation}(Auth::user(), $contentMedia); |
| 62 | + } catch (\Exception $ex) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + #endregion content media |
| 71 | + |
| 72 | + #region attachment |
| 73 | + |
| 74 | + /** |
| 75 | + * Handle request fo single content media |
| 76 | + * |
| 77 | + * @param Request $request |
| 78 | + * @param \Spatie\MediaLibrary\MediaCollections\Models\Media $contentMedia |
| 79 | + * @return Response |
| 80 | + */ |
| 81 | + public function getAttachment(Request $request, $attachment = null) |
| 82 | + { |
| 83 | + if (empty($attachment)) { |
| 84 | + return response(null, 404); |
| 85 | + } |
| 86 | + if (!$this->authorizeAttachmentOperation(PolicyActionsConstants::VIEW, $attachment)) { |
| 87 | + return response(null, 403); |
| 88 | + } |
| 89 | + |
| 90 | + if (0 == strcasecmp($request->get('disposition'), 'inline')) { |
| 91 | + return response()->stream( |
| 92 | + function () use ($attachment) { |
| 93 | + echo Storage::disk($attachment->disk)->get(Utils::getMediaRelativePath($attachment, true)); |
| 94 | + }, |
| 95 | + 200, |
| 96 | + [ |
| 97 | + 'Content-Type' => $attachment->mime_type, |
| 98 | + 'Content-Length' => $attachment->size, |
| 99 | + 'Last-Modified' => $attachment->updated_at->format('D, d M Y H:i:s T'), |
| 100 | + 'Content-Disposition' => HeaderUtils::DISPOSITION_INLINE, |
| 101 | + ] |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return $attachment; |
| 106 | + } |
| 107 | + |
| 108 | + public function authorizeAttachmentOperation($operation = PolicyActionsConstants::VIEW, $attachment = null) |
| 109 | + { |
| 110 | + $attachmentPolicy = Utils::getAttachmentPolicy(); |
| 111 | + if ($attachmentPolicy) { |
| 112 | + try { |
| 113 | + return $attachmentPolicy->{$operation}(Auth::user(), $attachment); |
| 114 | + } catch (\Exception $ex) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + #endregion attachment |
| 123 | +} |
0 commit comments