87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Motion;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
use Symfony\Component\Finder\Finder;
|
|
use App\Motion\Snapshot;
|
|
|
|
/**
|
|
* class SnapshotRepository.
|
|
*
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
*/
|
|
class SnapshotRepository
|
|
{
|
|
protected ParameterBagInterface $parameters;
|
|
|
|
protected FFProbe $ffprobe;
|
|
|
|
public function __construct(ParameterBagInterface $parameters)
|
|
{
|
|
$this->parameters = $parameters;
|
|
}
|
|
|
|
public function find(array $criteria = []): array
|
|
{
|
|
$objects = [];
|
|
|
|
$criteria['order'] ??= 'ASC';
|
|
|
|
$directoriesFinder = new Finder();
|
|
|
|
$directories = $directoriesFinder
|
|
->directories()
|
|
->in($this->parameters->get('motion_snapshots_directory'))
|
|
->sortByName();
|
|
|
|
$directories->sortByName();
|
|
|
|
foreach ($directories as $directory) {
|
|
$moviesFinder = new Finder();
|
|
|
|
$files = $moviesFinder
|
|
->files()
|
|
->in($directory->getPathName())
|
|
->sortByName();
|
|
|
|
$date = $directory->getFilename();
|
|
|
|
foreach ($files as $file) {
|
|
$time = str_replace(['.mp4', '.jpg', '-'], ['', '', ':'], $file->getBasename());
|
|
|
|
$snapshot = new Snapshot();
|
|
$snapshot->setDate(new \DateTime($date.' '.$time));
|
|
|
|
if (substr($file->getFilename(), -3) === 'jpg') {
|
|
$movie = str_replace('.jpg', '.mp4', $file->getPathName());
|
|
|
|
if (file_exists($movie)) {
|
|
continue;
|
|
}
|
|
|
|
$thumbnail = $file->getPathName();
|
|
$snapshot->setPicture($file->getPathName());
|
|
$snapshot->setMovie('');
|
|
} else {
|
|
$thumbnail = str_replace('.mp4', '.jpg', $file->getPathName());
|
|
$snapshot->setMovie($file->getPathName());
|
|
$snapshot->setPicture('');
|
|
}
|
|
|
|
$snapshot->setThumbnailChecksum(sha1_file($thumbnail));
|
|
$snapshot->setThumbnail($thumbnail);
|
|
|
|
$objects[] = $snapshot;
|
|
}
|
|
}
|
|
|
|
if ($criteria['order'] === 'DESC') {
|
|
$objects = array_reverse($objects);
|
|
}
|
|
|
|
return $objects;
|
|
}
|
|
}
|