28
28
use Vvveb \System \Core \View ;
29
29
30
30
class Media extends Base {
31
- protected $ uploadDenyExtensions = ['php ' ];
31
+ protected $ uploadDenyExtensions = ['php ' , ' svg ' , ' js ' ];
32
32
33
33
//protected $uploadAllowExtensions = ['ico','jpg','jpeg','png','gif','webp', 'mp4', 'mkv', 'mov'];
34
34
@@ -68,48 +68,88 @@ function index() {
68
68
69
69
function upload () {
70
70
$ path = sanitizeFileName ($ this ->request ->post ['mediaPath ' ]);
71
- $ file = $ this ->request ->files ['file ' ];
71
+ $ file = $ this ->request ->files ['file ' ] ?? [] ;
72
72
$ fileName = sanitizeFileName ($ file ['name ' ]);
73
73
$ path = preg_replace ('@^/public/media|^/media|^/public@ ' , '' , $ path );
74
74
$ extension = strtolower (substr ($ fileName , strrpos ($ fileName , '. ' ) + 1 ));
75
+ $ success = false ;
76
+ $ return = '' ;
77
+ $ message = '' ;
78
+
79
+ if ($ file ) {
80
+ switch ($ file ['error ' ]) {
81
+ case UPLOAD_ERR_OK :
82
+ $ success = true ;
83
+ break ;
84
+
85
+ case UPLOAD_ERR_NO_FILE :
86
+ $ message = __ ('No file sent ' );
87
+ break ;
88
+
89
+ case UPLOAD_ERR_PARTIAL :
90
+ $ message = __ ('The uploaded file was only partially uploaded ' );
91
+ break ;
92
+
93
+ case UPLOAD_ERR_NO_TMP_DIR :
94
+ $ message = __ ('Missing a temporary folder ' );
95
+ break ;
96
+
97
+ case UPLOAD_ERR_CANT_WRITE :
98
+ $ message = __ ('Failed to write file to disk ' );
99
+ break ;
100
+
101
+ case UPLOAD_ERR_EXTENSION :
102
+ $ message = __ ('A PHP extension stopped the file upload ' );
103
+ break ;
104
+
105
+ case UPLOAD_ERR_INI_SIZE :
106
+ case UPLOAD_ERR_FORM_SIZE :
107
+ $ message = __ ('Exceeded filesize limit ' );
108
+ break ;
109
+
110
+ default :
111
+ $ message = __ ('Unknown errors ' );
112
+ }
113
+
114
+ if (in_array ($ extension , $ this ->uploadDenyExtensions )) {
115
+ $ message = __ ('File type not allowed! ' );
116
+ $ success = false ;
117
+ }
75
118
76
- if (in_array ($ extension , $ this ->uploadDenyExtensions )) {
77
- die (__ ('File type not allowed! ' ));
78
- }
79
-
80
- switch ($ file ['error ' ]) {
81
- case UPLOAD_ERR_OK :
82
- break ;
83
-
84
- case UPLOAD_ERR_NO_FILE :
85
- die (__ ('No file sent. ' ));
86
-
87
- case UPLOAD_ERR_INI_SIZE :
88
- case UPLOAD_ERR_FORM_SIZE :
89
- die (__ ('Exceeded filesize limit. ' ));
90
-
91
- default :
92
- die (__ ('Unknown errors. ' ));
93
- }
94
-
95
- $ origFilename = $ fileName ;
96
- $ i = 1 ;
97
-
98
- while (file_exists ($ destination = DIR_MEDIA . $ path . DS . $ fileName ) && ($ i ++ < 5 )) {
99
- $ fileName = rand (0 , 10000 ) . '- ' . $ origFilename ;
100
- }
119
+ $ origFilename = $ fileName ;
120
+ $ i = 1 ;
121
+
122
+ if ($ success ) {
123
+ while (file_exists ($ destination = DIR_MEDIA . $ path . DS . $ fileName ) && ($ i ++ < 5 )) {
124
+ $ fileName = rand (0 , 10000 ) . '- ' . $ origFilename ;
125
+ }
101
126
102
- if (move_uploaded_file ($ file ['tmp_name ' ], $ destination )) {
103
- if (isset ($ this ->request ->post ['onlyFilename ' ])) {
104
- echo $ fileName ;
105
- } else {
106
- echo $ destination ;
127
+ if (move_uploaded_file ($ file ['tmp_name ' ], $ destination )) {
128
+ if (isset ($ this ->request ->post ['onlyFilename ' ])) {
129
+ $ return = $ fileName ;
130
+ } else {
131
+ $ return = $ destination ;
132
+ }
133
+ $ message = __ ('File uploaded successfully! ' );
134
+ } else {
135
+ $ destination = DIR_MEDIA . $ path . DS ;
136
+ $ success = false ;
137
+
138
+ if (!is_writable ($ destination )) {
139
+ $ message = sprintf (__ ('%s not writable! ' ), $ destination );
140
+ } else {
141
+ $ message = __ ('Error moving uploaded file! ' );
142
+ }
143
+ }
107
144
}
108
145
} else {
109
- echo __ ('Error uploading file ! ' );
146
+ $ message = __ ('Invalid upload ! ' );
110
147
}
111
-
112
- die ();
148
+
149
+ $ message = ['success ' => true , 'message ' => $ message , 'file ' => $ return ];
150
+
151
+ $ this ->response ->setType ('json ' );
152
+ $ this ->response ->output ($ message );
113
153
}
114
154
115
155
function delete () {
@@ -124,9 +164,8 @@ function delete() {
124
164
$ message = ['success ' => false , 'message ' => __ ('Error deleting file! ' )];
125
165
}
126
166
127
- echo json_encode ($ message );
128
-
129
- die ();
167
+ $ this ->response ->setType ('json ' );
168
+ $ this ->response ->output ($ message );
130
169
}
131
170
132
171
function rename () {
@@ -153,9 +192,9 @@ function rename() {
153
192
}
154
193
}
155
194
156
- echo json_encode ($ message );
157
195
158
- die ();
196
+ $ this ->response ->setType ('json ' );
197
+ $ this ->response ->output ($ message );
159
198
}
160
199
161
200
function scan () {
@@ -217,15 +256,5 @@ function scan() {
217
256
'path ' => '' ,
218
257
'items ' => $ response ,
219
258
]);
220
- /*
221
- $view = View::getInstance();
222
- $view->set([
223
- 'name' => '',
224
- 'type' => 'folder',
225
- 'path' => '',
226
- 'items' => $response,
227
- ]);
228
- */
229
- return ;
230
259
}
231
260
}
0 commit comments