@@ -39,9 +39,8 @@ class FilexController extends Controller
3939 private static array $ performanceMetrics = [];
4040
4141 /**
42- * Cache size limits
42+ * Cache size limits (removed unused constant)
4343 */
44- private const MAX_CACHE_SIZE = 100 ;
4544
4645 public function __construct (FilexService $ filexService )
4746 {
@@ -168,8 +167,8 @@ protected function handleChunkedUpload(Request $request, $file): JsonResponse
168167 $ originalFileName = $ file ->getClientOriginalName ();
169168
170169 // Create temp directory for chunks with proper permissions
171- $ tempDir = 'temp/chunks/ ' . $ uuid ;
172- $ chunkPath = $ tempDir. '/chunk_ ' . $ chunkIndex ;
170+ $ tempDir = 'temp/chunks/ ' . $ uuid ;
171+ $ chunkPath = $ tempDir . '/chunk_ ' . $ chunkIndex ;
173172 $ tempDisk = $ this ->getTempDisk ();
174173
175174 try {
@@ -257,7 +256,7 @@ public function deleteTempFile(Request $request, string $filename): JsonResponse
257256 {
258257 try {
259258 // Reconstruct the temp path from the filename
260- $ tempPath = 'temp/ ' . $ filename ;
259+ $ tempPath = 'temp/ ' . $ filename ;
261260
262261 // Security check - ensure file is in temp directory and filename is valid
263262 if (
@@ -305,7 +304,7 @@ public function deleteTempFile(Request $request, string $filename): JsonResponse
305304 'timestamp ' => now ()->toISOString (),
306305 ]);
307306 } catch (\Exception $ e ) {
308- Log::error ('Temp file deletion error: ' . $ e ->getMessage (), [
307+ Log::error ('Temp file deletion error: ' . $ e ->getMessage (), [
309308 'temp_path ' => $ request ->input ('tempPath ' ),
310309 'filename ' => $ filename ,
311310 'user_id ' => Auth::check () ? Auth::id () : null ,
@@ -327,7 +326,7 @@ public function deleteTempFile(Request $request, string $filename): JsonResponse
327326 public function getTempFileInfo (Request $ request , string $ filename ): JsonResponse
328327 {
329328 // Reconstruct the temp path from the filename
330- $ tempPath = 'temp/ ' . $ filename ;
329+ $ tempPath = 'temp/ ' . $ filename ;
331330
332331 // Security check - ensure file is in temp directory and filename is valid
333332 if (
@@ -389,7 +388,7 @@ protected function mergeChunksStreaming(string $tempDir, string $finalPath, int
389388
390389 try {
391390 for ($ i = 0 ; $ i < $ totalChunks ; $ i ++) {
392- $ chunkFile = $ tempDir. '/chunk_ ' . $ i ;
391+ $ chunkFile = $ tempDir . '/chunk_ ' . $ i ;
393392
394393 if ($ tempDisk ->exists ($ chunkFile )) {
395394 $ chunkFullPath = $ tempDisk ->path ($ chunkFile );
@@ -468,7 +467,7 @@ public function getUploadConfig(): JsonResponse
468467 'max_execution_time ' => ini_get ('max_execution_time ' ),
469468 ],
470469 'app_settings ' => [
471- 'max_file_size ' => ConfigHelper::getMaxFileSize () / (1024 * 1024 ). 'MB ' ,
470+ 'max_file_size ' => ConfigHelper::getMaxFileSize () / (1024 * 1024 ) . 'MB ' ,
472471 'performance_memory_limit ' => ConfigHelper::get ('performance.memory_limit ' ),
473472 'performance_time_limit ' => ConfigHelper::get ('performance.time_limit ' ),
474473 'temp_disk ' => ConfigHelper::getTempDisk (),
@@ -606,7 +605,7 @@ public function uploadBulk(Request $request): JsonResponse
606605 ]);
607606 }
608607
609- $ successCount = count (array_filter ($ results , fn ($ r ) => $ r ['success ' ]));
608+ $ successCount = count (array_filter ($ results , fn ($ r ) => $ r ['success ' ]));
610609 $ failCount = count ($ results ) - $ successCount ;
611610
612611 PerformanceMonitor::endTimer ('bulk_upload ' , [
@@ -712,7 +711,7 @@ protected function validateBasicUploadFile($file): array
712711 public function deleteTempDirectory (Request $ request , string $ uuid ): JsonResponse
713712 {
714713 try {
715- $ tempDir = 'temp/chunks/ ' . $ uuid ;
714+ $ tempDir = 'temp/chunks/ ' . $ uuid ;
716715
717716 // Security check - ensure directory is valid
718717 if (
@@ -750,7 +749,7 @@ public function deleteTempDirectory(Request $request, string $uuid): JsonRespons
750749 'timestamp ' => now ()->toISOString (),
751750 ]);
752751 } catch (\Exception $ e ) {
753- Log::error ('Temp directory deletion error: ' . $ e ->getMessage (), [
752+ Log::error ('Temp directory deletion error: ' . $ e ->getMessage (), [
754753 'uuid ' => $ uuid ,
755754 'user_id ' => Auth::check () ? Auth::id () : null ,
756755 'exception ' => $ e ->getTraceAsString (),
@@ -942,7 +941,7 @@ protected function errorResponse(string $message, int $code = 500, ?string $erro
942941 */
943942 protected function handleUploadError (\Exception $ e , $ request ): JsonResponse
944943 {
945- Log::error ('File upload error: ' . $ e ->getMessage (), [
944+ Log::error ('File upload error: ' . $ e ->getMessage (), [
946945 'exception ' => $ e ,
947946 'request ' => $ request ->all (),
948947 'user_id ' => Auth::id (),
@@ -1044,14 +1043,16 @@ protected function streamChunkWithMonitoring(
10441043 $ lastProgressUpdate = microtime (true );
10451044 $ progressInterval = 0.5 ; // Update progress every 0.5 seconds
10461045
1047- $ source = fopen ($ sourcePath , 'rb ' );
1048- $ target = fopen ($ targetPath , 'wb ' );
1049-
1050- if (! $ source || ! $ target ) {
1051- throw new \RuntimeException ('Could not open files for streaming ' );
1052- }
1046+ $ source = null ;
1047+ $ target = null ;
10531048
10541049 try {
1050+ $ source = fopen ($ sourcePath , 'rb ' );
1051+ $ target = fopen ($ targetPath , 'wb ' );
1052+
1053+ if ($ source === false || $ target === false ) {
1054+ throw new \RuntimeException ('Could not open files for streaming ' );
1055+ }
10551056 while (! feof ($ source )) {
10561057 $ buffer = fread ($ source , $ bufferSize );
10571058 if ($ buffer === false ) {
@@ -1078,10 +1079,10 @@ protected function streamChunkWithMonitoring(
10781079 }
10791080 }
10801081 } finally {
1081- if ($ source ) {
1082+ if ($ source && is_resource ( $ source ) ) {
10821083 fclose ($ source );
10831084 }
1084- if ($ target ) {
1085+ if ($ target && is_resource ( $ target ) ) {
10851086 fclose ($ target );
10861087 }
10871088 }
@@ -1147,6 +1148,7 @@ public function getPerformanceMetrics(): JsonResponse
11471148 'peak ' => ByteHelper::formatBytes (memory_get_peak_usage (true )),
11481149 'limit ' => ByteHelper::formatBytes ($ this ->getMemoryLimit ()),
11491150 ],
1151+ 'performance_data ' => self ::$ performanceMetrics ,
11501152 'cache_status ' => [
11511153 'upload_limits_cached ' => self ::$ uploadLimits !== null ,
11521154 'disk_info_cached ' => self ::$ diskInfo !== null ,
@@ -1198,7 +1200,7 @@ private function updateUploadProgress(int $bytesWritten, int $totalSize): void
11981200 ];
11991201
12001202 Cache::put (
1201- 'upload_progress_ ' . request ()->input ('dzuuid ' ),
1203+ 'upload_progress_ ' . request ()->input ('dzuuid ' ),
12021204 $ progress ,
12031205 now ()->addMinutes (5 )
12041206 );
@@ -1235,9 +1237,9 @@ private function logUploadMetrics(
12351237 'chunk_index ' => $ chunkIndex + 1 ,
12361238 'total_chunks ' => $ totalChunks ,
12371239 'size ' => ByteHelper::formatBytes ($ size ),
1238- 'duration ' => $ duration. 's ' ,
1240+ 'duration ' => $ duration . 's ' ,
12391241 'memory_used ' => ByteHelper::formatBytes ($ memoryUsed ),
1240- 'throughput ' => ByteHelper::formatBytes ((int ) ($ size / $ duration )). '/s ' ,
1242+ 'throughput ' => ByteHelper::formatBytes ((int ) ($ size / $ duration )) . '/s ' ,
12411243 ]);
12421244 }
12431245
@@ -1247,7 +1249,7 @@ private function logUploadMetrics(
12471249 private function getUploadedChunksCount (string $ tempDir ): int
12481250 {
12491251 return collect ($ this ->getTempDisk ()->files ($ tempDir ))
1250- ->filter (fn ($ file ) => str_contains ($ file , 'chunk_ ' ))
1252+ ->filter (fn ($ file ) => str_contains ($ file , 'chunk_ ' ))
12511253 ->count ();
12521254 }
12531255
@@ -1266,7 +1268,7 @@ private function finalizeChunkedUpload(
12661268
12671269 // Generate final filename
12681270 $ finalFileName = $ this ->filexService ->generateFileName ($ originalFileName );
1269- $ finalPath = 'temp/ ' . $ finalFileName ;
1271+ $ finalPath = 'temp/ ' . $ finalFileName ;
12701272
12711273 // Merge chunks with optimized streaming
12721274 $ this ->mergeChunksStreaming ($ tempDir , $ finalPath , $ totalChunks );
@@ -1319,9 +1321,7 @@ private function finalizeChunkedUpload(
13191321 } catch (\Exception $ e ) {
13201322 // Clean up on error
13211323 $ this ->getTempDisk ()->deleteDirectory ($ tempDir );
1322- if (isset ($ finalPath )) {
1323- $ this ->getTempDisk ()->delete ($ finalPath );
1324- }
1324+ $ this ->getTempDisk ()->delete ($ finalPath );
13251325
13261326 Log::error ('Chunk merge error ' , [
13271327 'error ' => $ e ->getMessage (),
0 commit comments