Pony.fm/app/Track.php

703 lines
22 KiB
PHP
Raw Normal View History

2015-08-30 14:29:12 +02:00
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Peter Deltchev
*
* 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/>.
*/
namespace Poniverse\Ponyfm;
2015-08-31 14:35:47 +02:00
2015-11-01 17:49:28 +01:00
use Auth;
use Cache;
use Config;
use DB;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Poniverse\Ponyfm\Traits\SlugTrait;
2015-08-31 14:35:47 +02:00
use Exception;
use External;
use getid3_writetags;
use Helpers;
2015-08-30 14:29:12 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2015-11-01 17:49:28 +01:00
use Illuminate\Support\Str;
use Log;
use URL;
2015-08-30 14:29:12 +02:00
class Track extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
use SlugTrait {
SlugTrait::setTitleAttribute as setTitleAttributeSlug;
}
public static $Formats = [
'FLAC' => [
'index' => 0,
'is_lossless' => true,
2015-08-30 14:29:12 +02:00
'extension' => 'flac',
'tag_format' => 'metaflac',
'tag_method' => 'updateTagsWithGetId3',
'mime_type' => 'audio/flac',
'command' => 'ffmpeg 2>&1 -y -i {$source} -acodec flac -aq 8 -f flac {$target}'
],
'MP3' => [
'index' => 1,
'is_lossless' => false,
2015-08-30 14:29:12 +02:00
'extension' => 'mp3',
'tag_format' => 'id3v2.3',
'tag_method' => 'updateTagsWithGetId3',
'mime_type' => 'audio/mpeg',
'command' => 'ffmpeg 2>&1 -y -i {$source} -acodec libmp3lame -ab 320k -f mp3 {$target}'
],
'OGG Vorbis' => [
'index' => 2,
'is_lossless' => false,
2015-08-30 14:29:12 +02:00
'extension' => 'ogg',
'tag_format' => 'vorbiscomment',
'tag_method' => 'updateTagsWithGetId3',
'mime_type' => 'audio/ogg',
'command' => 'ffmpeg 2>&1 -y -i {$source} -acodec libvorbis -aq 7 -f ogg {$target}'
],
'AAC' => [
'index' => 3,
'is_lossless' => false,
2015-08-30 14:29:12 +02:00
'extension' => 'm4a',
'tag_format' => 'AtomicParsley',
'tag_method' => 'updateTagsWithAtomicParsley',
'mime_type' => 'audio/mp4',
'command' => 'ffmpeg 2>&1 -y -i {$source} -acodec libfaac -ab 256k -f mp4 {$target}'
],
'ALAC' => [
'index' => 4,
'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',
'command' => 'ffmpeg 2>&1 -y -i {$source} -acodec alac {$target}'
],
];
/**
* `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',
'AAC'
2015-08-30 14:29:12 +02:00
];
public static function summary()
{
2015-11-01 17:49:28 +01:00
return self::select('tracks.id', 'title', 'user_id', 'slug', 'is_vocal', 'is_explicit', 'created_at',
'published_at',
2015-08-30 14:29:12 +02:00
'duration', 'is_downloadable', 'genre_id', 'track_type_id', 'cover_id', 'album_id', 'comment_count',
'download_count', 'view_count', 'play_count', 'favourite_count');
}
public function scopeUserDetails($query)
{
if (Auth::check()) {
$query->with([
'users' => function ($query) {
$query->whereUserId(Auth::user()->id);
}
]);
}
}
public function scopePublished($query)
{
$query->whereNotNull('published_at');
}
public function scopeListed($query)
{
$query->whereIsListed(true);
}
public function scopeExplicitFilter($query)
{
if (!Auth::check() || !Auth::user()->can_see_explicit_content) {
$query->whereIsExplicit(false);
}
}
public function scopeWithComments($query)
{
$query->with([
'comments' => function ($query) {
$query->with('user');
}
]);
}
/**
* Limits results to MLP Music Archive tracks.
*
* @param $query
*/
public function scopeMlpma($query)
{
$query->join('mlpma_tracks', 'tracks.id', '=', 'mlpma_tracks.track_id');
}
2015-08-30 14:29:12 +02:00
public static function popular($count, $allowExplicit = false)
{
$trackIds = Cache::remember('popular_tracks' . $count . '-' . ($allowExplicit ? 'explicit' : 'safe'), 5,
function () use ($allowExplicit, $count) {
$query = static
::published()
->listed()
->join(DB::raw('
2015-10-25 03:35:37 +01:00
( SELECT `track_id`, `created_at`
FROM `resource_log_items`
WHERE track_id IS NOT NULL AND log_type = 3 AND `created_at` > now() - INTERVAL 1 DAY
) AS ranged_plays'),
2015-08-30 14:29:12 +02:00
'tracks.id', '=', 'ranged_plays.track_id')
->groupBy('id')
->orderBy('plays', 'desc')
->take($count);
if (!$allowExplicit) {
$query->whereIsExplicit(false);
}
$results = [];
foreach ($query->get(['*', DB::raw('count(*) as plays')]) as $track) {
$results[] = $track->id;
}
return $results;
});
if (!count($trackIds)) {
return [];
}
$tracks = Track::summary()
->userDetails()
->explicitFilter()
->published()
->with('user', 'genre', 'cover', 'album', 'album.user')
->whereIn('id', $trackIds);
$processed = [];
foreach ($tracks->get() as $track) {
$processed[] = Track::mapPublicTrackSummary($track);
}
return $processed;
}
public static function mapPublicTrackShow(Track $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;
if ($track->album_id != null) {
$returnValue['album'] = [
'title' => $track->album->title,
'url' => $track->album->url,
];
}
$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,
'isCacheable' => (bool) $trackFile->is_cacheable
2015-08-30 14:29:12 +02:00
];
}
$returnValue['share'] = [
'url' => URL::to('/t' . $track->id),
'html' => '<iframe src="' . URL::to('t' . $track->id . '/embed') . '" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>',
'bbcode' => '[url=' . $track->url . '][img]' . $track->getCoverUrl() . '[/img][/url]',
'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $track->title . ' by ' . $track->user->display_name . ' on Pony.fm'
];
$returnValue['share']['tumblrUrl'] = 'http://www.tumblr.com/share/video?embed=' . urlencode($returnValue['share']['html']) . '&caption=' . urlencode($track->title);
$returnValue['formats'] = $formats;
return $returnValue;
}
public static function mapPublicTrackSummary(Track $track)
2015-08-30 14:29:12 +02:00
{
$userData = [
'stats' => [
'views' => 0,
'plays' => 0,
'downloads' => 0
],
'is_favourited' => false
];
if (Auth::check() && $track->users->count()) {
$userRow = $track->users[0];
$userData = [
'stats' => [
2015-11-01 17:49:28 +01:00
'views' => (int)$userRow->view_count,
'plays' => (int)$userRow->play_count,
2015-08-30 14:29:12 +02:00
'downloads' => $userRow->download_count,
],
2015-11-01 17:49:28 +01:00
'is_favourited' => (bool)$userRow->is_favourited
2015-08-30 14:29:12 +02:00
];
}
return [
2015-11-01 17:49:28 +01:00
'id' => (int)$track->id,
2015-08-30 14:29:12 +02:00
'title' => $track->title,
'user' => [
2015-11-01 17:49:28 +01:00
'id' => (int)$track->user->id,
2015-08-30 14:29:12 +02:00
'name' => $track->user->display_name,
'url' => $track->user->url
],
'stats' => [
2015-11-01 17:49:28 +01:00
'views' => (int)$track->view_count,
'plays' => (int)$track->play_count,
'downloads' => (int)$track->download_count,
'comments' => (int)$track->comment_count,
'favourites' => (int)$track->favourite_count
2015-08-30 14:29:12 +02:00
],
'url' => $track->url,
'slug' => $track->slug,
2015-11-01 17:49:28 +01:00
'is_vocal' => (bool)$track->is_vocal,
'is_explicit' => (bool)$track->is_explicit,
'is_downloadable' => (bool)$track->is_downloadable,
'is_published' => (bool)$track->isPublished(),
2015-08-30 14:29:12 +02:00
'published_at' => $track->published_at,
'duration' => $track->duration,
'genre' => $track->genre != null
?
[
2015-11-01 17:49:28 +01:00
'id' => (int)$track->genre->id,
2015-08-30 14:29:12 +02:00
'slug' => $track->genre->slug,
'name' => $track->genre->name
] : null,
'track_type_id' => $track->track_type_id,
'covers' => [
'thumbnail' => $track->getCoverUrl(Image::THUMBNAIL),
'small' => $track->getCoverUrl(Image::SMALL),
'normal' => $track->getCoverUrl(Image::NORMAL)
],
'streams' => [
'mp3' => $track->getStreamUrl('MP3'),
'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
],
'user_data' => $userData,
'permissions' => [
'delete' => Auth::check() && Auth::user()->id == $track->user_id,
'edit' => Auth::check() && Auth::user()->id == $track->user_id
]
];
}
public static function mapPrivateTrackShow(Track $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;
$returnValue['real_cover_url'] = $track->getCoverUrl(Image::NORMAL);
$returnValue['cover_url'] = $track->hasCover() ? $track->getCoverUrl(Image::NORMAL) : null;
$returnValue['released_at'] = $track->released_at;
$returnValue['lyrics'] = $track->lyrics;
$returnValue['description'] = $track->description;
$returnValue['is_downloadable'] = !$track->isPublished() ? true : (bool)$track->is_downloadable;
$returnValue['license_id'] = $track->license_id != null ? $track->license_id : 3;
return $returnValue;
}
public static function mapPrivateTrackSummary(Track $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(),
'created_at' => $track->created_at,
'published_at' => $track->published_at,
'duration' => $track->duration,
'genre_id' => $track->genre_id,
'track_type_id' => $track->track_type_id,
'cover_url' => $track->getCoverUrl(Image::SMALL),
'is_listed' => !!$track->is_listed
];
}
protected $table = 'tracks';
public function genre()
{
return $this->belongsTo('Poniverse\Ponyfm\Genre');
2015-08-30 14:29:12 +02:00
}
public function trackType()
{
return $this->belongsTo('Poniverse\Ponyfm\TrackType', 'track_type_id');
2015-08-30 14:29:12 +02:00
}
public function comments()
{
return $this->hasMany('Poniverse\Ponyfm\Comment')->orderBy('created_at', 'desc');
2015-08-30 14:29:12 +02:00
}
public function favourites()
{
return $this->hasMany('Poniverse\Ponyfm\Favourite');
2015-08-30 14:29:12 +02:00
}
public function cover()
{
return $this->belongsTo('Poniverse\Ponyfm\Image');
2015-08-30 14:29:12 +02:00
}
public function showSongs()
{
return $this->belongsToMany('Poniverse\Ponyfm\ShowSong');
2015-08-30 14:29:12 +02:00
}
public function users()
{
return $this->hasMany('Poniverse\Ponyfm\ResourceUser');
2015-08-30 14:29:12 +02:00
}
public function user()
{
return $this->belongsTo('Poniverse\Ponyfm\User');
2015-08-30 14:29:12 +02:00
}
public function album()
{
return $this->belongsTo('Poniverse\Ponyfm\Album');
2015-08-30 14:29:12 +02:00
}
public function trackFiles()
{
return $this->hasMany('Poniverse\Ponyfm\TrackFile');
2015-08-30 14:29:12 +02:00
}
public function getYearAttribute()
2015-08-30 14:29:12 +02:00
{
return date('Y', strtotime($this->getReleaseDate()));
2015-08-30 14:29:12 +02:00
}
public function setTitleAttribute($value)
{
$this->setTitleAttributeSlug($value);;
$this->updateHash();
}
/**
* 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)
{
$trackFile = $this->trackFiles()->where('format', $formatName)->first();
if ($trackFile) {
return (int) $trackFile->filesize;
} else {
throw new TrackFileNotFoundException();
}
2015-08-30 14:29:12 +02:00
}
public function canView($user)
{
if ($this->isPublished()) {
return true;
}
return $this->user_id == $user->id;
}
public function getUrlAttribute()
{
return URL::to('/tracks/' . $this->id . '-' . $this->slug);
}
public function getDownloadDirectoryAttribute()
{
if ($this->album) {
return $this->user->display_name . '/' . $this->album->title;
}
return $this->user->display_name;
}
public function getReleaseDate()
2015-08-30 14:29:12 +02:00
{
if ($this->released_at !== null) {
return $this->released_at;
2015-08-30 14:29:12 +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, '');
}
public function ensureDirectoryExists()
{
$destination = $this->getDirectory();
umask(0);
if (!is_dir($destination)) {
mkdir($destination, 0777, true);
2015-08-30 14:29:12 +02:00
}
}
public function hasCover()
{
return $this->cover_id != null;
}
public function isPublished()
{
return $this->published_at != null && $this->deleted_at == null;
}
public function getCoverUrl($type = Image::NORMAL)
{
if (!$this->hasCover()) {
if ($this->album_id != null) {
return $this->album->getCoverUrl($type);
}
return $this->user->getAvatarUrl($type);
}
return $this->cover->getUrl($type);
}
public function getStreamUrl($format = 'MP3')
{
return URL::to('/t' . $this->id . '/stream.' . self::$Formats[$format]['extension']);
}
public function getDirectory()
{
$dir = (string)(floor($this->id / 100) * 100);
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'];
}
public function getFilenameFor($format)
{
if (!isset(self::$Formats[$format])) {
throw new Exception("$format is not a valid format!");
}
$format = self::$Formats[$format];
return "{$this->id}.{$format['extension']}";
}
public function getDownloadFilenameFor($format)
{
if (!isset(self::$Formats[$format])) {
throw new Exception("$format is not a valid format!");
}
$format = self::$Formats[$format];
return "{$this->title}.{$format['extension']}";
}
public function getFileFor($format)
{
if (!isset(self::$Formats[$format])) {
throw new Exception("$format is not a valid format!");
}
$format = self::$Formats[$format];
return "{$this->getDirectory()}/{$this->id}.{$format['extension']}";
}
public function getUrlFor($format)
{
if (!isset(self::$Formats[$format])) {
throw new Exception("$format is not a valid format!");
}
$format = self::$Formats[$format];
return URL::to('/t' . $this->id . '/dl.' . $format['extension']);
}
public function updateHash()
{
$this->hash = md5(Helpers::sanitizeInputForHashing($this->user->display_name) . ' - ' . Helpers::sanitizeInputForHashing($this->title));
}
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);
}
}
private function updateTagsForTrackFile($trackFile) {
$trackFile->touch();
2015-11-01 17:49:28 +01:00
if (\File::exists($trackFile->getFile())) {
$format = $trackFile->format;
$data = self::$Formats[$format];
2015-08-30 14:29:12 +02:00
$this->{$data['tag_method']}($format);
}
}
/** @noinspection PhpUnusedPrivateMethodInspection */
private function updateTagsWithAtomicParsley($format)
{
$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 : '') . ' ';
2015-08-30 14:29:12 +02:00
$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) {
$command .= '--album ' . escapeshellarg($this->album->title) . ' ';
$command .= '--tracknum ' . $this->track_number . ' ';
}
if ($this->cover !== null) {
$command .= '--artwork ' . $this->cover->getFile() . ' ';
2015-08-30 14:29:12 +02:00
}
$command .= '--overWrite';
External::execute($command);
}
/** @noinspection PhpUnusedPrivateMethodInspection */
private function updateTagsWithGetId3($format)
{
2015-09-12 23:43:01 +02: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],
'year' => ['' . $this->year],
'genre' => [$this->genre != null ? $this->genre->name : ''],
2015-08-30 14:29:12 +02:00
'comment' => ['Downloaded from: https://pony.fm/'],
'copyright' => ['© ' . $this->year . ' ' . $this->user->display_name],
'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],
2015-08-30 14:29:12 +02:00
'url_publisher' => ['https://pony.fm/']
];
if ($this->album_id !== null) {
$tagWriter->tag_data['album'] = [$this->album->title];
$tagWriter->tag_data['track'] = [$this->track_number];
}
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] = [
'data' => file_get_contents($this->cover->getFile()),
2015-08-30 14:29:12 +02:00
'picturetypeid' => 2,
'description' => 'cover',
'mime' => 'image/png'
];
}
$tagWriter->filename = $this->getFileFor($format);
$tagWriter->tagformats = [self::$Formats[$format]['tag_format']];
if ($tagWriter->WriteTags()) {
if (!empty($tagWriter->warnings)) {
2015-11-01 17:49:28 +01: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 {
2015-11-01 17:49:28 +01:00
Log::error('Track #' . $this->id . ': Failed to write tags!<br />' . implode('<br /><br />',
$tagWriter->errors));
2015-08-30 14:29:12 +02:00
}
}
private function getCacheKey($key)
{
return 'track-' . $this->id . '-' . $key;
}
}