Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CanUploadMiddleware
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Http\Middleware\Subscriptions;
4
5use App\Traits\SubscriptionTrait;
6use Closure;
7use Illuminate\Http\Request;
8use Symfony\Component\HttpFoundation\Response;
9
10class CanUploadMiddleware
11{
12    use SubscriptionTrait;
13
14    /**
15     * Handle an incoming request.
16     */
17    public function handle(Request $request, Closure $next): Response
18    {
19        if ($request->file && $extension = $request->extension) {
20            $currentPlan = $this->getCurrentPlan($request->user());
21
22            // Limit Image Upload Size
23            $imageUploadSize = $this->getFeatureByNewKey($currentPlan, 'storage_limits', 0);
24
25            if (! $imageUploadSize) {
26                return response()->json([
27                    'error' => true,
28                    'message' => 'You are not authorized to upload images with current plan',
29                ], 422);
30            }
31
32            $fileData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->file));
33            $fileSize = strlen($fileData);
34
35            $sizeInMb = $fileSize / 1000000;
36
37            if ($sizeInMb > $imageUploadSize) {
38                return response()->json([
39                    'error' => true,
40                    'message' => "You are not authorized to upload images greater than {$imageUploadSize} MB with current plan",
41                ], 422);
42            }
43        }
44
45        return $next($request);
46    }
47}