2015-08-30 14:29:12 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2021-02-14 03:39:15 +01:00
|
|
|
* Copyright (C) 2015-2017 Feld0.
|
2015-10-25 06:17:45 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
namespace App\Models;
|
2015-08-31 14:35:47 +02:00
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Contracts\Commentable;
|
|
|
|
use App\Contracts\Favouritable;
|
|
|
|
use App\Contracts\Searchable;
|
|
|
|
use App\Exceptions\TrackFileNotFoundException;
|
|
|
|
use App\Models\ResourceLogItem;
|
|
|
|
use App\Traits\IndexedInElasticsearchTrait;
|
|
|
|
use App\Traits\SlugTrait;
|
2021-02-14 03:39:15 +01:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use Config;
|
|
|
|
use DB;
|
2015-08-31 14:35:47 +02:00
|
|
|
use Exception;
|
|
|
|
use External;
|
2021-02-14 03:39:15 +01:00
|
|
|
use Gate;
|
2015-08-31 14:35:47 +02:00
|
|
|
use getid3_writetags;
|
|
|
|
use Helpers;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-02-14 03:39:15 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-11-01 17:49:28 +01:00
|
|
|
use Illuminate\Support\Str;
|
2015-09-20 09:27:52 +02:00
|
|
|
use Log;
|
2015-11-24 12:07:43 +01:00
|
|
|
use Venturecraft\Revisionable\RevisionableTrait;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-01-01 01:36:08 +01:00
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* App\Models\Track.
|
2016-01-01 01:36:08 +01:00
|
|
|
*
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int $license_id
|
|
|
|
* @property int $genre_id
|
|
|
|
* @property int $track_type_id
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property string $title
|
|
|
|
* @property string $slug
|
|
|
|
* @property string $description
|
|
|
|
* @property string $lyrics
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property bool $is_vocal
|
|
|
|
* @property bool $is_explicit
|
|
|
|
* @property int $cover_id
|
|
|
|
* @property bool $is_downloadable
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property float $duration
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $play_count
|
|
|
|
* @property int $view_count
|
|
|
|
* @property int $download_count
|
|
|
|
* @property int $favourite_count
|
|
|
|
* @property int $comment_count
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property string $updated_at
|
|
|
|
* @property \Carbon\Carbon $deleted_at
|
|
|
|
* @property \Carbon\Carbon $published_at
|
|
|
|
* @property \Carbon\Carbon $released_at
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $album_id
|
|
|
|
* @property int $track_number
|
|
|
|
* @property bool $is_latest
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property string $hash
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property bool $is_listed
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property string $source
|
|
|
|
* @property string $original_tags
|
|
|
|
* @property string $metadata
|
2021-02-14 03:34:58 +01:00
|
|
|
* @property-read \App\Models\Genre $genre
|
|
|
|
* @property-read \App\Models\TrackType $trackType
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Favourite[] $favourites
|
|
|
|
* @property-read \App\Models\Image $cover
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ShowSong[] $showSongs
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ResourceUser[] $users
|
|
|
|
* @property-read \App\Models\User $user
|
|
|
|
* @property-read \App\Models\Album $album
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TrackFile[] $trackFiles
|
2016-01-01 01:36:08 +01:00
|
|
|
* @property-read mixed $year
|
|
|
|
* @property-read mixed $url
|
|
|
|
* @property-read mixed $download_directory
|
|
|
|
* @property-read mixed $status
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory
|
2021-02-14 03:34:58 +01:00
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track userDetails()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track published()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track listed()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track explicitFilter()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track withComments()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track mlpma()
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Activity[] $notifications
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Activity[] $activities
|
2017-11-28 17:24:32 +01:00
|
|
|
* @property int $current_version
|
|
|
|
* @property int|null $version_upload_status
|
2021-02-14 03:34:58 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TrackFile[] $trackFilesForAllVersions
|
2017-11-28 17:24:32 +01:00
|
|
|
* @method static bool|null forceDelete()
|
2021-02-14 03:34:58 +01:00
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track onlyTrashed()
|
2017-11-28 17:24:32 +01:00
|
|
|
* @method static bool|null restore()
|
2021-02-14 03:34:58 +01:00
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereAlbumId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereCommentCount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereCoverId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereCurrentVersion($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereDeletedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereDescription($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereDownloadCount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereDuration($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereFavouriteCount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereGenreId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereHash($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereIsDownloadable($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereIsExplicit($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereIsLatest($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereIsListed($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereIsVocal($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereLicenseId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereLyrics($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereMetadata($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereOriginalTags($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track wherePlayCount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track wherePublishedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereReleasedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereSlug($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereSource($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereTitle($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereTrackNumber($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereTrackTypeId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereVersionUploadStatus($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Track whereViewCount($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Track withoutTrashed()
|
2017-11-28 17:24:32 +01:00
|
|
|
* @mixin \Eloquent
|
2016-01-01 01:36:08 +01:00
|
|
|
*/
|
2016-05-27 21:12:40 +02:00
|
|
|
class Track extends Model implements Searchable, Commentable, Favouritable
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-01-16 05:10:20 +01:00
|
|
|
use SoftDeletes, IndexedInElasticsearchTrait;
|
2016-01-07 19:16:37 +01:00
|
|
|
|
|
|
|
protected $elasticsearchType = 'track';
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2021-02-14 19:01:29 +01:00
|
|
|
protected $dates = [
|
2021-02-14 19:01:59 +01:00
|
|
|
'published_at', 'released_at',
|
2021-02-14 19:01:29 +01:00
|
|
|
];
|
2015-12-26 12:40:47 +01:00
|
|
|
protected $hidden = ['original_tags', 'metadata'];
|
2015-11-09 20:35:30 +01:00
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'user_id' => 'integer',
|
|
|
|
'license_id' => 'integer',
|
2015-12-27 16:31:54 +01:00
|
|
|
'album_id' => 'integer',
|
|
|
|
'track_number' => 'integer',
|
2015-11-09 20:35:30 +01:00
|
|
|
'genre_id' => 'integer',
|
|
|
|
'track_type_id' => 'integer',
|
|
|
|
'is_vocal' => 'boolean',
|
|
|
|
'is_explicit' => 'boolean',
|
|
|
|
'cover_id' => 'integer',
|
|
|
|
'is_downloadable' => 'boolean',
|
|
|
|
'is_latest' => 'boolean',
|
|
|
|
'is_listed' => 'boolean',
|
2015-12-26 12:40:47 +01:00
|
|
|
'original_tags' => 'array',
|
|
|
|
'metadata' => 'array',
|
2015-11-09 20:35:30 +01:00
|
|
|
];
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
use SlugTrait {
|
|
|
|
SlugTrait::setTitleAttribute as setTitleAttributeSlug;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:07:43 +01:00
|
|
|
use RevisionableTrait;
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
// Used for the track's post-upload status. See UploadTrackCommand.
|
2015-12-18 09:56:13 +01:00
|
|
|
const STATUS_COMPLETE = 0;
|
|
|
|
const STATUS_PROCESSING = 1;
|
|
|
|
const STATUS_ERROR = 2;
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* All the information about how to encode files into Pony.fm's various formats.
|
2017-09-22 13:42:59 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public static $Formats = [
|
|
|
|
'FLAC' => [
|
|
|
|
'index' => 0,
|
2015-09-10 13:15:19 +02:00
|
|
|
'is_lossless' => true,
|
2015-08-30 14:29:12 +02:00
|
|
|
'extension' => 'flac',
|
|
|
|
'tag_format' => 'metaflac',
|
|
|
|
'tag_method' => 'updateTagsWithGetId3',
|
|
|
|
'mime_type' => 'audio/flac',
|
2021-02-14 03:39:15 +01:00
|
|
|
'command' => 'ffmpeg 2>&1 -y -i {$source} -map 0:a -map_metadata -1 -codec:a flac -aq 8 -f flac {$target}',
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'MP3' => [
|
|
|
|
'index' => 1,
|
2015-09-10 13:15:19 +02:00
|
|
|
'is_lossless' => false,
|
2015-08-30 14:29:12 +02:00
|
|
|
'extension' => 'mp3',
|
|
|
|
'tag_format' => 'id3v2.3',
|
|
|
|
'tag_method' => 'updateTagsWithGetId3',
|
|
|
|
'mime_type' => 'audio/mpeg',
|
2021-02-14 03:39:15 +01:00
|
|
|
'command' => 'ffmpeg 2>&1 -y -i {$source} -map 0:a -map_metadata -1 -codec:a libmp3lame -ab 320k -f mp3 {$target}',
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'OGG Vorbis' => [
|
|
|
|
'index' => 2,
|
2015-09-10 13:15:19 +02:00
|
|
|
'is_lossless' => false,
|
2015-08-30 14:29:12 +02:00
|
|
|
'extension' => 'ogg',
|
|
|
|
'tag_format' => 'vorbiscomment',
|
|
|
|
'tag_method' => 'updateTagsWithGetId3',
|
|
|
|
'mime_type' => 'audio/ogg',
|
2021-02-14 03:39:15 +01:00
|
|
|
'command' => 'ffmpeg 2>&1 -y -i {$source} -map 0:a -map_metadata -1 -codec:a libvorbis -aq 7 -f ogg {$target}',
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'AAC' => [
|
|
|
|
'index' => 3,
|
2015-09-10 13:15:19 +02:00
|
|
|
'is_lossless' => false,
|
2015-08-30 14:29:12 +02:00
|
|
|
'extension' => 'm4a',
|
|
|
|
'tag_format' => 'AtomicParsley',
|
|
|
|
'tag_method' => 'updateTagsWithAtomicParsley',
|
|
|
|
'mime_type' => 'audio/mp4',
|
2021-02-14 03:39:15 +01:00
|
|
|
'command' => 'ffmpeg 2>&1 -y -i {$source} -map 0:a -map_metadata -1 -codec:a libfaac -ab 256k -f mp4 {$target}',
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'ALAC' => [
|
|
|
|
'index' => 4,
|
2015-09-10 13:15:19 +02:00
|
|
|
'is_lossless' => true,
|
2015-08-30 14:29:12 +02:00
|
|
|
'extension' => 'alac.m4a',
|
|
|
|
'tag_format' => 'AtomicParsley',
|
2015-10-26 20:48:55 +01:00
|
|
|
'tag_method' => 'updateTagsWithAtomicParsley',
|
|
|
|
'mime_type' => 'audio/mp4',
|
2021-02-14 03:39:15 +01:00
|
|
|
'command' => 'ffmpeg 2>&1 -y -i {$source} -map 0:a -map_metadata -1 -codec:a alac {$target}',
|
2015-10-26 20:48:55 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2015-11-03 05:28:39 +01:00
|
|
|
/**
|
|
|
|
* `TrackFiles` in these formats, with the exception of any master files, will
|
|
|
|
* be generated upon user request and kept around temporarily.
|
|
|
|
*
|
|
|
|
* After updating this array, run `php artisan rebuild:track-cache` to bring
|
|
|
|
* the track store into a consistent state.
|
|
|
|
*
|
|
|
|
* The strings in this array must match keys in the `Track::$Formats` array.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-10-26 20:48:55 +01:00
|
|
|
public static $CacheableFormats = [
|
2015-11-01 17:49:28 +01:00
|
|
|
'OGG Vorbis',
|
|
|
|
'ALAC',
|
2021-02-14 03:39:15 +01:00
|
|
|
'AAC',
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Formats in this file are treated specially by Pony.fm as lossless when,
|
|
|
|
* for example, generating playlist and album downloads that contain a mix
|
|
|
|
* of lossy and lossless tracks.
|
|
|
|
*
|
|
|
|
* The strings in this array must match keys in the `Track::$Formats` array.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-07-10 16:00:59 +02:00
|
|
|
public static $LosslessFormats = [
|
|
|
|
'FLAC',
|
2021-02-14 03:39:15 +01:00
|
|
|
'ALAC',
|
2016-07-10 16:00:59 +02:00
|
|
|
];
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Prepares a query builder object with typical fields used for a track
|
|
|
|
* "tile" around the site and eager-loaded relations that are almost always
|
|
|
|
* relevant.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public static function summary()
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
return self::select(
|
|
|
|
'tracks.id',
|
|
|
|
'title',
|
|
|
|
'user_id',
|
|
|
|
'slug',
|
|
|
|
'is_vocal',
|
|
|
|
'is_explicit',
|
|
|
|
'created_at',
|
2015-11-01 17:49:28 +01:00
|
|
|
'published_at',
|
2016-09-30 00:26:31 +02:00
|
|
|
'duration',
|
|
|
|
'is_downloadable',
|
|
|
|
'genre_id',
|
|
|
|
'track_type_id',
|
|
|
|
'cover_id',
|
|
|
|
'album_id',
|
|
|
|
'comment_count',
|
|
|
|
'download_count',
|
|
|
|
'view_count',
|
|
|
|
'play_count',
|
|
|
|
'favourite_count'
|
|
|
|
)
|
2016-05-28 21:29:06 +02:00
|
|
|
->with('user', 'cover', 'album');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* If a user is currently logged in, this scope eager-loads them.
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function scopeUserDetails($query)
|
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
|
|
|
$query->with([
|
2016-09-30 00:26:31 +02:00
|
|
|
'users' => function ($query) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$query->whereUserId(Auth::user()->id);
|
2021-02-14 03:39:15 +01:00
|
|
|
},
|
2015-08-30 14:29:12 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Limits the query scope to published tracks.
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function scopePublished($query)
|
|
|
|
{
|
|
|
|
$query->whereNotNull('published_at');
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Limits the query scope to listed tracks.
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function scopeListed($query)
|
|
|
|
{
|
|
|
|
$query->whereIsListed(true);
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Applies the NSFW filter: only allow explicit tracks to come back for
|
|
|
|
* this query if a user is logged in and has opted in to them.
|
|
|
|
* @param $query
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function scopeExplicitFilter($query)
|
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! Auth::check() || ! Auth::user()->can_see_explicit_content) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$query->whereIsExplicit(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Eager-loads track comments with this query.
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function scopeWithComments($query)
|
|
|
|
{
|
|
|
|
$query->with([
|
2016-09-30 00:26:31 +02:00
|
|
|
'comments' => function ($query) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$query->with('user');
|
2021-02-14 03:39:15 +01:00
|
|
|
},
|
2015-08-30 14:29:12 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:47:11 +01:00
|
|
|
/**
|
|
|
|
* Limits results to MLP Music Archive tracks.
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
*/
|
|
|
|
public function scopeMlpma($query)
|
|
|
|
{
|
|
|
|
$query->join('mlpma_tracks', 'tracks.id', '=', 'mlpma_tracks.track_id');
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
/**
|
2017-09-22 13:42:59 +02:00
|
|
|
* The (messy) query used to generate the list of popular tracks on PFM's
|
|
|
|
* homepage.
|
|
|
|
*
|
2021-02-14 03:39:15 +01:00
|
|
|
* @param int $count the number of tracks to return
|
2017-09-22 13:42:59 +02:00
|
|
|
* @param bool $allowExplicit whether to include explicit tracks in the results
|
|
|
|
* @param int $skip currently unused as of 2017-09-22
|
2016-05-28 21:53:18 +02:00
|
|
|
* @return array
|
2016-01-18 02:21:39 +01:00
|
|
|
*/
|
2016-09-30 23:21:17 +02:00
|
|
|
public static function popular($count, $allowExplicit = false, $skip = 0)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-10-06 23:11:19 +02:00
|
|
|
$trackData = Cache::remember(
|
2016-09-30 00:26:31 +02:00
|
|
|
'popular_tracks'.$count.'-'.($allowExplicit ? 'explicit' : 'safe'),
|
2021-02-14 19:01:31 +01:00
|
|
|
300,
|
2016-09-30 23:21:17 +02:00
|
|
|
function () use ($allowExplicit, $count, $skip) {
|
2016-10-06 23:11:19 +02:00
|
|
|
/*$query = static
|
2015-08-30 14:29:12 +02:00
|
|
|
::published()
|
|
|
|
->listed()
|
2016-09-30 00:26:31 +02:00
|
|
|
->join(
|
|
|
|
DB::raw('(
|
2016-06-27 04:20:10 +02:00
|
|
|
SELECT "track_id"
|
|
|
|
FROM "resource_log_items"
|
|
|
|
WHERE track_id IS NOT NULL AND log_type = 3 AND "created_at" > now() - INTERVAL \'1\' DAY
|
|
|
|
) ranged_plays'),
|
2016-09-30 00:26:31 +02:00
|
|
|
'tracks.id',
|
|
|
|
'=',
|
|
|
|
'ranged_plays.track_id'
|
|
|
|
)
|
2016-06-27 04:20:10 +02:00
|
|
|
->groupBy(['id', 'track_id'])
|
2015-08-30 14:29:12 +02:00
|
|
|
->orderBy('plays', 'desc')
|
2016-09-30 23:21:17 +02:00
|
|
|
->skip($skip)
|
2015-08-30 14:29:12 +02:00
|
|
|
->take($count);
|
|
|
|
|
|
|
|
if (!$allowExplicit) {
|
2016-06-27 04:20:10 +02:00
|
|
|
$query->where('is_explicit', false);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($query->get(['*', DB::raw('count(*) as plays')]) as $track) {
|
|
|
|
$results[] = $track->id;
|
2016-10-06 23:11:19 +02:00
|
|
|
}*/
|
|
|
|
|
|
|
|
$explicitFilter = '
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT id, is_explicit FROM tracks
|
|
|
|
WHERE track_id = id AND is_explicit = TRUE
|
|
|
|
)';
|
|
|
|
|
|
|
|
if ($allowExplicit) {
|
|
|
|
$explicitFilter = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryText = '
|
|
|
|
SELECT track_id,
|
|
|
|
SUM(CASE WHEN log_type = 1 THEN 0.1
|
|
|
|
WHEN log_type = 3 THEN 1
|
|
|
|
WHEN log_type = 2 THEN 2
|
|
|
|
ELSE 0 END) AS weight
|
|
|
|
FROM "resource_log_items"
|
|
|
|
WHERE track_id IS NOT NULL AND log_type IS NOT NULL AND "created_at" > now() - INTERVAL \'1\' DAY
|
|
|
|
'.$explicitFilter.'
|
|
|
|
GROUP BY track_id
|
|
|
|
ORDER BY weight DESC
|
2016-10-06 23:46:31 +02:00
|
|
|
LIMIT '.$count;
|
2016-10-06 23:11:19 +02:00
|
|
|
|
|
|
|
$countQuery = DB::select(DB::raw($queryText));
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
foreach ($countQuery as $track) {
|
|
|
|
$results[] = [
|
|
|
|
'id' => $track->track_id,
|
2021-02-14 03:39:15 +01:00
|
|
|
'weight' => $track->weight,
|
2016-10-06 23:11:19 +02:00
|
|
|
];
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
2016-06-27 04:20:10 +02:00
|
|
|
}
|
|
|
|
);
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-10-06 23:11:19 +02:00
|
|
|
$trackIds = [];
|
|
|
|
$trackWeights = [];
|
|
|
|
|
|
|
|
foreach ($trackData as $track) {
|
|
|
|
$trackIds[] = $track['id'];
|
|
|
|
$trackWeights[$track['id']] = $track['weight'];
|
|
|
|
}
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! count($trackIds)) {
|
2015-08-30 14:29:12 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
$tracks = self::summary()
|
2015-08-30 14:29:12 +02:00
|
|
|
->userDetails()
|
|
|
|
->explicitFilter()
|
|
|
|
->published()
|
|
|
|
->with('user', 'genre', 'cover', 'album', 'album.user')
|
|
|
|
->whereIn('id', $trackIds);
|
|
|
|
|
|
|
|
$processed = [];
|
|
|
|
foreach ($tracks->get() as $track) {
|
2021-02-14 03:39:15 +01:00
|
|
|
$trackModel = self::mapPublicTrackSummary($track);
|
2016-10-06 23:11:19 +02:00
|
|
|
$trackModel['weight'] = $trackWeights[$track->id];
|
|
|
|
$processed[] = $trackModel;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
usort($processed, function ($a, $b) {
|
2016-10-06 23:11:19 +02:00
|
|
|
return $a['weight'] <=> $b['weight'];
|
|
|
|
});
|
|
|
|
|
|
|
|
$processed = array_reverse($processed);
|
2015-12-31 13:20:04 +01:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
return $processed;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Brings together all the data displayed on a track page.
|
|
|
|
* The data structure this returns is what the web API spits out.
|
|
|
|
*
|
|
|
|
* @param Track $track
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public static function mapPublicTrackShow(self $track)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$returnValue = self::mapPublicTrackSummary($track);
|
|
|
|
$returnValue['description'] = $track->description;
|
|
|
|
$returnValue['lyrics'] = $track->lyrics;
|
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
|
|
|
|
foreach ($track->comments as $comment) {
|
|
|
|
$comments[] = Comment::mapPublic($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$returnValue['comments'] = $comments;
|
2021-02-14 03:39:15 +01:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$formats = [];
|
|
|
|
|
|
|
|
foreach ($track->trackFiles as $trackFile) {
|
|
|
|
$formats[] = [
|
|
|
|
'name' => $trackFile->format,
|
|
|
|
'extension' => $trackFile->extension,
|
|
|
|
'url' => $trackFile->url,
|
2015-10-29 15:36:13 +01:00
|
|
|
'size' => $trackFile->size,
|
2021-02-14 03:39:15 +01:00
|
|
|
'isCacheable' => (bool) $trackFile->is_cacheable,
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$returnValue['share'] = [
|
2015-12-20 16:07:36 +01:00
|
|
|
'url' => action('TracksController@getShortlink', ['id' => $track->id]),
|
2016-01-18 02:21:39 +01:00
|
|
|
'html' => '<iframe src="'.action('TracksController@getEmbed', ['id' => $track->id]).'" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>',
|
|
|
|
'bbcode' => '[url='.$track->url.'][img]'.$track->getCoverUrl().'[/img][/url]',
|
2021-02-14 03:39:15 +01:00
|
|
|
'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text='.$track->title.' by '.$track->user->display_name.' on Pony.fm',
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
$returnValue['share']['tumblrUrl'] = 'http://www.tumblr.com/share/video?embed='.urlencode($returnValue['share']['html']).'&caption='.urlencode($track->title);
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
$returnValue['formats'] = $formats;
|
|
|
|
|
|
|
|
return $returnValue;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Brings together all the data shown on "track tiles" throughout the site.
|
|
|
|
* The data structure this returns is what the web API spits out.
|
|
|
|
*
|
|
|
|
* @param Track $track
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public static function mapPublicTrackSummary(self $track)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => 0,
|
|
|
|
'plays' => 0,
|
2021-02-14 03:39:15 +01:00
|
|
|
'downloads' => 0,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_favourited' => false,
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if (Auth::check() && $track->users->count()) {
|
|
|
|
$userRow = $track->users[0];
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'views' => (int) $userRow->view_count,
|
|
|
|
'plays' => (int) $userRow->play_count,
|
2015-08-30 14:29:12 +02:00
|
|
|
'downloads' => $userRow->download_count,
|
|
|
|
],
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_favourited' => (bool) $userRow->is_favourited,
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-01-02 07:29:36 +01:00
|
|
|
$data = [
|
2016-01-18 02:21:39 +01:00
|
|
|
'id' => (int) $track->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'title' => $track->title,
|
|
|
|
'user' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'id' => (int) $track->user->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'name' => $track->user->display_name,
|
2021-02-14 03:39:15 +01:00
|
|
|
'url' => $track->user->url,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'stats' => [
|
2016-01-18 02:21:39 +01:00
|
|
|
'views' => (int) $track->view_count,
|
|
|
|
'plays' => (int) $track->play_count,
|
|
|
|
'downloads' => (int) $track->download_count,
|
|
|
|
'comments' => (int) $track->comment_count,
|
2021-02-14 03:39:15 +01:00
|
|
|
'favourites' => (int) $track->favourite_count,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'url' => $track->url,
|
|
|
|
'slug' => $track->slug,
|
2016-01-04 17:42:30 +01:00
|
|
|
'is_vocal' => $track->is_vocal,
|
|
|
|
'is_explicit' => $track->is_explicit,
|
|
|
|
'is_downloadable' => $track->is_downloadable,
|
|
|
|
'is_published' => $track->isPublished(),
|
|
|
|
'published_at' => $track->isPublished() ? $track->published_at->format('c') : null,
|
2015-08-30 14:29:12 +02:00
|
|
|
'duration' => $track->duration,
|
|
|
|
'genre' => $track->genre != null
|
|
|
|
?
|
|
|
|
[
|
2016-01-18 02:21:39 +01:00
|
|
|
'id' => (int) $track->genre->id,
|
2015-08-30 14:29:12 +02:00
|
|
|
'slug' => $track->genre->slug,
|
2017-09-22 14:10:26 +02:00
|
|
|
'name' => $track->genre->name,
|
|
|
|
'url' => $track->genre->url,
|
2015-08-30 14:29:12 +02:00
|
|
|
] : null,
|
|
|
|
'track_type_id' => $track->track_type_id,
|
|
|
|
'covers' => [
|
|
|
|
'thumbnail' => $track->getCoverUrl(Image::THUMBNAIL),
|
|
|
|
'small' => $track->getCoverUrl(Image::SMALL),
|
2015-12-13 14:42:37 +01:00
|
|
|
'normal' => $track->getCoverUrl(Image::NORMAL),
|
2021-02-14 03:39:15 +01:00
|
|
|
'original' => $track->getCoverUrl(Image::ORIGINAL),
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'streams' => [
|
2016-10-01 18:59:30 +02:00
|
|
|
'mp3' => $track->getStreamUrl('MP3'),
|
2021-02-14 03:39:15 +01:00
|
|
|
'aac' => (! Config::get('app.debug') || is_file($track->getFileFor('AAC'))) ? $track->getStreamUrl('AAC') : null,
|
|
|
|
'ogg' => (Config::get('app.debug') || is_file($track->getFileFor('OGG Vorbis'))) ? $track->getStreamUrl('OGG Vorbis') : null,
|
2015-08-30 14:29:12 +02:00
|
|
|
],
|
|
|
|
'user_data' => $userData,
|
|
|
|
'permissions' => [
|
2016-02-25 20:00:12 +01:00
|
|
|
'delete' => Gate::allows('delete', $track),
|
2021-02-14 03:39:15 +01:00
|
|
|
'edit' => Gate::allows('edit', $track),
|
|
|
|
],
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
2021-01-02 07:29:36 +01:00
|
|
|
|
|
|
|
if ($track->album_id != null) {
|
|
|
|
$data['album'] = [
|
|
|
|
'title' => $track->album->title,
|
|
|
|
'url' => $track->album->url,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Brings together the data about a track used by the track editor.
|
|
|
|
* The data structure this returns is what the web API spits out.
|
|
|
|
*
|
|
|
|
* @param Track $track
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public static function mapPrivateTrackShow(self $track)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
$showSongs = [];
|
|
|
|
foreach ($track->showSongs as $showSong) {
|
|
|
|
$showSongs[] = ['id' => $showSong->id, 'title' => $showSong->title];
|
|
|
|
}
|
|
|
|
|
|
|
|
$returnValue = self::mapPrivateTrackSummary($track);
|
|
|
|
$returnValue['album_id'] = $track->album_id;
|
|
|
|
$returnValue['show_songs'] = $showSongs;
|
2016-02-14 11:53:27 +01:00
|
|
|
$returnValue['cover_id'] = $track->cover_id;
|
2015-08-30 14:29:12 +02:00
|
|
|
$returnValue['real_cover_url'] = $track->getCoverUrl(Image::NORMAL);
|
|
|
|
$returnValue['cover_url'] = $track->hasCover() ? $track->getCoverUrl(Image::NORMAL) : null;
|
2016-02-14 10:29:02 +01:00
|
|
|
$returnValue['released_at'] = $track->released_at ? $track->released_at->toDateString() : null;
|
2015-08-30 14:29:12 +02:00
|
|
|
$returnValue['lyrics'] = $track->lyrics;
|
|
|
|
$returnValue['description'] = $track->description;
|
2021-02-14 03:39:15 +01:00
|
|
|
$returnValue['is_downloadable'] = ! $track->isPublished() ? true : (bool) $track->is_downloadable;
|
2015-08-30 14:29:12 +02:00
|
|
|
$returnValue['license_id'] = $track->license_id != null ? $track->license_id : 3;
|
2016-05-16 00:22:29 +02:00
|
|
|
$returnValue['username'] = User::whereId($track->user_id)->first()->username;
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-11-21 01:42:43 +01:00
|
|
|
// Seasonal
|
2016-11-28 01:19:16 +01:00
|
|
|
if (Playlist::where('user_id', 22549)->first()) {
|
|
|
|
$returnValue['hwc_submit'] = Playlist::where('user_id', 22549)->first()->tracks()->get()->contains($track);
|
|
|
|
}
|
2016-11-21 01:42:43 +01:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
return $returnValue;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Brings together the data shown for a "track tile" in a user's account
|
|
|
|
* area (where they'd manage unlisted and unpublished tracks and the admin
|
|
|
|
* area.
|
|
|
|
*
|
|
|
|
* The data structure this returns is what the web API spits out.
|
|
|
|
* @param Track $track
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-02-14 03:39:15 +01:00
|
|
|
public static function mapPrivateTrackSummary(self $track)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $track->id,
|
|
|
|
'title' => $track->title,
|
|
|
|
'user_id' => $track->user_id,
|
|
|
|
'slug' => $track->slug,
|
|
|
|
'is_vocal' => $track->is_vocal,
|
|
|
|
'is_explicit' => $track->is_explicit,
|
|
|
|
'is_downloadable' => $track->is_downloadable,
|
|
|
|
'is_published' => $track->isPublished(),
|
2015-12-26 16:20:31 +01:00
|
|
|
'created_at' => $track->created_at->format('c'),
|
2015-12-26 23:11:17 +01:00
|
|
|
'published_at' => $track->published_at ? $track->published_at->format('c') : null,
|
2015-08-30 14:29:12 +02:00
|
|
|
'duration' => $track->duration,
|
|
|
|
'genre_id' => $track->genre_id,
|
|
|
|
'track_type_id' => $track->track_type_id,
|
|
|
|
'cover_url' => $track->getCoverUrl(Image::SMALL),
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_listed' => (bool) $track->is_listed,
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $table = 'tracks';
|
|
|
|
|
|
|
|
public function genre()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsTo(Genre::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function trackType()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsTo(TrackType::class, 'track_type_id');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:12:40 +02:00
|
|
|
public function comments():HasMany
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->hasMany(Comment::class)->orderBy('created_at', 'desc');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:12:40 +02:00
|
|
|
public function favourites():HasMany
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->hasMany(Favourite::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cover()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsTo(Image::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showSongs()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsToMany(ShowSong::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function users()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->hasMany(ResourceUser::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsTo(User::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function album()
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->belongsTo(Album::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function trackFiles()
|
2016-08-24 13:48:02 +02:00
|
|
|
{
|
|
|
|
return $this->hasMany(TrackFile::class)->where('version', $this->current_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function trackFilesForAllVersions()
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->hasMany(TrackFile::class);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
public function trackFilesForVersion(int $version)
|
|
|
|
{
|
2017-11-28 17:24:32 +01:00
|
|
|
return $this->trackFilesForAllVersions()->where('track_files.version', $version);
|
2016-08-24 13:48:02 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:12:40 +02:00
|
|
|
public function notifications()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Activity::class, 'notification_type');
|
|
|
|
}
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function activities():MorphMany
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
return $this->morphMany(Activity::class, 'resource');
|
|
|
|
}
|
|
|
|
|
2015-09-10 12:27:04 +02:00
|
|
|
public function getYearAttribute()
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2015-09-10 12:30:48 +02:00
|
|
|
return date('Y', strtotime($this->getReleaseDate()));
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setTitleAttribute($value)
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
$this->setTitleAttributeSlug($value);
|
2015-08-30 14:29:12 +02:00
|
|
|
$this->updateHash();
|
|
|
|
}
|
|
|
|
|
2015-11-10 05:08:37 +01:00
|
|
|
/**
|
|
|
|
* Returns the size of this track's file in the given format.
|
|
|
|
*
|
|
|
|
* @param $formatName
|
|
|
|
* @return int filesize in bytes
|
|
|
|
* @throws TrackFileNotFoundException
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getFilesize($formatName)
|
|
|
|
{
|
2016-08-29 16:36:38 +02:00
|
|
|
$trackFile = $this->trackFiles()->where('format', $formatName)->first();
|
2015-11-10 05:08:37 +01:00
|
|
|
|
|
|
|
if ($trackFile) {
|
|
|
|
return (int) $trackFile->filesize;
|
|
|
|
} else {
|
|
|
|
throw new TrackFileNotFoundException();
|
|
|
|
}
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given user can view this track.
|
|
|
|
*
|
|
|
|
* @param $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function canView($user)
|
|
|
|
{
|
2016-05-11 18:41:49 +02:00
|
|
|
if ($this->isPublished() || $user->hasRole('admin')) {
|
2015-08-30 14:29:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user_id == $user->id;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Magic method to create a "url" attribute on Track objects that contains
|
|
|
|
* the track's canonical URL.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('TracksController@getTrack', ['id' => $this->id, 'slug' => $this->slug]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Magic method to create a "downloadDirectory" attribute on Track objects
|
|
|
|
* that contains the directory path it should show up in inside downloaded
|
|
|
|
* TrackCollection zip files.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getDownloadDirectoryAttribute()
|
|
|
|
{
|
|
|
|
if ($this->album) {
|
2016-01-18 02:21:39 +01:00
|
|
|
return $this->user->display_name.'/'.$this->album->title;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user->display_name;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns a track's release date, heuristically generating a plausible one
|
|
|
|
* if it is unknown.
|
|
|
|
*
|
|
|
|
* @return \Carbon\Carbon|string
|
|
|
|
*/
|
2015-09-10 12:30:48 +02:00
|
|
|
public function getReleaseDate()
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2015-09-10 12:30:48 +02:00
|
|
|
if ($this->released_at !== null) {
|
|
|
|
return $this->released_at;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-09-10 12:30:48 +02:00
|
|
|
if ($this->published_at !== null) {
|
|
|
|
return Str::limit($this->published_at, 10, '');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Str::limit($this->attributes['created_at'], 10, '');
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Ensures that the numbered directory this track's files would be stored in
|
|
|
|
* server-side exists, creating it if needed.
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function ensureDirectoryExists()
|
|
|
|
{
|
|
|
|
$destination = $this->getDirectory();
|
|
|
|
umask(0);
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! is_dir($destination)) {
|
2015-09-10 13:50:04 +02:00
|
|
|
mkdir($destination, 0777, true);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* @return bool True if the track has an associated cover file.
|
|
|
|
*/
|
|
|
|
public function hasCover() : bool
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
return $this->cover_id != null;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* @return bool True if the track has been published and not deleted.
|
|
|
|
*/
|
|
|
|
public function isPublished() : bool
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
|
|
|
return $this->published_at != null && $this->deleted_at == null;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the TrackFile object that represents the highest-fidelity version
|
|
|
|
* of this track, which is used as the source file for transcoding to other
|
|
|
|
* formats.
|
|
|
|
*
|
|
|
|
* @return TrackFile
|
|
|
|
*/
|
2016-08-29 16:43:05 +02:00
|
|
|
protected function getMasterTrackFile() : TrackFile
|
2016-07-11 12:25:45 +02:00
|
|
|
{
|
2016-08-29 16:36:38 +02:00
|
|
|
return $this->trackFiles()->where('is_master', true)->first();
|
2016-07-11 12:25:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* The key in `$Formats` above for the master file's format.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-11 13:43:10 +02:00
|
|
|
public function getMasterFormatName() : string
|
|
|
|
{
|
|
|
|
return $this->getMasterTrackFile()->format;
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* @return bool True if this track's master file is a lossy one.
|
|
|
|
*/
|
2016-07-11 12:25:45 +02:00
|
|
|
public function isMasterLossy() : bool
|
|
|
|
{
|
|
|
|
return $this->getMasterTrackFile()->isLossy();
|
2016-07-10 16:00:59 +02:00
|
|
|
}
|
2016-11-21 01:42:43 +01:00
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the URL to this track's cover art in the given size.
|
|
|
|
*
|
|
|
|
* @param int $type one of the image size constants in the `Image` class
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getCoverUrl($type = Image::NORMAL)
|
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! $this->hasCover()) {
|
2015-08-30 14:29:12 +02:00
|
|
|
if ($this->album_id != null) {
|
|
|
|
return $this->album->getCoverUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->user->getAvatarUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->cover->getUrl($type);
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the "stream" URL for this track in the given format. This is to
|
|
|
|
* be used by the on-site player.
|
|
|
|
*
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
2017-10-29 01:44:08 +02:00
|
|
|
* @param string $apiClientId if a URL is being requested for the third-party
|
|
|
|
* API, this should be set to the requesting app's
|
|
|
|
* client ID.
|
2017-09-22 13:42:59 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-10-29 01:44:08 +02:00
|
|
|
public function getStreamUrl(string $format = 'MP3', string $apiClientId = null)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2017-10-29 01:44:08 +02:00
|
|
|
return action('TracksController@getStream',
|
|
|
|
[
|
|
|
|
'id' => $this->id,
|
2021-02-14 03:39:15 +01:00
|
|
|
'extension' => self::$Formats[$format]['extension'],
|
2017-10-29 01:44:08 +02:00
|
|
|
] + ($apiClientId !== null ? ['api_client_id' => $apiClientId] : [])
|
|
|
|
);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the absolute path to the server-side directory that this track's
|
|
|
|
* audio files are stored in.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getDirectory()
|
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
$dir = (string) (floor($this->id / 100) * 100);
|
2015-08-30 14:29:12 +02:00
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
return \Config::get('ponyfm.files_directory').'/tracks/'.$dir;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'deleted_at', 'published_at', 'released_at'];
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the server-side filename (not path) that a current TrackFile in
|
|
|
|
* the given format would have.
|
|
|
|
*
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
* @return string a filename
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getFilenameFor(string $format) : string
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! isset(self::$Formats[$format])) {
|
2015-08-30 14:29:12 +02:00
|
|
|
throw new Exception("$format is not a valid format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = self::$Formats[$format];
|
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
return "{$this->id}-v{$this->current_version}.{$format['extension']}";
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the filename that this track would have in the given format when
|
|
|
|
* downloaded by a user.
|
|
|
|
*
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getDownloadFilenameFor(string $format) : string
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! isset(self::$Formats[$format])) {
|
2015-08-30 14:29:12 +02:00
|
|
|
throw new Exception("$format is not a valid format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = self::$Formats[$format];
|
|
|
|
|
|
|
|
return "{$this->title}.{$format['extension']}";
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:21:39 +01:00
|
|
|
/**
|
2017-09-22 13:42:59 +02:00
|
|
|
* Returns the absolute path to this track's file in a given format.
|
|
|
|
*
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
* @return string absolute path to a track file
|
2016-05-28 21:53:18 +02:00
|
|
|
* @throws Exception
|
2016-01-18 02:21:39 +01:00
|
|
|
*/
|
2017-09-22 13:42:59 +02:00
|
|
|
public function getFileFor(string $format) : string
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! isset(self::$Formats[$format])) {
|
2015-08-30 14:29:12 +02:00
|
|
|
throw new Exception("$format is not a valid format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = self::$Formats[$format];
|
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
return "{$this->getDirectory()}/{$this->id}-v{$this->current_version}.{$format['extension']}";
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
/**
|
|
|
|
* Returns the path to the "temporary" master file uploaded by the user.
|
|
|
|
* This file is used during the upload process to generate the actual master
|
|
|
|
* file stored by Pony.fm.
|
|
|
|
*
|
2016-08-24 13:48:02 +02:00
|
|
|
* @param int $version
|
2015-12-18 09:56:13 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function getTemporarySourceFileForVersion(int $version):string
|
|
|
|
{
|
2016-08-24 13:48:02 +02:00
|
|
|
return Config::get('ponyfm.files_directory').'/queued-tracks/'.$this->id.'v'.$version;
|
2015-12-18 09:56:13 +01:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the URL to download a track file from in the given format.
|
|
|
|
*
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getUrlFor(string $format) : string
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! isset(self::$Formats[$format])) {
|
2015-08-30 14:29:12 +02:00
|
|
|
throw new Exception("$format is not a valid format!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = self::$Formats[$format];
|
|
|
|
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('TracksController@getDownload', ['id' => $this->id, 'extension' => $format['extension']]);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 09:56:13 +01:00
|
|
|
/**
|
2017-09-22 13:42:59 +02:00
|
|
|
* @return int one of the Track::STATUS_* values, indicating whether this track is currently being processed
|
2015-12-18 09:56:13 +01:00
|
|
|
*/
|
2017-09-22 13:42:59 +02:00
|
|
|
public function getStatusAttribute():int
|
2016-09-30 00:26:31 +02:00
|
|
|
{
|
|
|
|
return $this->trackFiles->reduce(function ($carry, $trackFile) {
|
2016-01-18 02:21:39 +01:00
|
|
|
if ((int) $trackFile->status === TrackFile::STATUS_PROCESSING_ERROR) {
|
2015-12-18 09:56:13 +01:00
|
|
|
return static::STATUS_ERROR;
|
2016-09-30 00:26:31 +02:00
|
|
|
} elseif ($carry !== static::STATUS_ERROR &&
|
2016-01-04 23:55:20 +01:00
|
|
|
in_array($trackFile->status, [TrackFile::STATUS_PROCESSING, TrackFile::STATUS_PROCESSING_PENDING])) {
|
2015-12-18 09:56:13 +01:00
|
|
|
return static::STATUS_PROCESSING;
|
2021-02-14 03:39:15 +01:00
|
|
|
} elseif (! in_array($carry, [static::STATUS_ERROR, static::STATUS_PROCESSING, TrackFile::STATUS_PROCESSING_PENDING]) &&
|
2015-12-18 14:33:30 +01:00
|
|
|
(int) $trackFile->status === TrackFile::STATUS_NOT_BEING_PROCESSED
|
2015-12-18 09:56:13 +01:00
|
|
|
) {
|
|
|
|
return static::STATUS_COMPLETE;
|
|
|
|
} else {
|
|
|
|
return $carry;
|
|
|
|
}
|
|
|
|
}, static::STATUS_COMPLETE);
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Updates this track's hash of the artist name and track title.
|
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function updateHash()
|
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
$this->hash = md5(Helpers::sanitizeInputForHashing($this->user->display_name).' - '.Helpers::sanitizeInputForHashing($this->title));
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Ensures that the metadata for this track's file in the given format is
|
|
|
|
* up to date.
|
|
|
|
* @param string $trackFileFormat one of the format keys from the `$Formats` array OR
|
|
|
|
* 'all' to indicate the operation should be performed on all formats
|
|
|
|
*/
|
2015-11-01 17:49:28 +01:00
|
|
|
public function updateTags($trackFileFormat = 'all')
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2015-11-01 17:49:28 +01:00
|
|
|
if ($trackFileFormat === 'all') {
|
|
|
|
foreach ($this->trackFiles as $trackFile) {
|
|
|
|
$this->updateTagsForTrackFile($trackFile);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$trackFile = $this->trackFiles()->where('format', $trackFileFormat)->firstOrFail();
|
|
|
|
$this->updateTagsForTrackFile($trackFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Runs the command to update the given TrackFile's metadata.
|
|
|
|
*
|
|
|
|
* @param TrackFile $trackFile
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
private function updateTagsForTrackFile(TrackFile $trackFile)
|
|
|
|
{
|
2015-11-01 17:49:28 +01:00
|
|
|
$trackFile->touch();
|
2015-09-10 13:15:19 +02:00
|
|
|
|
2015-11-01 17:49:28 +01:00
|
|
|
if (\File::exists($trackFile->getFile())) {
|
2015-09-10 13:15:19 +02:00
|
|
|
$format = $trackFile->format;
|
|
|
|
$data = self::$Formats[$format];
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
$this->{$data['tag_method']}($format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Writes a TrackFile's tags using AtomicParsley. This is useful for MP4
|
|
|
|
* files (AAC and ALAC). This function is called from updateTagsForTrackFile.
|
|
|
|
*
|
|
|
|
* @noinspection PhpUnusedPrivateMethodInspection
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
*/
|
|
|
|
private function updateTagsWithAtomicParsley(string $format)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
$command = 'AtomicParsley "'.$this->getFileFor($format).'" ';
|
|
|
|
$command .= '--title '.escapeshellarg($this->title).' ';
|
|
|
|
$command .= '--artist '.escapeshellarg($this->user->display_name).' ';
|
|
|
|
$command .= '--year "'.$this->year.'" ';
|
|
|
|
$command .= '--genre '.escapeshellarg($this->genre != null ? $this->genre->name : '').' ';
|
|
|
|
$command .= '--copyright '.escapeshellarg('© '.$this->year.' '.$this->user->display_name).' ';
|
|
|
|
$command .= '--comment "'.'Downloaded from: https://pony.fm/'.'" ';
|
|
|
|
$command .= '--encodingTool "'.'Pony.fm - https://pony.fm/'.'" ';
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
if ($this->album_id !== null) {
|
2016-01-18 02:21:39 +01:00
|
|
|
$command .= '--album '.escapeshellarg($this->album->title).' ';
|
|
|
|
$command .= '--tracknum '.$this->track_number.' ';
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->cover !== null) {
|
2016-01-18 02:21:39 +01:00
|
|
|
$command .= '--artwork '.$this->cover->getFile(Image::ORIGINAL).' ';
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$command .= '--overWrite';
|
|
|
|
|
|
|
|
External::execute($command);
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Writes a TrackFile's tags using getId3(). This is useful for MP3, FLAC,
|
|
|
|
* and OGG Vorbis files. This function is called from updateTagsForTrackFile.
|
|
|
|
*
|
|
|
|
* @noinspection PhpUnusedPrivateMethodInspection
|
|
|
|
* @param string $format one of the format keys from the `$Formats` array
|
|
|
|
*/
|
|
|
|
private function updateTagsWithGetId3(string $format)
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
require_once app_path().'/Library/getid3/getid3/getid3.php';
|
|
|
|
require_once app_path().'/Library/getid3/getid3/write.php';
|
2015-08-30 14:29:12 +02:00
|
|
|
$tagWriter = new getid3_writetags;
|
|
|
|
|
|
|
|
$tagWriter->overwrite_tags = true;
|
|
|
|
$tagWriter->tag_encoding = 'UTF-8';
|
|
|
|
$tagWriter->remove_other_tags = true;
|
|
|
|
|
|
|
|
$tagWriter->tag_data = [
|
|
|
|
'title' => [$this->title],
|
|
|
|
'artist' => [$this->user->display_name],
|
2016-01-18 02:21:39 +01:00
|
|
|
'year' => [''.$this->year],
|
2015-09-10 12:27:04 +02:00
|
|
|
'genre' => [$this->genre != null ? $this->genre->name : ''],
|
2015-08-30 14:29:12 +02:00
|
|
|
'comment' => ['Downloaded from: https://pony.fm/'],
|
2016-01-18 02:21:39 +01:00
|
|
|
'copyright' => ['© '.$this->year.' '.$this->user->display_name],
|
2015-08-30 14:29:12 +02:00
|
|
|
'publisher' => ['Pony.fm - https://pony.fm/'],
|
|
|
|
'encoded_by' => ['https://pony.fm/'],
|
2015-10-25 03:35:37 +01:00
|
|
|
// 'url_artist' => [$this->user->url],
|
|
|
|
// 'url_source' => [$this->url],
|
|
|
|
// 'url_file' => [$this->url],
|
2021-02-14 03:39:15 +01:00
|
|
|
'url_publisher' => ['https://pony.fm/'],
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->album_id !== null) {
|
|
|
|
$tagWriter->tag_data['album'] = [$this->album->title];
|
|
|
|
$tagWriter->tag_data['track'] = [$this->track_number];
|
|
|
|
}
|
|
|
|
|
2015-09-10 13:30:59 +02:00
|
|
|
if ($format == 'MP3' && $this->cover_id != null && is_file($this->cover->getFile())) {
|
2015-08-30 14:29:12 +02:00
|
|
|
$tagWriter->tag_data['attached_picture'][0] = [
|
2016-01-06 14:52:09 +01:00
|
|
|
'data' => file_get_contents($this->cover->getFile(Image::ORIGINAL)),
|
2015-08-30 14:29:12 +02:00
|
|
|
'picturetypeid' => 2,
|
|
|
|
'description' => 'cover',
|
2021-02-14 03:39:15 +01:00
|
|
|
'mime' => $this->cover->mime,
|
2015-08-30 14:29:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tagWriter->filename = $this->getFileFor($format);
|
|
|
|
$tagWriter->tagformats = [self::$Formats[$format]['tag_format']];
|
|
|
|
|
|
|
|
if ($tagWriter->WriteTags()) {
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! empty($tagWriter->warnings)) {
|
2016-09-30 00:26:31 +02:00
|
|
|
Log::warning('Track #'.$this->id.': There were some warnings:<br />'.implode(
|
|
|
|
'<br /><br />',
|
|
|
|
$tagWriter->warnings
|
|
|
|
));
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-30 00:26:31 +02:00
|
|
|
Log::error('Track #'.$this->id.': Failed to write tags!<br />'.implode(
|
|
|
|
'<br /><br />',
|
|
|
|
$tagWriter->errors
|
|
|
|
));
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
private function getCacheKey($key) : string
|
2015-08-30 14:29:12 +02:00
|
|
|
{
|
2016-01-18 02:21:39 +01:00
|
|
|
return 'track-'.$this->id.'-'.$key;
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
2016-01-07 19:16:37 +01:00
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Marks this track and associated notification records as deleted.
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function delete()
|
|
|
|
{
|
2016-07-10 03:16:25 +02:00
|
|
|
DB::transaction(function () {
|
|
|
|
$this->activities()->delete();
|
|
|
|
parent::delete();
|
|
|
|
});
|
|
|
|
}
|
2016-01-15 07:20:28 +01:00
|
|
|
|
2016-01-17 11:33:58 +01:00
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* {@inheritdoc}
|
2016-01-17 11:33:58 +01:00
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function shouldBeIndexed():bool
|
|
|
|
{
|
2016-01-17 11:33:58 +01:00
|
|
|
return $this->is_listed &&
|
|
|
|
$this->published_at !== null &&
|
2021-02-14 03:39:15 +01:00
|
|
|
! $this->trashed();
|
2016-01-17 11:33:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* {@inheritdoc}
|
2016-01-17 11:33:58 +01:00
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function toElasticsearch():array
|
|
|
|
{
|
2016-01-07 19:16:37 +01:00
|
|
|
return [
|
|
|
|
'title' => $this->title,
|
|
|
|
'artist' => $this->user->display_name,
|
|
|
|
'published_at' => $this->published_at ? $this->published_at->toIso8601String() : null,
|
|
|
|
'genre' => $this->genre->name,
|
|
|
|
'track_type' => $this->trackType->title,
|
2021-02-14 03:39:15 +01:00
|
|
|
'show_songs' => $this->showSongs->pluck('title'),
|
2016-01-07 19:16:37 +01:00
|
|
|
];
|
|
|
|
}
|
2016-05-27 21:12:40 +02:00
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the word used to identify this "type" of content in notifications.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function getResourceType():string
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
return 'track';
|
|
|
|
}
|
2016-08-24 13:48:02 +02:00
|
|
|
|
2017-09-22 13:42:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the next version after this track's current one.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNextVersion() : int
|
2016-08-24 13:48:02 +02:00
|
|
|
{
|
|
|
|
return $this->current_version + 1;
|
|
|
|
}
|
2015-09-12 23:56:09 +02:00
|
|
|
}
|