| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Hyde\Support\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use function extension_loaded; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function file_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function filesize; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Hyde\Framework\Exceptions\FileNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function is_file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function pathinfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * File abstraction for a project media file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class MediaFile extends ProjectFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         return array_merge(parent::toArray(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'length' => $this->getContentLength(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'mimeType' => $this->getMimeType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function getContentLength(): int | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         if (! is_file($this->path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             throw new FileNotFoundException(message: "Could not get the content length of file '$this->path'"); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         return filesize($this->getAbsolutePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function getMimeType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // See if we can find a mime type for the extension instead of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // having to rely on a PHP extension and filesystem lookups. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $lookup = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'txt'  => 'text/plain', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             'md'   => 'text/markdown', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'html' => 'text/html', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'css'  => 'text/css', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'svg'  => 'image/svg+xml', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'png'  => 'image/png', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'jpg'  => 'image/jpeg', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'jpeg' => 'image/jpeg', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'gif'  => 'image/gif', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'json' => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'js'   => 'application/javascript', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'xml'  => 'application/xml', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (isset($lookup[$extension])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return $lookup[$extension]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (extension_loaded('fileinfo') && file_exists($this->getAbsolutePath())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return mime_content_type($this->getAbsolutePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return 'text/plain'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  |