33namespace Portavice \LaravelFileSanitizer ;
44
55use Illuminate \Http \UploadedFile ;
6+ use Illuminate \Support \Facades \Storage ;
7+ use RuntimeException ;
68use SytxLabs \FileSanitizer \FileSanitizer as BaseFileSanitizer ;
79
810class FileSanitizerManager
@@ -16,14 +18,47 @@ public function getSanitizer(): BaseFileSanitizer
1618 return $ this ->sanitizer ;
1719 }
1820
19- public function process (string $ inputPath , ?string $ outputPath = null , ?bool $ sanitizeAlways = null ): array
21+ public function process (string $ inputPath , ?string $ outputPath = null , ?bool $ sanitizeAlways = null , ? string $ diskName = null ): array
2022 {
21- return $ this ->sanitizer -> process ($ inputPath , $ outputPath , $ sanitizeAlways ?? (bool ) ($ this ->config ['sanitize_always ' ] ?? false ));
23+ return $ this ->run ($ inputPath , $ outputPath , $ sanitizeAlways ?? (bool ) ($ this ->config ['sanitize_always ' ] ?? false ), $ diskName );
2224 }
2325
24- public function sanitizeAlways (string $ inputPath , ?string $ outputPath = null ): array
26+ public function sanitizeAlways (string $ inputPath , ?string $ outputPath = null , ? string $ diskName = null ): array
2527 {
26- return method_exists ($ this ->sanitizer , 'sanitizeAlways ' ) ? $ this ->sanitizer ->sanitizeAlways ($ inputPath , $ outputPath ) : $ this ->sanitizer ->process ($ inputPath , $ outputPath , true );
28+ return $ this ->run ($ inputPath , $ outputPath , true , $ diskName );
29+ }
30+
31+ protected function run (string $ inputPath , ?string $ outputPath , bool $ sanitizeAlways , ?string $ diskName ): array
32+ {
33+ if ($ diskName === null ) {
34+ return $ sanitizeAlways && method_exists ($ this ->sanitizer , 'sanitizeAlways ' ) ? $ this ->sanitizer ->sanitizeAlways ($ inputPath , $ outputPath ) : $ this ->sanitizer ->process ($ inputPath , $ outputPath , $ sanitizeAlways );
35+ }
36+ $ disk = Storage::disk ($ diskName );
37+ if (! $ disk ->exists ($ inputPath )) {
38+ throw new RuntimeException ("Input file [ {$ inputPath }] was not found on disk [ {$ diskName }]. " );
39+ }
40+ $ tmpInput = tempnam (sys_get_temp_dir (), 'fs_in_ ' );
41+ $ tmpOutput = tempnam (sys_get_temp_dir (), 'fs_out_ ' );
42+ if ($ tmpInput === false || $ tmpOutput === false ) {
43+ if ($ tmpInput !== false ) {
44+ @unlink ($ tmpInput );
45+ }
46+ if ($ tmpOutput !== false ) {
47+ @unlink ($ tmpOutput );
48+ }
49+ throw new RuntimeException ('Unable to create temporary files. ' );
50+ }
51+ try {
52+ file_put_contents ($ tmpInput , $ disk ->get ($ inputPath ));
53+ $ result = $ sanitizeAlways && method_exists ($ this ->sanitizer , 'sanitizeAlways ' ) ? $ this ->sanitizer ->sanitizeAlways ($ tmpInput , $ tmpOutput ) : $ this ->sanitizer ->process ($ tmpInput , $ tmpOutput , $ sanitizeAlways );
54+ if ($ outputPath !== null && is_file ($ tmpOutput )) {
55+ $ disk ->put ($ outputPath , file_get_contents ($ tmpOutput ));
56+ }
57+ return $ result ;
58+ } finally {
59+ @unlink ($ tmpInput );
60+ @unlink ($ tmpOutput );
61+ }
2762 }
2863
2964 public function processUploadedFile (UploadedFile $ file , ?string $ outputPath = null , ?bool $ sanitizeAlways = null ): array
0 commit comments