6
6
use App \Models \Link ;
7
7
use App \Models \Button ;
8
8
use Illuminate \Support \Facades \Route ;
9
+ use Illuminate \Support \Facades \File ;
9
10
10
11
class LinkTypeViewController extends Controller
11
12
{
@@ -55,28 +56,47 @@ public function getParamForm($typename, $linkId = 0)
55
56
public function blockAsset (Request $ request , $ type )
56
57
{
57
58
$ asset = $ request ->query ('asset ' );
58
-
59
+
59
60
// Prevent directory traversal in $type
60
61
if (preg_match ('/\.\.|\/| \\\\/ ' , $ type )) {
61
62
abort (403 , 'Unauthorized action. ' );
62
63
}
63
-
64
+
64
65
// Define allowed file extensions
65
66
$ allowedExtensions = ['js ' , 'css ' , 'img ' , 'svg ' , 'gif ' , 'jpg ' , 'jpeg ' , 'png ' , 'mp4 ' , 'mp3 ' ];
66
-
67
+
67
68
$ extension = strtolower (pathinfo ($ asset , PATHINFO_EXTENSION ));
68
69
if (!in_array ($ extension , $ allowedExtensions )) {
69
70
return response ('File type not allowed ' , Response::HTTP_FORBIDDEN );
70
71
}
71
-
72
+
72
73
$ basePath = realpath (base_path ("blocks/ $ type " ));
73
-
74
+
74
75
$ fullPath = realpath (base_path ("blocks/ $ type/ $ asset " ));
75
-
76
+
76
77
if (!$ fullPath || !file_exists ($ fullPath ) || strpos ($ fullPath , $ basePath ) !== 0 ) {
77
78
return response ('File not found ' , Response::HTTP_NOT_FOUND );
78
79
}
79
-
80
- return response ()->file ($ fullPath );
80
+
81
+ // Map file extensions to MIME types
82
+ $ mimeTypes = [
83
+ 'js ' => 'application/javascript ' ,
84
+ 'css ' => 'text/css ' ,
85
+ 'img ' => 'image/png ' ,
86
+ 'svg ' => 'image/svg+xml ' ,
87
+ 'gif ' => 'image/gif ' ,
88
+ 'jpg ' => 'image/jpeg ' ,
89
+ 'jpeg ' => 'image/jpeg ' ,
90
+ 'png ' => 'image/png ' ,
91
+ 'mp4 ' => 'video/mp4 ' ,
92
+ 'mp3 ' => 'audio/mpeg ' ,
93
+ ];
94
+
95
+ // Determine the MIME type using the mapping
96
+ $ mimeType = $ mimeTypes [$ extension ] ?? 'application/octet-stream ' ;
97
+
98
+ return response ()->file ($ fullPath , [
99
+ 'Content-Type ' => $ mimeType
100
+ ]);
81
101
}
82
102
}
0 commit comments