Move some file read operations to base class, moved Quicktime Trait to be with Quicktime classes

This commit is contained in:
Deon George 2024-09-20 12:56:20 +10:00
parent f14e29a54f
commit 286746019f
5 changed files with 92 additions and 93 deletions

View File

@ -6,12 +6,15 @@ use Illuminate\Support\Facades\Log;
abstract class Base 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 */ /** Full path to the file */
protected string $filename; protected string $filename;
protected int $filesize; protected int $filesize;
protected string $type; protected ?string $type;
private mixed $fh;
private int $fp;
public function __construct(string $filename,string $type) public function __construct(string $filename,string $type)
{ {
@ -39,4 +42,85 @@ abstract class Base
throw new \Exception('Unknown key: '.$key); 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();
}
} }

View File

@ -5,7 +5,7 @@ namespace App\Media;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Media\QuickTime\Atoms\{mdat,moov,Unknown}; 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 // https://developer.apple.com/documentation/quicktime-file-format/quicktime_movie_files

View File

@ -4,23 +4,17 @@ namespace App\Media\QuickTime;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Media\QuickTime;
use App\Media\QuickTime\Atoms\moov\{mvhd,trak}; use App\Media\QuickTime\Atoms\moov\{mvhd,trak};
use App\Traits\FindQuicktimeAtoms;
abstract class Atom abstract class Atom extends QuickTime
{ {
use FindQuicktimeAtoms; use FindQuicktimeAtoms;
protected const record_size = 16384;
protected const BLOCK_SIZE = 4096;
protected int $offset; protected int $offset;
protected int $size; protected int $size;
protected string $filename; protected string $filename;
private mixed $fh;
private int $fp;
protected Collection $cache; protected Collection $cache;
protected Collection $atoms; protected Collection $atoms;
@ -65,80 +59,4 @@ abstract class Atom
throw new \Exception('Unknown key: '.$key); 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();
}
} }

View File

@ -13,7 +13,6 @@ class minf extends SubAtom
private const subatom_classes = 'App\\Media\\QuickTime\\Atoms\\moov\\trak\\mdia\\minf\\'; 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) public function __construct(int $offset,int $size,string $filename,?string $data,string $arg=NULL)
{ {
parent::__construct($offset,$size,$filename); parent::__construct($offset,$size,$filename);

View File

@ -1,11 +1,9 @@
<?php <?php
namespace App\Traits; namespace App\Media\QuickTime;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use App\Media\QuickTime\Atom;
trait FindQuicktimeAtoms trait FindQuicktimeAtoms
{ {
protected function get_atoms(string $class_prefix,string $unknown,int $offset,int $size,string $atom=NULL,string $passthru=NULL,\Closure $callback=NULL): Collection protected function get_atoms(string $class_prefix,string $unknown,int $offset,int $size,string $atom=NULL,string $passthru=NULL,\Closure $callback=NULL): Collection
@ -40,7 +38,7 @@ trait FindQuicktimeAtoms
$data = $atom $data = $atom
? substr($atom,$rp,$header['size']-8) ? substr($atom,$rp,$header['size']-8)
: ($header['size']-8 && ($header['size']-8 <= self::BLOCK_SIZE) ? fread($fh,$header['size']-8) : NULL); : ($header['size']-8 && ($header['size']-8 <= static::record_size) ? fread($fh,$header['size']-8) : NULL);
if ($header['size'] >= 8) { if ($header['size'] >= 8) {
$o = class_exists($class) $o = class_exists($class)
@ -52,7 +50,7 @@ trait FindQuicktimeAtoms
$rp += $header['size']-8; $rp += $header['size']-8;
// Only need to seek if we didnt read all the data // 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); fseek($fh,$offset+$rp);
} else { } else {