2015-01-02 08:16:54 +00:00
|
|
|
<?php
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2015-01-02 08:16:54 +00:00
|
|
|
namespace Entity;
|
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2017-08-17 18:28:48 +00:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2015-01-02 08:16:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Table(name="station_media", indexes={
|
2016-09-23 00:49:59 +00:00
|
|
|
* @index(name="search_idx", columns={"title", "artist", "album"})
|
|
|
|
* }, uniqueConstraints={
|
2016-11-22 05:09:18 +00:00
|
|
|
* @UniqueConstraint(name="path_unique_idx", columns={"path", "station_id"})
|
2015-01-02 08:16:54 +00:00
|
|
|
* })
|
2017-01-17 08:46:43 +00:00
|
|
|
* @Entity(repositoryClass="Entity\Repository\StationMediaRepository")
|
2015-01-02 08:16:54 +00:00
|
|
|
* @HasLifecycleCallbacks
|
|
|
|
*/
|
2017-08-17 18:28:48 +00:00
|
|
|
class StationMedia
|
2015-01-02 08:16:54 +00:00
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
use Traits\UniqueId, Traits\TruncateStrings;
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2015-01-02 08:16:54 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="id", type="integer")
|
|
|
|
* @Id
|
2016-05-15 09:31:21 +00:00
|
|
|
* @GeneratedValue(strategy="IDENTITY")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
2015-01-02 08:16:54 +00:00
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="station_id", type="integer")
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $station_id;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @ManyToOne(targetEntity="Station", inversedBy="media")
|
|
|
|
* @JoinColumns({
|
|
|
|
* @JoinColumn(name="station_id", referencedColumnName="id", onDelete="CASCADE")
|
|
|
|
* })
|
|
|
|
* @var Station
|
|
|
|
*/
|
|
|
|
protected $station;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="song_id", type="string", length=50, nullable=true)
|
|
|
|
* @var int|null
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $song_id;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @ManyToOne(targetEntity="Song")
|
|
|
|
* @JoinColumns({
|
|
|
|
* @JoinColumn(name="song_id", referencedColumnName="id", onDelete="SET NULL")
|
|
|
|
* })
|
|
|
|
* @var Song|null
|
|
|
|
*/
|
|
|
|
protected $song;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="title", type="string", length=200, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $title;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="artist", type="string", length=200, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $artist;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="album", type="string", length=200, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $album;
|
|
|
|
|
2017-11-04 02:13:41 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="lyrics", type="text", nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $lyrics;
|
|
|
|
|
|
|
|
/**
|
2017-11-28 12:22:40 +00:00
|
|
|
* @OneToOne(targetEntity="StationMediaArt", mappedBy="media", cascade={"persist"})
|
2017-11-24 19:04:07 +00:00
|
|
|
* @var StationMediaArt
|
2017-11-04 02:13:41 +00:00
|
|
|
*/
|
|
|
|
protected $art;
|
|
|
|
|
2017-05-24 09:21:11 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="isrc", type="string", length=15, nullable=true)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null The track ISRC (International Standard Recording Code), used for licensing purposes.
|
2017-05-24 09:21:11 +00:00
|
|
|
*/
|
|
|
|
protected $isrc;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="length", type="smallint")
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-01-02 08:16:54 +00:00
|
|
|
protected $length;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @Column(name="length_text", type="string", length=10, nullable=true)
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $length_text;
|
|
|
|
|
|
|
|
/**
|
2017-08-29 03:44:51 +00:00
|
|
|
* @Column(name="path", type="string", length=255, nullable=true)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
protected $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="mtime", type="integer", nullable=true)
|
|
|
|
* @var int|null
|
|
|
|
*/
|
|
|
|
protected $mtime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="fade_overlap", type="decimal", precision=3, scale=1, nullable=true)
|
|
|
|
* @var float|null
|
|
|
|
*/
|
|
|
|
protected $fade_overlap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="fade_in", type="decimal", precision=3, scale=1, nullable=true)
|
|
|
|
* @var float|null
|
|
|
|
*/
|
|
|
|
protected $fade_in;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="fade_out", type="decimal", precision=3, scale=1, nullable=true)
|
|
|
|
* @var float|null
|
|
|
|
*/
|
|
|
|
protected $fade_out;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="cue_in", type="decimal", precision=5, scale=1, nullable=true)
|
|
|
|
* @var float|null
|
|
|
|
*/
|
|
|
|
protected $cue_in;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Column(name="cue_out", type="decimal", precision=5, scale=1, nullable=true)
|
|
|
|
* @var float|null
|
|
|
|
*/
|
|
|
|
protected $cue_out;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ManyToMany(targetEntity="StationPlaylist", inversedBy="media")
|
|
|
|
* @JoinTable(name="station_playlist_has_media",
|
|
|
|
* joinColumns={@JoinColumn(name="media_id", referencedColumnName="id", onDelete="CASCADE")},
|
|
|
|
* inverseJoinColumns={@JoinColumn(name="playlists_id", referencedColumnName="id", onDelete="CASCADE")}
|
|
|
|
* )
|
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
protected $playlists;
|
|
|
|
|
|
|
|
public function __construct(Station $station, string $path)
|
|
|
|
{
|
|
|
|
$this->station = $station;
|
|
|
|
$this->path = $path;
|
|
|
|
|
|
|
|
$this->length = 0;
|
|
|
|
$this->length_text = '0:00';
|
|
|
|
|
|
|
|
$this->mtime = 0;
|
|
|
|
|
|
|
|
$this->playlists = new ArrayCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Station
|
|
|
|
*/
|
|
|
|
public function getStation(): Station
|
|
|
|
{
|
|
|
|
return $this->station;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Song|null
|
|
|
|
*/
|
|
|
|
public function getSong(): ?Song
|
|
|
|
{
|
|
|
|
return $this->song;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Song|null $song
|
|
|
|
*/
|
|
|
|
public function setSong(Song $song = null)
|
|
|
|
{
|
|
|
|
$this->song = $song;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getTitle(): ?string
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $title
|
|
|
|
*/
|
|
|
|
public function setTitle(string $title = null)
|
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->title = $this->_truncateString($title, 200);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getArtist(): ?string
|
|
|
|
{
|
|
|
|
return $this->artist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $artist
|
|
|
|
*/
|
|
|
|
public function setArtist(string $artist = null)
|
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->artist = $this->_truncateString($artist, 200);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getAlbum(): ?string
|
|
|
|
{
|
|
|
|
return $this->album;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $album
|
|
|
|
*/
|
|
|
|
public function setAlbum(string $album = null)
|
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->album = $this->_truncateString($album, 200);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 02:13:41 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getLyrics()
|
|
|
|
{
|
|
|
|
return $this->lyrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $lyrics
|
|
|
|
*/
|
|
|
|
public function setLyrics($lyrics)
|
|
|
|
{
|
|
|
|
$this->lyrics = $lyrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|resource
|
|
|
|
*/
|
|
|
|
public function getArt()
|
|
|
|
{
|
2017-11-24 19:04:07 +00:00
|
|
|
if ($this->art instanceof StationMediaArt) {
|
|
|
|
return $this->art->getArt();
|
|
|
|
}
|
|
|
|
return null;
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-04 06:09:49 +00:00
|
|
|
* @param resource $source_image_path A GD image manipulation resource.
|
2017-11-04 02:13:41 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-11-04 06:09:49 +00:00
|
|
|
public function setArt($source_gd_image = null)
|
2017-11-04 02:13:41 +00:00
|
|
|
{
|
2017-11-24 19:04:07 +00:00
|
|
|
if (!($this->art instanceof StationMediaArt)) {
|
|
|
|
$this->art = new StationMediaArt($this);
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 19:04:07 +00:00
|
|
|
return $this->art->setArt($source_gd_image);
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getIsrc(): ?string
|
|
|
|
{
|
|
|
|
return $this->isrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $isrc
|
|
|
|
*/
|
|
|
|
public function setIsrc(string $isrc = null)
|
|
|
|
{
|
|
|
|
$this->isrc = $isrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLength(): int
|
|
|
|
{
|
|
|
|
return $this->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $length
|
|
|
|
*/
|
2017-01-24 00:17:50 +00:00
|
|
|
public function setLength($length)
|
|
|
|
{
|
|
|
|
$length_min = floor($length / 60);
|
|
|
|
$length_sec = $length % 60;
|
|
|
|
|
2017-07-30 22:04:34 +00:00
|
|
|
$this->length = round($length);
|
2017-01-24 00:35:16 +00:00
|
|
|
$this->length_text = $length_min . ':' . str_pad($length_sec, 2, '0', STR_PAD_LEFT);
|
2017-01-24 00:17:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getLengthText(): ?string
|
|
|
|
{
|
|
|
|
return $this->length_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $length_text
|
|
|
|
*/
|
|
|
|
public function setLengthText(string $length_text = null)
|
|
|
|
{
|
|
|
|
$this->length_text = $length_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function getPath(): ?string
|
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param null|string $path
|
|
|
|
*/
|
|
|
|
public function setPath(string $path = null)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getMtime(): ?int
|
|
|
|
{
|
|
|
|
return $this->mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $mtime
|
|
|
|
*/
|
|
|
|
public function setMtime(int $mtime = null)
|
|
|
|
{
|
|
|
|
$this->mtime = $mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getFadeOverlap(): ?float
|
|
|
|
{
|
|
|
|
return $this->fade_overlap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $fade_overlap
|
|
|
|
*/
|
|
|
|
public function setFadeOverlap(float $fade_overlap = null)
|
|
|
|
{
|
|
|
|
$this->fade_overlap = $fade_overlap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getFadeIn(): ?float
|
|
|
|
{
|
|
|
|
return $this->fade_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $fade_in
|
|
|
|
*/
|
|
|
|
public function setFadeIn(float $fade_in = null)
|
|
|
|
{
|
|
|
|
$this->fade_in = $fade_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getFadeOut(): ?float
|
|
|
|
{
|
|
|
|
return $this->fade_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $fade_out
|
|
|
|
*/
|
|
|
|
public function setFadeOut(float $fade_out = null)
|
|
|
|
{
|
|
|
|
$this->fade_out = $fade_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getCueIn(): ?float
|
|
|
|
{
|
|
|
|
return $this->cue_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $cue_in
|
|
|
|
*/
|
|
|
|
public function setCueIn(float $cue_in = null)
|
|
|
|
{
|
|
|
|
$this->cue_in = $cue_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getCueOut(): ?float
|
|
|
|
{
|
|
|
|
return $this->cue_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param float|null $cue_out
|
|
|
|
*/
|
|
|
|
public function setCueOut(float $cue_out = null)
|
|
|
|
{
|
|
|
|
$this->cue_out = $cue_out;
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:57 +00:00
|
|
|
/**
|
|
|
|
* Get the length with cue-in and cue-out points included.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCalculatedLength()
|
|
|
|
{
|
|
|
|
$length = (int)$this->length;
|
|
|
|
|
|
|
|
if ((int)$this->cue_out > 0) {
|
|
|
|
$length_removed = $length - (int)$this->cue_out;
|
|
|
|
$length -= $length_removed;
|
|
|
|
}
|
|
|
|
if ((int)$this->cue_in > 0) {
|
|
|
|
$length -= $this->cue_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $length;
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPlaylists(): Collection
|
2017-01-24 00:17:50 +00:00
|
|
|
{
|
2017-08-17 18:28:48 +00:00
|
|
|
return $this->playlists;
|
2017-01-24 00:17:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Assemble a list of annotations for LiquidSoap.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getAnnotations(): array
|
2017-05-18 10:35:46 +00:00
|
|
|
{
|
2017-06-14 17:39:11 +00:00
|
|
|
$annotations = [];
|
2017-05-18 10:35:46 +00:00
|
|
|
$annotation_types = [
|
2017-06-14 17:39:11 +00:00
|
|
|
'title' => 'title',
|
|
|
|
'artist' => 'artist',
|
2017-05-18 10:35:46 +00:00
|
|
|
'fade_overlap' => 'liq_start_next',
|
|
|
|
'fade_in' => 'liq_fade_in',
|
|
|
|
'fade_out' => 'liq_fade_out',
|
|
|
|
'cue_in' => 'liq_cue_in',
|
|
|
|
'cue_out' => 'liq_cue_out',
|
|
|
|
];
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
foreach ($annotation_types as $annotation_property => $annotation_name) {
|
2017-05-18 10:35:46 +00:00
|
|
|
if ($this->$annotation_property !== null) {
|
2017-08-04 23:48:24 +00:00
|
|
|
$prop = $this->$annotation_property;
|
2017-10-19 02:30:23 +00:00
|
|
|
$prop = mb_convert_encoding($prop, "UTF-8");
|
2017-08-04 23:48:24 +00:00
|
|
|
$prop = str_replace(['"', "\n", "\t", "\r"], ["'", '', '', ''], $prop);
|
2017-10-19 02:30:23 +00:00
|
|
|
|
2017-10-19 21:55:02 +00:00
|
|
|
// if ($annotation_property == 'title') {
|
|
|
|
// $prop .= ' #'.$this->song_id.'#';
|
|
|
|
// }
|
2017-10-19 02:30:23 +00:00
|
|
|
|
|
|
|
$annotations[$annotation_property] = $annotation_name . '="' . $prop . '"';
|
2017-05-18 10:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $annotations;
|
|
|
|
}
|
|
|
|
|
2016-05-15 09:31:21 +00:00
|
|
|
/**
|
|
|
|
* Process metadata information from media file.
|
2017-07-29 07:18:54 +00:00
|
|
|
*
|
|
|
|
* @param bool $force
|
|
|
|
* @return array|bool
|
|
|
|
* - Array containing song information, if one is detected and needs updating
|
|
|
|
* - False if information was not updated
|
2016-05-15 09:31:21 +00:00
|
|
|
*/
|
2017-04-14 23:44:31 +00:00
|
|
|
public function loadFromFile($force = false)
|
2016-05-15 09:31:21 +00:00
|
|
|
{
|
2017-01-24 00:35:16 +00:00
|
|
|
if (empty($this->path)) {
|
2016-05-15 09:31:21 +00:00
|
|
|
return false;
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 09:31:21 +00:00
|
|
|
$media_base_dir = $this->station->getRadioMediaDir();
|
2017-01-24 00:35:16 +00:00
|
|
|
$media_path = $media_base_dir . '/' . $this->path;
|
2016-05-15 09:31:21 +00:00
|
|
|
|
2017-07-29 07:18:54 +00:00
|
|
|
$path_parts = pathinfo($media_path);
|
|
|
|
|
2016-05-15 09:31:21 +00:00
|
|
|
// Only update metadata if the file has been updated.
|
|
|
|
$media_mtime = filemtime($media_path);
|
2017-07-29 07:18:54 +00:00
|
|
|
|
2017-10-16 00:21:00 +00:00
|
|
|
// Check for a hash mismatch.
|
|
|
|
$expected_song_hash = Song::getSongHash([
|
|
|
|
'artist' => $this->artist,
|
|
|
|
'title' => $this->title,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($media_mtime > $this->mtime
|
2018-02-09 20:56:04 +00:00
|
|
|
|| null === $this->song_id
|
2017-10-16 00:21:00 +00:00
|
|
|
|| $this->song_id != $expected_song_hash
|
|
|
|
|| $force) {
|
2017-07-29 07:18:54 +00:00
|
|
|
|
|
|
|
$this->mtime = $media_mtime;
|
|
|
|
|
|
|
|
// Load metadata from supported files.
|
2016-09-09 05:07:40 +00:00
|
|
|
$id3 = new \getID3();
|
2016-05-15 09:31:21 +00:00
|
|
|
|
2016-09-09 05:07:40 +00:00
|
|
|
$id3->option_md5_data = true;
|
|
|
|
$id3->option_md5_data_source = true;
|
|
|
|
$id3->encoding = 'UTF-8';
|
|
|
|
|
|
|
|
$file_info = $id3->analyze($media_path);
|
2016-05-15 09:31:21 +00:00
|
|
|
|
2017-07-29 07:18:54 +00:00
|
|
|
if (empty($file_info['error'])) {
|
|
|
|
$this->setLength($file_info['playtime_seconds']);
|
2016-05-15 09:31:21 +00:00
|
|
|
|
2017-07-29 07:18:54 +00:00
|
|
|
$tags_to_set = ['title', 'artist', 'album'];
|
|
|
|
if (!empty($file_info['tags'])) {
|
|
|
|
foreach ($file_info['tags'] as $tag_type => $tag_data) {
|
|
|
|
foreach ($tags_to_set as $tag) {
|
|
|
|
if (!empty($tag_data[$tag][0])) {
|
2017-10-16 00:21:00 +00:00
|
|
|
$this->{$tag} = mb_convert_encoding($tag_data[$tag][0], "UTF-8");
|
2017-07-29 07:18:54 +00:00
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
}
|
2017-11-04 02:13:41 +00:00
|
|
|
|
|
|
|
if (!empty($tag_data['unsynchronized_lyric'][0])) {
|
|
|
|
$this->lyrics = $tag_data['unsynchronized_lyric'][0];
|
|
|
|
}
|
2016-11-14 05:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 02:13:41 +00:00
|
|
|
|
|
|
|
if (!empty($file_info['comments']['picture'][0])) {
|
|
|
|
$picture = $file_info['comments']['picture'][0];
|
2017-11-04 06:09:49 +00:00
|
|
|
$this->setArt(imagecreatefromstring($picture['data']));
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 09:31:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-29 07:18:54 +00:00
|
|
|
// Attempt to derive title and artist from filename.
|
2017-07-30 05:24:41 +00:00
|
|
|
if (empty($this->title)) {
|
|
|
|
$filename = str_replace('_', ' ', $path_parts['filename']);
|
|
|
|
|
|
|
|
$string_parts = explode('-', $filename);
|
|
|
|
|
|
|
|
// If not normally delimited, return "text" only.
|
|
|
|
if (count($string_parts) == 1) {
|
|
|
|
$this->title = trim($filename);
|
|
|
|
$this->artist = '';
|
|
|
|
} else {
|
|
|
|
$this->title = trim(array_pop($string_parts));
|
|
|
|
$this->artist = trim(implode('-', $string_parts));
|
|
|
|
}
|
2017-07-29 07:18:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 05:24:41 +00:00
|
|
|
return [
|
|
|
|
'artist' => $this->artist,
|
|
|
|
'title' => $this->title,
|
|
|
|
];
|
2016-05-15 09:31:21 +00:00
|
|
|
}
|
2017-01-24 00:35:16 +00:00
|
|
|
|
2016-05-15 09:31:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:32:31 +00:00
|
|
|
/**
|
|
|
|
* Write modified metadata directly to the file as ID3 information.
|
|
|
|
*/
|
|
|
|
public function writeToFile()
|
|
|
|
{
|
|
|
|
$getID3 = new \getID3;
|
2017-01-24 00:35:16 +00:00
|
|
|
$getID3->setOption(['encoding' => 'UTF8']);
|
2016-09-21 23:32:31 +00:00
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
require_once(APP_INCLUDE_VENDOR . '/james-heinrich/getid3/getid3/write.php');
|
2016-09-21 23:32:31 +00:00
|
|
|
|
|
|
|
$tagwriter = new \getid3_writetags;
|
|
|
|
$tagwriter->filename = $this->getFullPath();
|
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
$tagwriter->tagformats = ['id3v1', 'id3v2.3'];
|
2016-09-21 23:32:31 +00:00
|
|
|
$tagwriter->overwrite_tags = true;
|
|
|
|
$tagwriter->tag_encoding = 'UTF8';
|
|
|
|
$tagwriter->remove_other_tags = true;
|
|
|
|
|
2017-01-24 00:35:16 +00:00
|
|
|
$tag_data = [
|
|
|
|
'title' => [$this->title],
|
|
|
|
'artist' => [$this->artist],
|
|
|
|
'album' => [$this->album],
|
|
|
|
];
|
2016-09-21 23:32:31 +00:00
|
|
|
|
2017-11-04 06:09:49 +00:00
|
|
|
if (is_resource($this->art)) {
|
|
|
|
$tag_data['attached_picture'][0] = [
|
|
|
|
'data' => stream_get_contents($this->art),
|
|
|
|
'picturetypeid' => 'image/jpeg',
|
|
|
|
'mime' => 'image/jpeg',
|
|
|
|
];
|
|
|
|
$tag_data['comments']['picture'][0] = $tag_data['attached_picture'][0];
|
|
|
|
}
|
|
|
|
|
2016-09-21 23:32:31 +00:00
|
|
|
$tagwriter->tag_data = $tag_data;
|
|
|
|
|
|
|
|
// write tags
|
2017-01-24 00:35:16 +00:00
|
|
|
if ($tagwriter->WriteTags()) {
|
2016-09-21 23:32:31 +00:00
|
|
|
$this->mtime = time();
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-14 05:38:59 +00:00
|
|
|
|
2017-07-29 07:18:54 +00:00
|
|
|
return false;
|
2017-01-24 00:17:50 +00:00
|
|
|
}
|
2017-08-04 23:48:24 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
public function getFullPath()
|
|
|
|
{
|
|
|
|
$media_base_dir = $this->station->getRadioMediaDir();
|
|
|
|
|
|
|
|
return $media_base_dir . '/' . $this->path;
|
|
|
|
}
|
|
|
|
|
2017-08-04 23:48:24 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the API version of the object/array.
|
|
|
|
*
|
|
|
|
* @return Api\Song
|
|
|
|
*/
|
2017-11-04 02:13:41 +00:00
|
|
|
public function api(\App\Url $url): Api\Song
|
2017-08-04 23:48:24 +00:00
|
|
|
{
|
|
|
|
$response = new Api\Song;
|
|
|
|
$response->id = (string)$this->song_id;
|
2017-08-17 18:28:48 +00:00
|
|
|
$response->text = $this->artist . ' - ' . $this->title;
|
2017-08-04 23:48:24 +00:00
|
|
|
$response->artist = (string)$this->artist;
|
|
|
|
$response->title = (string)$this->title;
|
|
|
|
|
2017-11-04 02:13:41 +00:00
|
|
|
$response->album = (string)$this->album;
|
|
|
|
$response->lyrics = (string)$this->lyrics;
|
|
|
|
|
2018-03-17 02:40:34 +00:00
|
|
|
$response->art = $url->named('api:stations:media:art', ['station' => $this->station_id, 'media_id' => $this->unique_id], true);
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2017-08-04 23:48:24 +00:00
|
|
|
return $response;
|
|
|
|
}
|
2014-02-21 09:25:10 +00:00
|
|
|
}
|