Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
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 / 48
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
110
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            $current_subscription = $this->getCurrentPlan($request->user());
21            $features = $current_subscription->features;
22
23            $image_extensions = ['tif', 'tiff', 'png', 'PNG', 'jpeg', 'JPEG', 'bmp', 'gif'];
24
25            $can_upload = $features['Rich Text Editor'] ?? null;
26
27            if (! $can_upload) {
28                $response =  [
29                    'error' => true,
30                    'message' => 'You are not authorized to upload any file with current plan'
31                ];       
32                
33                return response()->json($response, 422);
34
35                //abort(403, 'You are not authorized to upload any file with current plan');
36            }
37
38            if ($can_upload == 'Picture Only' && ! in_array($extension, $image_extensions)) {
39                $response =  [
40                    'error' => true,
41                    'message' => 'You are not authorized to upload '.$extension
42                ];       
43                
44                return response()->json($response, 422);
45
46                //abort(403, 'You are not authorized to upload '.$extension);
47            }
48
49            //** Limit Image Upload Size */
50
51            $image_upload_size = $features['Image Upload Size'];
52
53            if (! $image_upload_size) {
54                $response =  [
55                    'error' => true,
56                    'message' => 'You are not authorized to upload images with current plan'
57                ];       
58                
59                return response()->json($response, 422);
60
61                //abort(403, 'You are not authorized to upload images with current plan');
62            }
63
64            $file_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->file));
65            $file_size = strlen($file_data);
66
67            $size_in_mb = $file_size / 1000000;
68
69            if ($size_in_mb > $image_upload_size) {
70                $response =  [
71                    'error' => true,
72                    'message' => "You are not authorized to upload images greater than $image_upload_size MB with current plan"
73                ];       
74                
75                return response()->json($response, 422);
76
77                //abort(403, "You are not authorized to upload images greater than $image_upload_size MB with current plan");
78            }
79
80            $media_storage = $features['Media Storage'];
81
82            if (! $media_storage) {
83                $response =  [
84                    'error' => true,
85                    'message' => 'You have reached the maximum limit to upload media with current plan'
86                ];       
87                
88                return response()->json($response, 422);
89
90                //abort(403, 'You have reached the maximum limit to upload media with current plan');
91            }
92
93            $current_media_storage = $this->getMediaStorage($request->user());
94
95            if (($current_media_storage + $file_size) / 1000000 > $media_storage) {
96                $response =  [
97                    'error' => true,
98                    'message' => "You have reached the maximum storage limit of {$media_storage} MB with current plan"
99                ];       
100                
101                return response()->json($response, 422);
102
103                //abort(403, "You have reached the maximum storage limit of {$media_storage} MB with current plan");
104            }
105        }
106
107        return $next($request);
108    }
109}