2016-03-22 08:22:39 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
namespace App\Services;
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Models\Song;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\SongRepository;
|
2024-01-04 21:51:32 +00:00
|
|
|
use App\Values\ScanConfiguration;
|
|
|
|
use App\Values\ScanResult;
|
2022-07-05 13:47:26 +00:00
|
|
|
use App\Values\SongScanInformation;
|
2019-10-09 17:36:22 +00:00
|
|
|
use getID3;
|
2018-08-29 09:41:24 +00:00
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache;
|
2022-07-05 13:47:26 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2016-03-22 08:22:39 +00:00
|
|
|
use SplFileInfo;
|
2016-08-07 10:31:19 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
class FileScanner
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
private ?int $fileModifiedTime = null;
|
|
|
|
private ?string $filePath = null;
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2016-08-17 14:48:18 +00:00
|
|
|
/**
|
2018-08-29 09:41:24 +00:00
|
|
|
* The song model that's associated with the current file.
|
2016-08-17 14:48:18 +00:00
|
|
|
*/
|
2021-06-05 10:47:56 +00:00
|
|
|
private ?Song $song;
|
2016-08-17 14:48:18 +00:00
|
|
|
|
2022-07-29 11:08:24 +00:00
|
|
|
private ?string $syncError = null;
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
public function __construct(
|
2022-07-05 13:47:26 +00:00
|
|
|
private getID3 $getID3,
|
|
|
|
private MediaMetadataService $mediaMetadataService,
|
|
|
|
private SongRepository $songRepository,
|
2022-09-14 12:12:06 +00:00
|
|
|
private SimpleLrcReader $lrcReader,
|
2022-07-05 13:47:26 +00:00
|
|
|
private Cache $cache,
|
|
|
|
private Finder $finder
|
2019-06-30 11:13:41 +00:00
|
|
|
) {
|
2018-08-29 09:41:24 +00:00
|
|
|
}
|
2017-06-03 23:21:50 +00:00
|
|
|
|
2024-01-04 11:35:36 +00:00
|
|
|
public function setFile(string|SplFileInfo $path): static
|
2018-08-29 09:41:24 +00:00
|
|
|
{
|
2022-07-07 10:45:47 +00:00
|
|
|
$file = $path instanceof SplFileInfo ? $path : new SplFileInfo($path);
|
2016-08-11 03:25:17 +00:00
|
|
|
|
2022-08-01 10:42:33 +00:00
|
|
|
$this->filePath = $file->getRealPath();
|
2024-02-04 20:31:01 +00:00
|
|
|
$this->song = $this->songRepository->findOneByPath($this->filePath);
|
2022-07-07 10:45:47 +00:00
|
|
|
$this->fileModifiedTime = Helper::getModifiedTime($file);
|
2018-08-29 09:41:24 +00:00
|
|
|
|
|
|
|
return $this;
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:31:50 +00:00
|
|
|
public function getScanInformation(): ?SongScanInformation
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2022-08-31 21:27:40 +00:00
|
|
|
$raw = $this->getID3->analyze($this->filePath);
|
|
|
|
$this->syncError = Arr::get($raw, 'error.0') ?: (Arr::get($raw, 'playtime_seconds') ? null : 'Empty file');
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-09-14 12:12:06 +00:00
|
|
|
if ($this->syncError) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:27:40 +00:00
|
|
|
$this->getID3->CopyTagsToComments($raw);
|
2022-11-29 10:05:58 +00:00
|
|
|
$info = SongScanInformation::fromGetId3Info($raw, $this->filePath);
|
2022-08-31 21:27:40 +00:00
|
|
|
|
2022-09-14 12:12:06 +00:00
|
|
|
$info->lyrics = $info->lyrics ?: $this->lrcReader->tryReadForMediaFile($this->filePath);
|
|
|
|
|
|
|
|
return $info;
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
2018-08-29 09:42:11 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
public function scan(ScanConfiguration $config): ScanResult
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2024-01-04 21:51:32 +00:00
|
|
|
if (!$this->isFileNewOrChanged() && !$config->force) {
|
|
|
|
return ScanResult::skipped($this->filePath);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:31:50 +00:00
|
|
|
$info = $this->getScanInformation()?->toArray();
|
2020-12-22 20:11:22 +00:00
|
|
|
|
|
|
|
if (!$info) {
|
2024-01-04 21:51:32 +00:00
|
|
|
return ScanResult::error($this->filePath, $this->syncError);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
if (!$this->isFileNew()) {
|
2024-01-04 21:51:32 +00:00
|
|
|
Arr::forget($info, $config->ignores);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
$artist = Arr::get($info, 'artist') ? Artist::getOrCreate($info['artist']) : $this->song->artist;
|
|
|
|
$albumArtist = Arr::get($info, 'albumartist') ? Artist::getOrCreate($info['albumartist']) : $artist;
|
|
|
|
$album = Arr::get($info, 'album') ? Album::getOrCreate($albumArtist, $info['album']) : $this->song->album;
|
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
if (!in_array('cover', $config->ignores, true) && !$album->has_cover) {
|
2022-07-05 13:47:26 +00:00
|
|
|
$this->tryGenerateAlbumCover($album, Arr::get($info, 'cover', []));
|
2018-09-03 12:41:49 +00:00
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
$data = Arr::except($info, ['album', 'artist', 'albumartist', 'cover']);
|
2017-12-09 02:24:09 +00:00
|
|
|
$data['album_id'] = $album->id;
|
|
|
|
$data['artist_id'] = $artist->id;
|
2024-01-04 21:51:32 +00:00
|
|
|
$data['is_public'] = $config->makePublic;
|
|
|
|
|
|
|
|
if ($this->isFileNew()) {
|
|
|
|
// Only set the owner if the song is new i.e. don't override the owner if the song is being updated.
|
|
|
|
$data['owner_id'] = $config->owner->id;
|
|
|
|
}
|
2022-07-05 13:47:26 +00:00
|
|
|
|
2024-02-23 18:36:02 +00:00
|
|
|
// @todo Decouple song creation from scanning.
|
2022-08-09 18:45:11 +00:00
|
|
|
$this->song = Song::query()->updateOrCreate(['path' => $this->filePath], $data); // @phpstan-ignore-line
|
2017-06-03 23:21:50 +00:00
|
|
|
|
2024-01-04 21:51:32 +00:00
|
|
|
return ScanResult::success($this->filePath);
|
2017-06-03 23:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to generate a cover for an album based on extracted data, or use the cover file under the directory.
|
|
|
|
*
|
2024-01-04 21:51:32 +00:00
|
|
|
* @param ?array<mixed> $coverData
|
2017-06-03 23:21:50 +00:00
|
|
|
*/
|
2022-07-05 13:47:26 +00:00
|
|
|
private function tryGenerateAlbumCover(Album $album, ?array $coverData): void
|
2017-06-03 23:21:50 +00:00
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
attempt(function () use ($album, $coverData): void {
|
2022-07-05 13:47:26 +00:00
|
|
|
// If the album has no cover, we try to get the cover image from existing tag data
|
|
|
|
if ($coverData) {
|
2024-03-19 22:48:12 +00:00
|
|
|
$this->mediaMetadataService->writeAlbumCover($album, $coverData['data']);
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
// Or, if there's a cover image under the same directory, use it.
|
|
|
|
$cover = $this->getCoverFileUnderSameDirectory();
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
if ($cover) {
|
2024-03-19 22:48:12 +00:00
|
|
|
$this->mediaMetadataService->writeAlbumCover($album, $cover);
|
2022-07-05 13:47:26 +00:00
|
|
|
}
|
2022-08-08 16:00:59 +00:00
|
|
|
}, false);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
/**
|
|
|
|
* Issue #380.
|
|
|
|
* Some albums have its own cover image under the same directory as cover|folder.jpg/png.
|
|
|
|
* We'll check if such a cover file is found, and use it if positive.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
private function getCoverFileUnderSameDirectory(): ?string
|
2016-08-07 10:30:55 +00:00
|
|
|
{
|
|
|
|
// As directory scanning can be expensive, we cache and reuse the result.
|
2021-06-05 10:47:56 +00:00
|
|
|
return $this->cache->remember(md5($this->filePath . '_cover'), now()->addDay(), function (): ?string {
|
2020-03-10 10:16:55 +00:00
|
|
|
$matches = array_keys(
|
|
|
|
iterator_to_array(
|
2020-03-12 15:30:52 +00:00
|
|
|
$this->finder->create()
|
2020-12-22 20:11:22 +00:00
|
|
|
->depth(0)
|
|
|
|
->ignoreUnreadableDirs()
|
|
|
|
->files()
|
|
|
|
->followLinks()
|
|
|
|
->name('/(cov|fold)er\.(jpe?g|png)$/i')
|
|
|
|
->in(dirname($this->filePath))
|
2020-03-12 15:30:52 +00:00
|
|
|
)
|
2017-08-05 21:58:50 +00:00
|
|
|
);
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
$cover = $matches ? $matches[0] : null;
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
return $cover && self::isImage($cover) ? $cover : null;
|
2017-08-05 21:58:50 +00:00
|
|
|
});
|
2016-08-07 10:30:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 13:47:26 +00:00
|
|
|
private static function isImage(string $path): bool
|
2018-10-06 10:44:25 +00:00
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
return attempt(static fn () => (bool) exif_imagetype($path)) ?? false;
|
2018-10-06 10:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
/**
|
|
|
|
* Determine if the file is new (its Song record can't be found in the database).
|
|
|
|
*/
|
|
|
|
public function isFileNew(): bool
|
|
|
|
{
|
|
|
|
return !$this->song;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the file is changed (its Song record is found, but the timestamp is different).
|
|
|
|
*/
|
|
|
|
public function isFileChanged(): bool
|
2016-03-22 08:22:39 +00:00
|
|
|
{
|
2018-08-29 09:41:24 +00:00
|
|
|
return !$this->isFileNew() && $this->song->mtime !== $this->fileModifiedTime;
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2018-08-29 09:41:24 +00:00
|
|
|
public function isFileNewOrChanged(): bool
|
2018-08-19 09:05:33 +00:00
|
|
|
{
|
2018-08-29 09:41:24 +00:00
|
|
|
return $this->isFileNew() || $this->isFileChanged();
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:43:04 +00:00
|
|
|
public function getSong(): ?Song
|
|
|
|
{
|
|
|
|
return $this->song;
|
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|