3
3
namespace ctf0 \MediaManager \Controllers ;
4
4
5
5
use Exception ;
6
- use ZipStream \ZipStream ;
7
6
use Illuminate \Http \Request ;
8
7
use App \Http \Controllers \Controller ;
9
8
@@ -75,9 +74,10 @@ public function get_files(Request $request)
75
74
76
75
return response ()->json ([
77
76
'locked ' => $ this ->lockedList ,
77
+ 'dirs ' => $ this ->dirsList ($ request ->dirs ),
78
78
'files ' => [
79
- 'path ' => $ folder ,
80
- 'items ' => $ this ->getData ($ folder ),
79
+ 'path ' => $ folder ,
80
+ 'items ' => $ this ->getData ($ folder ),
81
81
],
82
82
]);
83
83
}
@@ -97,9 +97,16 @@ public function get_dirs(Request $request)
97
97
$ folderLocation = rtrim (implode ('/ ' , $ folderLocation ), '/ ' );
98
98
}
99
99
100
- return response ()->json (
101
- str_replace ($ folderLocation , '' , $ this ->storageDisk ->allDirectories ($ folderLocation ))
102
- );
100
+ return response ()->json ($ this ->dirsList ($ folderLocation ));
101
+ }
102
+
103
+ protected function dirsList ($ location )
104
+ {
105
+ if (is_array ($ location )) {
106
+ $ location = rtrim (implode ('/ ' , $ location ), '/ ' );
107
+ }
108
+
109
+ return str_replace ($ location , '' , $ this ->storageDisk ->allDirectories ($ location ));
103
110
}
104
111
105
112
/**
@@ -117,12 +124,11 @@ public function upload(Request $request)
117
124
$ result = [];
118
125
119
126
foreach ($ files as $ one ) {
120
- $ file_type = $ one ->getMimeType ();
121
-
122
127
$ original = $ one ->getClientOriginalName ();
123
- $ get_name = pathinfo ($ original , PATHINFO_FILENAME );
124
- $ file_ext = pathinfo ($ original , PATHINFO_EXTENSION );
125
- $ file_name = $ random_name ? uniqid () . ". $ file_ext " : $ this ->cleanName ($ get_name , null ) . ". $ file_ext " ;
128
+ $ name_only = pathinfo ($ original , PATHINFO_FILENAME );
129
+ $ ext_only = pathinfo ($ original , PATHINFO_EXTENSION );
130
+ $ file_name = $ random_name ? uniqid () . ". $ ext_only " : $ this ->cleanName ($ name_only , null ) . ". $ ext_only " ;
131
+ $ file_type = $ one ->getMimeType ();
126
132
$ destination = "$ upload_path/ $ file_name " ;
127
133
128
134
try {
@@ -137,7 +143,7 @@ public function upload(Request $request)
137
143
}
138
144
139
145
// save file
140
- $ saved_name = $ one -> storeAs ( $ upload_path , $ file_name , $ this -> fileSystem );
146
+ $ saved_name = $ this -> storeFile ( $ one , $ upload_path , $ file_name );
141
147
142
148
// fire event
143
149
event ('MMFileUploaded ' , $ this ->getFilePath ($ saved_name ));
@@ -426,7 +432,11 @@ public function lock_file(Request $request)
426
432
}
427
433
428
434
/**
429
- * zip ops.
435
+ * zip folder.
436
+ *
437
+ * @param Request $request [description]
438
+ *
439
+ * @return [type] [description]
430
440
*/
431
441
public function folder_download (Request $ request )
432
442
{
@@ -437,6 +447,13 @@ public function folder_download(Request $request)
437
447
);
438
448
}
439
449
450
+ /**
451
+ * zip files.
452
+ *
453
+ * @param Request $request [description]
454
+ *
455
+ * @return [type] [description]
456
+ */
440
457
public function files_download (Request $ request )
441
458
{
442
459
return $ this ->download (
@@ -446,42 +463,6 @@ public function files_download(Request $request)
446
463
);
447
464
}
448
465
449
- protected function download ($ name , $ list , $ type )
450
- {
451
- // track changes
452
- $ counter = 100 / count ($ list );
453
- $ store = $ this ->cacheStore ;
454
- $ cache_name = $ name ;
455
- $ store ->forever ("$ cache_name.progress " , 0 );
456
-
457
- return response ()->stream (function () use ($ name , $ list , $ type , $ counter , $ store , $ cache_name ) {
458
- $ zip = new ZipStream ("$ name.zip " , [
459
- 'content_type ' => 'application/octet-stream ' ,
460
- ]);
461
-
462
- foreach ($ list as $ file ) {
463
- if ('folder ' == $ type ) {
464
- $ file_name = pathinfo ($ file , PATHINFO_BASENAME );
465
- $ streamRead = $ this ->storageDisk ->readStream ($ file );
466
- } else {
467
- $ file_name = $ file ['name ' ];
468
- $ streamRead = @fopen ($ file ['path ' ], 'r ' );
469
- }
470
-
471
- if ($ streamRead ) {
472
- $ store ->increment ("$ cache_name.progress " , round ($ counter , 2 ));
473
- $ zip ->addFileFromStream ($ file_name , $ streamRead );
474
- } else {
475
- $ store ->forever ("$ cache_name.abort " , $ file_name );
476
- die ();
477
- }
478
- }
479
-
480
- $ store ->forever ("$ cache_name.done " , true );
481
- $ zip ->finish ();
482
- });
483
- }
484
-
485
466
/**
486
467
* zip progress update.
487
468
*/
@@ -498,34 +479,34 @@ public function zip_progress(Request $request)
498
479
$ name = $ request ->name ;
499
480
500
481
// get changes
501
- $ store = $ this ->cacheStore ;
502
- $ cache_name = $ name ;
482
+ $ store = $ this ->cacheStore ;
503
483
504
- return response ()->stream (function () use ($ start , $ maxExecution , $ close , $ sleep , $ store , $ cache_name ) {
484
+ return response ()->stream (function () use ($ start , $ maxExecution , $ close , $ sleep , $ store , $ name ) {
505
485
while (!$ close ) {
506
486
// progress
507
- $ this ->es_msg ($ store ->get ("$ cache_name.progress " ), 'progress ' );
508
-
509
- // avoid server crash
510
- if (time () >= $ start + $ maxExecution ) {
511
- $ close = true ;
512
- $ this ->es_msg (null , 'exit ' );
513
- $ this ->clearZipCache ($ store , $ cache_name );
487
+ $ this ->SSE_msg ($ store ->get ("$ name.progress " ), 'progress ' );
488
+
489
+ // warn
490
+ if ($ store ->has ("$ name.warn " )) {
491
+ $ this ->SSE_msg (
492
+ trans ('MediaManager::messages.stream_error ' , ['attr ' => $ store ->pull ("$ name.warn " )]),
493
+ 'warn '
494
+ );
514
495
}
515
496
516
- // abort
517
- if ($ store ->has ("$ cache_name .abort " )) {
497
+ // done
498
+ if ($ store ->has ("$ name .done " )) {
518
499
$ close = true ;
519
- $ this ->es_msg ('Could not open " ' . $ store ->get ("$ cache_name.abort " ) . '" stream for reading. ' , 'abort ' );
520
- $ this ->clearZipCache ($ store , $ cache_name );
500
+ $ this ->SSE_msg (100 , 'progress ' );
501
+ $ this ->SSE_msg ('All Done ' , 'done ' );
502
+ $ this ->clearZipCache ($ store , $ name );
521
503
}
522
504
523
- // done
524
- if ($ store -> has ( " $ cache_name .done " ) ) {
505
+ // exit
506
+ if (time () >= $ start + $ maxExecution ) {
525
507
$ close = true ;
526
- $ this ->es_msg (100 , 'progress ' );
527
- $ this ->es_msg ('All Done ' , 'done ' );
528
- $ this ->clearZipCache ($ store , $ cache_name );
508
+ $ this ->SSE_msg (null , 'exit ' );
509
+ $ this ->clearZipCache ($ store , $ name );
529
510
}
530
511
531
512
ob_flush ();
@@ -538,24 +519,11 @@ public function zip_progress(Request $request)
538
519
}
539
520
}, 200 , [
540
521
'Content-Type ' => 'text/event-stream ' , // needed for SSE to work
541
- 'Cache-Control ' => 'no-cache ' , // make sure we dont cache this response
522
+ 'Cache-Control ' => 'no-cache ' , // dont cache this response
542
523
'X-Accel-Buffering ' => 'no ' , // needed for while loop to work
543
524
'Access-Control-Allow-Origin ' => config ('app.url ' ), // for cors
544
525
'Access-Control-Expose-Headers ' => '* ' , // for cors
545
526
'Access-Control-Allow-Credentials ' => true , // for cors
546
527
]);
547
528
}
548
-
549
- protected function es_msg ($ data = null , $ event = null )
550
- {
551
- echo $ event ? "event: $ event \n" : ': ' ;
552
- echo $ data ? 'data: ' . json_encode (['response ' => $ data ]) . "\n\n" : ': ' ;
553
- }
554
-
555
- protected function clearZipCache ($ store , $ item )
556
- {
557
- $ store ->forget ("$ item.progress " );
558
- $ store ->forget ("$ item.done " );
559
- $ store ->forget ("$ item.abort " );
560
- }
561
529
}
0 commit comments