diff --git a/app/Media/Base.php b/app/Media/Base.php index d4f7cce..953b1de 100644 --- a/app/Media/Base.php +++ b/app/Media/Base.php @@ -6,12 +6,15 @@ use Illuminate\Support\Facades\Log; abstract class Base { - protected const BLOCK_SIZE = 4096; + protected const record_size = 16384; // Limiting the maximum amount of data we read into memory /** Full path to the file */ protected string $filename; protected int $filesize; - protected string $type; + protected ?string $type; + + private mixed $fh; + private int $fp; public function __construct(string $filename,string $type) { @@ -39,4 +42,85 @@ abstract class Base throw new \Exception('Unknown key: '.$key); } } + + protected function data(int $size=4096): string + { + // Quick validation + if (($size ?: $this->size) > static::record_size) + throw new \Exception(sprintf('Refusing to read [%d] which is more than %d of data',$size ?: $this->size,self::record_size)); + + $data = ''; + if ($this->fopen()) { + while ((! is_null($read=$this->fread())) && (strlen($data) <= ($size ?: $this->size))) + $data .= $read; + + $this->fclose(); + } + + return $data; + } + + protected function fclose(): bool + { + fclose($this->fh); + unset($this->fh); + + return TRUE; + } + + /** + * Open the file and seek to the atom + * + * @return bool + */ + protected function fopen(): bool + { + $this->fh = fopen($this->filename,'r'); + fseek($this->fh,$this->offset); + $this->fp = 0; + + return TRUE; + } + + /** + * Read the atom from the file + * + * @param int $size + * @return string|NULL + */ + protected function fread(int $size=4096): ?string + { + if ($this->fp === $this->size) + return NULL; + + if ($this->fp+$size > $this->size) + $size = $this->size-$this->fp; + + $read = fread($this->fh,$size); + $this->fp += $size; + + return $read; + } + + protected function fseek(int $offset): int + { + $this->fp = $offset; + + return fseek($this->fh,$this->offset+$this->fp); + } + + protected function ftell(): int + { + return ftell($this->fh); + } + + protected function unpack(array $unpack=[]): string + { + return collect($unpack ?: static::unpack)->map(fn($v,$k)=>$v[0].$k)->join('/'); + } + + protected function unpack_size(array $unpack=[]): int + { + return collect($unpack ?: static::unpack)->map(fn($v,$k)=>$v[1])->sum(); + } } \ No newline at end of file diff --git a/app/Media/QuickTime.php b/app/Media/QuickTime.php index 4d2d9bb..d49c1b9 100644 --- a/app/Media/QuickTime.php +++ b/app/Media/QuickTime.php @@ -5,7 +5,7 @@ namespace App\Media; use Illuminate\Support\Collection; use App\Media\QuickTime\Atoms\{mdat,moov,Unknown}; -use App\Traits\FindQuicktimeAtoms; +use App\Media\QuickTime\FindQuicktimeAtoms; // https://developer.apple.com/documentation/quicktime-file-format/quicktime_movie_files diff --git a/app/Media/QuickTime/Atom.php b/app/Media/QuickTime/Atom.php index eba1181..b790f71 100644 --- a/app/Media/QuickTime/Atom.php +++ b/app/Media/QuickTime/Atom.php @@ -4,23 +4,17 @@ namespace App\Media\QuickTime; use Illuminate\Support\Collection; +use App\Media\QuickTime; use App\Media\QuickTime\Atoms\moov\{mvhd,trak}; -use App\Traits\FindQuicktimeAtoms; -abstract class Atom +abstract class Atom extends QuickTime { use FindQuicktimeAtoms; - protected const record_size = 16384; - - protected const BLOCK_SIZE = 4096; - protected int $offset; protected int $size; protected string $filename; - private mixed $fh; - private int $fp; protected Collection $cache; protected Collection $atoms; @@ -65,80 +59,4 @@ abstract class Atom throw new \Exception('Unknown key: '.$key); } } - - protected function data(): string - { - // Quick validation - if ($this->size > self::record_size) - throw new \Exception(sprintf('Refusing to read more than %d of data',self::record_size)); - - $data = ''; - if ($this->fopen()) { - while (! is_null($read=$this->fread())) - $data .= $read; - - $this->fclose(); - } - - return $data; - } - - protected function fclose(): bool - { - fclose($this->fh); - unset($this->fh); - - return TRUE; - } - - /** - * Open the file and seek to the atom - * - * @return bool - */ - protected function fopen(): bool - { - $this->fh = fopen($this->filename,'r'); - fseek($this->fh,$this->offset); - $this->fp = 0; - - return TRUE; - } - - /** - * Read the atom from the file - * - * @param int $size - * @return string|NULL - */ - protected function fread(int $size=4096): ?string - { - if ($this->fp === $this->size) - return NULL; - - if ($this->fp+$size > $this->size) - $size = $this->size-$this->fp; - - $read = fread($this->fh,$size); - $this->fp += $size; - - return $read; - } - - protected function fseek(int $offset): int - { - $this->fp = $offset; - - return fseek($this->fh,$this->offset+$this->fp); - } - - protected function unpack(array $unpack=[]): string - { - return collect($unpack ?: static::unpack)->map(fn($v,$k)=>$v[0].$k)->join('/'); - } - - protected function unpack_size(array $unpack=[]): int - { - return collect($unpack ?: static::unpack)->map(fn($v,$k)=>$v[1])->sum(); - } } \ No newline at end of file diff --git a/app/Media/QuickTime/Atoms/moov/trak/mdia/minf.php b/app/Media/QuickTime/Atoms/moov/trak/mdia/minf.php index 31bc672..23a76ca 100644 --- a/app/Media/QuickTime/Atoms/moov/trak/mdia/minf.php +++ b/app/Media/QuickTime/Atoms/moov/trak/mdia/minf.php @@ -13,7 +13,6 @@ class minf extends SubAtom private const subatom_classes = 'App\\Media\\QuickTime\\Atoms\\moov\\trak\\mdia\\minf\\'; - protected ?string $type; public function __construct(int $offset,int $size,string $filename,?string $data,string $arg=NULL) { parent::__construct($offset,$size,$filename); diff --git a/app/Traits/FindQuicktimeAtoms.php b/app/Media/QuickTime/FindQuicktimeAtoms.php similarity index 92% rename from app/Traits/FindQuicktimeAtoms.php rename to app/Media/QuickTime/FindQuicktimeAtoms.php index 626b725..b7f05ec 100644 --- a/app/Traits/FindQuicktimeAtoms.php +++ b/app/Media/QuickTime/FindQuicktimeAtoms.php @@ -1,11 +1,9 @@ = 8) { $o = class_exists($class) @@ -52,7 +50,7 @@ trait FindQuicktimeAtoms $rp += $header['size']-8; // Only need to seek if we didnt read all the data - if ((! $atom) && ($header['size'] > self::BLOCK_SIZE)) + if ((! $atom) && ($header['size']-8 > static::record_size)) fseek($fh,$offset+$rp); } else {