2016-03-22 08:22:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use getID3;
|
|
|
|
use getid3_lib;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2016-07-05 10:14:12 +00:00
|
|
|
use Media;
|
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
|
|
|
|
|
|
|
class File
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A MD5 hash of the file's path.
|
|
|
|
* This value is unique, and can be used to query a Song record.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $hash;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file's last modified time.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $mtime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The file's path.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The getID3 object, for ID3 tag reading.
|
|
|
|
*
|
|
|
|
* @var getID3
|
|
|
|
*/
|
|
|
|
protected $getID3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The SplFileInfo object of the file.
|
|
|
|
*
|
|
|
|
* @var SplFileInfo
|
|
|
|
*/
|
|
|
|
protected $splFileInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The song model that's associated with this file.
|
|
|
|
*
|
|
|
|
* @var Song
|
|
|
|
*/
|
|
|
|
protected $song;
|
|
|
|
|
2016-08-17 14:48:18 +00:00
|
|
|
/**
|
|
|
|
* The last parsing error text, if any.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $syncError;
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
/**
|
|
|
|
* Construct our File object.
|
2016-03-27 02:54:22 +00:00
|
|
|
* Upon construction, we'll set the path, hash, and associated Song object (if any).
|
2016-03-22 08:22:39 +00:00
|
|
|
*
|
|
|
|
* @param string|SplFileInfo $path Either the file's path, or a SplFileInfo object
|
|
|
|
* @param getID3 $getID3 A getID3 object for DI (and better performance)
|
|
|
|
*/
|
|
|
|
public function __construct($path, $getID3 = null)
|
|
|
|
{
|
2016-04-05 07:38:10 +00:00
|
|
|
$this->splFileInfo = $path instanceof SplFileInfo ? $path : new SplFileInfo($path);
|
2016-03-22 08:22:39 +00:00
|
|
|
$this->setGetID3($getID3);
|
2016-08-11 03:25:17 +00:00
|
|
|
|
|
|
|
// Workaround for #344, where getMTime() fails for certain files with Unicode names
|
|
|
|
// on Windows.
|
|
|
|
// Yes, beloved Windows.
|
|
|
|
try {
|
|
|
|
$this->mtime = $this->splFileInfo->getMTime();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// Not worth logging the error. Just use current stamp for mtime.
|
|
|
|
$this->mtime = time();
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
$this->path = $this->splFileInfo->getPathname();
|
|
|
|
$this->hash = self::getHash($this->path);
|
|
|
|
$this->song = Song::find($this->hash);
|
2016-08-17 14:48:18 +00:00
|
|
|
$this->syncError = '';
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all applicable ID3 info from the file.
|
|
|
|
*
|
2017-04-23 16:01:02 +00:00
|
|
|
* @return array
|
2016-03-22 08:22:39 +00:00
|
|
|
*/
|
|
|
|
public function getInfo()
|
|
|
|
{
|
|
|
|
$info = $this->getID3->analyze($this->path);
|
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
if (isset($info['error']) || !isset($info['playtime_seconds'])) {
|
2016-08-17 14:48:18 +00:00
|
|
|
$this->syncError = isset($info['error']) ? $info['error'][0] : 'No playtime found';
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the available tags over to comment.
|
|
|
|
// This is a helper from getID3, though it doesn't really work well.
|
|
|
|
// We'll still prefer getting ID3v2 tags directly later.
|
|
|
|
// Read on.
|
|
|
|
getid3_lib::CopyTagsToComments($info);
|
|
|
|
|
2016-03-23 14:07:50 +00:00
|
|
|
$track = 0;
|
2016-03-24 03:06:28 +00:00
|
|
|
|
|
|
|
// Apparently track number can be stored with different indices as the following.
|
2016-03-23 14:07:50 +00:00
|
|
|
$trackIndices = [
|
|
|
|
'comments.track',
|
|
|
|
'comments.tracknumber',
|
|
|
|
'comments.track_number',
|
|
|
|
];
|
2016-03-24 03:06:28 +00:00
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
for ($i = 0; $i < count($trackIndices) && $track === 0; ++$i) {
|
2016-03-23 14:07:50 +00:00
|
|
|
$track = array_get($info, $trackIndices[$i], [0])[0];
|
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
|
|
|
|
$props = [
|
|
|
|
'artist' => '',
|
|
|
|
'album' => '',
|
2016-06-16 10:17:16 +00:00
|
|
|
'compilation' => false,
|
2016-03-22 08:22:39 +00:00
|
|
|
'title' => '',
|
|
|
|
'length' => $info['playtime_seconds'],
|
2016-03-24 03:06:28 +00:00
|
|
|
'track' => (int) $track,
|
2016-03-22 08:22:39 +00:00
|
|
|
'lyrics' => '',
|
|
|
|
'cover' => array_get($info, 'comments.picture', [null])[0],
|
|
|
|
'path' => $this->path,
|
|
|
|
'mtime' => $this->mtime,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!$comments = array_get($info, 'comments_html')) {
|
|
|
|
return $props;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We prefer id3v2 tags over others.
|
|
|
|
if (!$artist = array_get($info, 'tags.id3v2.artist', [null])[0]) {
|
|
|
|
$artist = array_get($comments, 'artist', [''])[0];
|
|
|
|
}
|
|
|
|
|
2016-06-16 10:17:16 +00:00
|
|
|
if (!$albumArtist = array_get($info, 'tags.id3v2.band', [null])[0]) {
|
|
|
|
$albumArtist = array_get($comments, 'band', [''])[0];
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
if (!$album = array_get($info, 'tags.id3v2.album', [null])[0]) {
|
|
|
|
$album = array_get($comments, 'album', [''])[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$title = array_get($info, 'tags.id3v2.title', [null])[0]) {
|
|
|
|
$title = array_get($comments, 'title', [''])[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$lyrics = array_get($info, 'tags.id3v2.unsynchronised_lyric', [null])[0]) {
|
|
|
|
$lyrics = array_get($comments, 'unsynchronised_lyric', [''])[0];
|
|
|
|
}
|
|
|
|
|
2016-05-05 15:03:30 +00:00
|
|
|
// Fixes #323, where tag names can be htmlentities()'ed
|
|
|
|
$props['title'] = html_entity_decode(trim($title));
|
2016-06-16 10:17:16 +00:00
|
|
|
$props['album'] = html_entity_decode(trim($album));
|
|
|
|
$props['artist'] = html_entity_decode(trim($artist));
|
|
|
|
$props['albumartist'] = html_entity_decode(trim($albumArtist));
|
2016-05-05 15:03:30 +00:00
|
|
|
$props['lyrics'] = html_entity_decode(trim($lyrics));
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2016-06-16 10:17:16 +00:00
|
|
|
// A "compilation" property can is determined by:
|
|
|
|
// - "part_of_a_compilation" tag (used by iTunes), or
|
|
|
|
// - "albumartist" (used by non-retarded applications).
|
|
|
|
$props['compilation'] = (bool) (
|
|
|
|
array_get($comments, 'part_of_a_compilation', [false])[0] || $props['albumartist']
|
|
|
|
);
|
|
|
|
|
2017-04-23 16:01:02 +00:00
|
|
|
return $props;
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the song with all available media info against the database.
|
|
|
|
*
|
|
|
|
* @param array $tags The (selective) tags to sync (if the song exists)
|
2016-06-04 14:17:24 +00:00
|
|
|
* @param bool $force Whether to force syncing, even if the file is unchanged
|
2016-03-22 08:22:39 +00:00
|
|
|
*
|
|
|
|
* @return bool|Song A Song object on success,
|
|
|
|
* true if file exists but is unmodified,
|
|
|
|
* or false on an error.
|
|
|
|
*/
|
|
|
|
public function sync($tags, $force = false)
|
|
|
|
{
|
|
|
|
// If the file is not new or changed and we're not forcing update, don't do anything.
|
|
|
|
if (!$this->isNewOrChanged() && !$force) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the file is invalid, don't do anything.
|
|
|
|
if (!$info = $this->getInfo()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-05 10:14:12 +00:00
|
|
|
// Fixes #366. If the file is new, we use all tags by simply setting $force to false.
|
|
|
|
if ($this->isNew()) {
|
|
|
|
$force = false;
|
|
|
|
}
|
|
|
|
|
2016-05-21 10:52:23 +00:00
|
|
|
$artist = null;
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
if ($this->isChanged() || $force) {
|
|
|
|
// This is a changed file, or the user is forcing updates.
|
2016-05-21 10:52:23 +00:00
|
|
|
// In such a case, the user must have specified a list of tags to sync.
|
|
|
|
// A sample command could be: ./artisan koel:sync --force --tags=artist,album,lyrics
|
|
|
|
// We cater for these tags by removing those not specified.
|
2016-06-04 18:17:27 +00:00
|
|
|
|
2016-06-16 10:17:16 +00:00
|
|
|
// There's a special case with 'album' though.
|
|
|
|
// If 'compilation' tag is specified, 'album' must be counted in as well.
|
2016-06-04 18:17:27 +00:00
|
|
|
// But if 'album' isn't specified, we don't want to update normal albums.
|
|
|
|
// This variable is to keep track of this state.
|
|
|
|
$changeCompilationAlbumOnly = false;
|
2016-08-16 15:12:11 +00:00
|
|
|
if (in_array('compilation', $tags, true) && !in_array('album', $tags, true)) {
|
2016-05-21 10:52:23 +00:00
|
|
|
$tags[] = 'album';
|
2016-06-04 18:17:27 +00:00
|
|
|
$changeCompilationAlbumOnly = true;
|
2016-05-21 10:52:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
$info = array_intersect_key($info, array_flip($tags));
|
|
|
|
|
2016-05-21 10:52:23 +00:00
|
|
|
// If the "artist" tag is specified, use it.
|
|
|
|
// Otherwise, re-use the existing model value.
|
2016-03-22 08:22:39 +00:00
|
|
|
$artist = isset($info['artist']) ? Artist::get($info['artist']) : $this->song->album->artist;
|
2016-05-21 10:52:23 +00:00
|
|
|
|
2016-06-16 10:17:16 +00:00
|
|
|
$isCompilation = (bool) array_get($info, 'compilation');
|
2016-05-21 10:52:23 +00:00
|
|
|
|
|
|
|
// If the "album" tag is specified, use it.
|
|
|
|
// Otherwise, re-use the existing model value.
|
2016-06-04 18:17:27 +00:00
|
|
|
if (isset($info['album'])) {
|
|
|
|
$album = $changeCompilationAlbumOnly
|
|
|
|
? $this->song->album
|
|
|
|
: Album::get($artist, $info['album'], $isCompilation);
|
|
|
|
} else {
|
|
|
|
$album = $this->song->album;
|
|
|
|
}
|
2016-03-22 08:22:39 +00:00
|
|
|
} else {
|
2016-05-21 10:52:23 +00:00
|
|
|
// The file is newly added.
|
2016-06-16 10:17:16 +00:00
|
|
|
$isCompilation = (bool) array_get($info, 'compilation');
|
2016-05-21 10:52:23 +00:00
|
|
|
$artist = Artist::get($info['artist']);
|
|
|
|
$album = Album::get($artist, $info['album'], $isCompilation);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
if (!$album->has_cover) {
|
|
|
|
// If the album has no cover, we try to get the cover image from existing tag data
|
|
|
|
if (!empty($info['cover'])) {
|
|
|
|
try {
|
|
|
|
$album->generateCover($info['cover']);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// or, if there's a cover image under the same directory, use it.
|
|
|
|
elseif ($cover = $this->getCoverFileUnderSameDirectory()) {
|
|
|
|
$album->copyCoverFile($cover);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$info['album_id'] = $album->id;
|
2017-04-23 16:01:02 +00:00
|
|
|
$info['contributing_artist_id'] = $artist->id;
|
2016-04-17 15:38:06 +00:00
|
|
|
|
2016-03-24 03:26:52 +00:00
|
|
|
// Remove these values from the info array, so that we can just use the array as model's input data.
|
2016-06-16 10:17:16 +00:00
|
|
|
array_forget($info, ['artist', 'albumartist', 'album', 'cover', 'compilation']);
|
2016-03-22 08:22:39 +00:00
|
|
|
|
2016-07-05 10:14:12 +00:00
|
|
|
return Song::updateOrCreate(['id' => $this->hash], $info);
|
2016-03-22 08:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the file is new (its Song record can't be found in the database).
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isNew()
|
|
|
|
{
|
|
|
|
return !$this->song;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the file is changed (its Song record is found, but the timestamp is different).
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isChanged()
|
|
|
|
{
|
|
|
|
return !$this->isNew() && $this->song->mtime !== $this->mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the file is new or changed.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isNewOrChanged()
|
|
|
|
{
|
|
|
|
return $this->isNew() || $this->isChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return getID3
|
|
|
|
*/
|
|
|
|
public function getGetID3()
|
|
|
|
{
|
|
|
|
return $this->getID3;
|
|
|
|
}
|
|
|
|
|
2016-08-17 14:48:18 +00:00
|
|
|
/**
|
|
|
|
* Get the last parsing error's text.
|
|
|
|
*
|
2017-04-23 16:01:02 +00:00
|
|
|
* @return string
|
2016-08-17 14:48:18 +00:00
|
|
|
*/
|
|
|
|
public function getSyncError()
|
|
|
|
{
|
|
|
|
return $this->syncError;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
/**
|
|
|
|
* @param getID3 $getID3
|
|
|
|
*/
|
|
|
|
public function setGetID3($getID3 = null)
|
|
|
|
{
|
|
|
|
$this->getID3 = $getID3 ?: new getID3();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2016-08-16 15:12:11 +00:00
|
|
|
* @throws \InvalidArgumentException
|
2016-08-16 15:12:35 +00:00
|
|
|
*
|
|
|
|
* @return string|false The cover file's full path, or false if none found
|
2016-08-07 10:30:55 +00:00
|
|
|
*/
|
|
|
|
private function getCoverFileUnderSameDirectory()
|
|
|
|
{
|
|
|
|
// As directory scanning can be expensive, we cache and reuse the result.
|
|
|
|
$cacheKey = md5($this->path.'_cover');
|
|
|
|
|
2016-12-12 02:43:14 +00:00
|
|
|
if (!is_null($cover = cache($cacheKey))) {
|
2016-08-07 10:30:55 +00:00
|
|
|
return $cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = array_keys(iterator_to_array(
|
|
|
|
Finder::create()
|
|
|
|
->depth(0)
|
|
|
|
->ignoreUnreadableDirs()
|
|
|
|
->files()
|
2016-08-17 14:46:22 +00:00
|
|
|
->followLinks()
|
2016-08-07 10:30:55 +00:00
|
|
|
->name('/(cov|fold)er\.(jpe?g|png)$/i')
|
|
|
|
->in(dirname($this->path))
|
|
|
|
));
|
|
|
|
|
|
|
|
$cover = $matches ? $matches[0] : false;
|
|
|
|
// Even if a file is found, make sure it's a real image.
|
|
|
|
if ($cover && exif_imagetype($cover) === false) {
|
|
|
|
$cover = false;
|
|
|
|
}
|
|
|
|
|
2016-12-12 02:43:14 +00:00
|
|
|
cache([$cacheKey => $cover], 24 * 60);
|
2016-08-07 10:30:55 +00:00
|
|
|
|
|
|
|
return $cover;
|
|
|
|
}
|
|
|
|
|
2016-03-22 08:22:39 +00:00
|
|
|
/**
|
|
|
|
* Get a unique hash from a file path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getHash($path)
|
|
|
|
{
|
|
|
|
return md5(config('app.key').$path);
|
|
|
|
}
|
|
|
|
}
|