2015-08-31 16:19:23 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2015-10-24 03:22:14 +02:00
|
|
|
namespace Poniverse\Ponyfm\Commands;
|
2015-08-31 16:19:23 +02:00
|
|
|
|
2016-06-14 10:27:56 +02:00
|
|
|
use Auth;
|
2015-12-26 12:40:47 +01:00
|
|
|
use Carbon\Carbon;
|
2015-12-18 09:56:13 +01:00
|
|
|
use Config;
|
2016-06-14 10:27:56 +02:00
|
|
|
use Gate;
|
2015-12-18 09:56:13 +01:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
2015-12-26 12:40:47 +01:00
|
|
|
use Input;
|
2016-01-01 01:12:30 +01:00
|
|
|
use Poniverse\Ponyfm\Models\Track;
|
2016-06-14 10:27:56 +02:00
|
|
|
use Poniverse\Ponyfm\Models\User;
|
2016-02-15 14:06:06 +01:00
|
|
|
use Validator;
|
2015-08-31 16:19:23 +02:00
|
|
|
|
|
|
|
class UploadTrackCommand extends CommandBase
|
|
|
|
{
|
2015-12-18 09:56:13 +01:00
|
|
|
use DispatchesJobs;
|
|
|
|
|
2016-06-14 10:27:56 +02:00
|
|
|
private $_artist;
|
2015-09-10 13:15:19 +02:00
|
|
|
private $_allowLossy;
|
|
|
|
private $_allowShortTrack;
|
2015-12-26 12:40:47 +01:00
|
|
|
private $_customTrackSource;
|
|
|
|
private $_autoPublishByDefault;
|
2016-08-24 13:48:02 +02:00
|
|
|
private $_track;
|
|
|
|
private $_version;
|
|
|
|
private $_isReplacingTrack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
if ($this->_isReplacingTrack) {
|
|
|
|
return $this->_track && Gate::allows('edit', $this->_track);
|
|
|
|
} else {
|
|
|
|
return Gate::allows('create-track', $this->_artist);
|
|
|
|
}
|
|
|
|
}
|
2015-12-26 12:40:47 +01:00
|
|
|
|
2016-02-15 14:06:06 +01:00
|
|
|
/**
|
|
|
|
* UploadTrackCommand constructor.
|
|
|
|
*
|
|
|
|
* @param bool $allowLossy
|
|
|
|
* @param bool $allowShortTrack allow tracks shorter than 30 seconds
|
|
|
|
* @param string|null $customTrackSource value to set in the track's "source" field; if left blank, "direct_upload" is used
|
|
|
|
* @param bool $autoPublishByDefault
|
2016-08-24 13:48:02 +02:00
|
|
|
* @param int $version
|
|
|
|
* @param Track $track | null
|
2016-02-15 14:06:06 +01:00
|
|
|
*/
|
2016-06-14 10:27:56 +02:00
|
|
|
public function __construct(
|
|
|
|
bool $allowLossy = false,
|
|
|
|
bool $allowShortTrack = false,
|
|
|
|
string $customTrackSource = null,
|
2016-08-24 13:48:02 +02:00
|
|
|
bool $autoPublishByDefault = false,
|
|
|
|
int $version = 1,
|
|
|
|
$track = null
|
2016-06-14 10:27:56 +02:00
|
|
|
) {
|
|
|
|
$userSlug = Input::get('user_slug', null);
|
|
|
|
$this->_artist =
|
|
|
|
$userSlug !== null
|
|
|
|
? User::where('slug', $userSlug)->first()
|
|
|
|
: Auth::user();
|
|
|
|
|
2015-09-10 13:15:19 +02:00
|
|
|
$this->_allowLossy = $allowLossy;
|
|
|
|
$this->_allowShortTrack = $allowShortTrack;
|
2015-12-26 12:40:47 +01:00
|
|
|
$this->_customTrackSource = $customTrackSource;
|
|
|
|
$this->_autoPublishByDefault = $autoPublishByDefault;
|
2016-08-24 13:48:02 +02:00
|
|
|
$this->_version = $version;
|
|
|
|
$this->_track = $track;
|
|
|
|
$this->_isReplacingTrack = $this->_track !== null && $version > 1;
|
2015-08-31 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
* @return CommandResponse
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2016-02-16 10:12:59 +01:00
|
|
|
$trackFile = Input::file('track', null);
|
2016-08-24 13:48:02 +02:00
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
$coverFile = Input::file('cover', null);
|
|
|
|
}
|
2015-11-09 20:35:30 +01:00
|
|
|
|
|
|
|
if (null === $trackFile) {
|
2016-08-24 13:48:02 +02:00
|
|
|
if ($this->_isReplacingTrack) {
|
|
|
|
$this->_track->version_upload_status = Track::STATUS_ERROR;
|
|
|
|
$this->_track->update();
|
|
|
|
}
|
2015-11-09 20:35:30 +01:00
|
|
|
return CommandResponse::fail(['track' => ['You must upload an audio file!']]);
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:19:23 +02:00
|
|
|
$audio = \AudioCache::get($trackFile->getPathname());
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
$this->_track = new Track();
|
|
|
|
$this->_track->user_id = $this->_artist->id;
|
|
|
|
// The title set here is a placeholder; it'll be replaced by ParseTrackTagsCommand
|
|
|
|
// if the file contains a title tag.
|
|
|
|
$this->_track->title = Input::get('title', pathinfo($trackFile->getClientOriginalName(), PATHINFO_FILENAME));
|
|
|
|
// The duration/version of the track cannot be changed until the encoding is successful
|
|
|
|
$this->_track->duration = $audio->getDuration();
|
|
|
|
$this->_track->current_version = $this->_version;
|
|
|
|
$this->_track->save();
|
|
|
|
}
|
|
|
|
$this->_track->ensureDirectoryExists();
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2016-06-06 08:29:37 +02:00
|
|
|
if (!is_dir(Config::get('ponyfm.files_directory').'/tmp')) {
|
|
|
|
mkdir(Config::get('ponyfm.files_directory').'/tmp', 0755, true);
|
2016-04-03 18:55:53 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 08:29:37 +02:00
|
|
|
if (!is_dir(Config::get('ponyfm.files_directory').'/queued-tracks')) {
|
|
|
|
mkdir(Config::get('ponyfm.files_directory').'/queued-tracks', 0755, true);
|
2015-12-25 03:08:49 +01:00
|
|
|
}
|
2016-08-24 13:48:02 +02:00
|
|
|
$trackFile = $trackFile->move(Config::get('ponyfm.files_directory').'/queued-tracks', $this->_track->id . 'v' . $this->_version);
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2015-12-26 12:40:47 +01:00
|
|
|
$input = Input::all();
|
|
|
|
$input['track'] = $trackFile;
|
2016-08-24 13:48:02 +02:00
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
$input['cover'] = $coverFile;
|
|
|
|
}
|
2015-12-18 09:56:13 +01:00
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
$rules = [
|
2015-08-31 16:19:23 +02:00
|
|
|
'track' =>
|
|
|
|
'required|'
|
2016-02-15 11:21:43 +01:00
|
|
|
. ($this->_allowLossy
|
2016-02-15 21:16:04 +01:00
|
|
|
? 'audio_format:flac,alac,pcm,adpcm,aac,mp3,vorbis|'
|
|
|
|
: 'audio_format:flac,alac,pcm,adpcm|')
|
2015-09-10 13:15:19 +02:00
|
|
|
. ($this->_allowShortTrack ? '' : 'min_duration:30|')
|
2015-12-26 12:40:47 +01:00
|
|
|
. 'audio_channels:1,2',
|
2016-08-24 13:48:02 +02:00
|
|
|
];
|
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
array_push($rules, [
|
|
|
|
'cover' => 'image|mimes:png,jpeg|min_width:350|min_height:350',
|
|
|
|
'auto_publish' => 'boolean',
|
|
|
|
'title' => 'string',
|
|
|
|
'track_type_id' => 'exists:track_types,id',
|
|
|
|
'genre' => 'string',
|
|
|
|
'album' => 'string',
|
|
|
|
'track_number' => 'integer',
|
|
|
|
'released_at' => 'date_format:'.Carbon::ISO8601,
|
|
|
|
'description' => 'string',
|
|
|
|
'lyrics' => 'string',
|
|
|
|
'is_vocal' => 'boolean',
|
|
|
|
'is_explicit' => 'boolean',
|
|
|
|
'is_downloadable' => 'boolean',
|
|
|
|
'is_listed' => 'boolean',
|
|
|
|
'metadata' => 'json'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
$validator = \Validator::make($input, $rules);
|
2015-08-31 16:19:23 +02:00
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2016-08-24 13:48:02 +02:00
|
|
|
if ($this->_isReplacingTrack) {
|
|
|
|
$this->_track->version_upload_status = Track::STATUS_ERROR;
|
|
|
|
$this->_track->update();
|
|
|
|
} else {
|
|
|
|
$this->_track->delete();
|
|
|
|
}
|
2015-08-31 16:19:23 +02:00
|
|
|
return CommandResponse::fail($validator);
|
|
|
|
}
|
2016-08-24 13:48:02 +02:00
|
|
|
|
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
// If json_decode() isn't called here, Laravel will surround the JSON
|
|
|
|
// string with quotes when storing it in the database, which breaks things.
|
|
|
|
$this->_track->metadata = json_decode(Input::get('metadata', null));
|
|
|
|
}
|
|
|
|
$autoPublish = (bool)($input['auto_publish'] ?? $this->_autoPublishByDefault);
|
|
|
|
$this->_track->source = $this->_customTrackSource ?? 'direct_upload';
|
|
|
|
$this->_track->save();
|
|
|
|
|
|
|
|
if (!$this->_isReplacingTrack) {
|
|
|
|
// Parse any tags in the uploaded files.
|
|
|
|
$parseTagsCommand = new ParseTrackTagsCommand($this->_track, $trackFile, $input);
|
|
|
|
$result = $parseTagsCommand->execute();
|
|
|
|
if ($result->didFail()) {
|
|
|
|
if ($this->_isReplacingTrack) {
|
|
|
|
$this->_track->version_upload_status = Track::STATUS_ERROR;
|
|
|
|
$this->_track->update();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2015-12-27 16:31:54 +01:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:48:02 +02:00
|
|
|
$generateTrackFiles = new GenerateTrackFilesCommand($this->_track, $trackFile, $autoPublish, true, $this->_isReplacingTrack, $this->_version);
|
2016-02-16 10:12:59 +01:00
|
|
|
return $generateTrackFiles->execute();
|
2015-12-27 18:11:07 +01:00
|
|
|
}
|
2015-09-13 07:21:17 +02:00
|
|
|
}
|