Esempio n. 1
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $slug = Str::slug($this->_newName);
     $rules = ['name' => 'required|unique:genres,name,' . $this->_genre->id . ',id,deleted_at,NULL|max:50', 'slug' => 'required|unique:genres,slug,' . $this->_genre->id . ',id,deleted_at,NULL'];
     $validator = Validator::make(['name' => $this->_newName, 'slug' => $slug], $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     $this->_genre->name = $this->_newName;
     $this->_genre->slug = $slug;
     $this->_genre->save();
     $this->dispatch(new UpdateTagsForRenamedGenre($this->_genre));
     return CommandResponse::succeed(['message' => 'Genre renamed!']);
 }
Esempio n. 2
0
 public function getIndex()
 {
     $this->authorize('access-admin-area');
     $genres = Genre::with(['trackCountRelation' => function ($query) {
         $query->withTrashed();
     }])->orderBy('name', 'asc')->get();
     return Response::json(['genres' => $genres->toArray()], 200);
 }
Esempio n. 3
0
 /**
  * @throws \Exception
  * @return CommandResponse
  */
 public function execute()
 {
     $slug = Str::slug($this->_genreName);
     $rules = ['name' => 'required|unique:genres,name,NULL,id,deleted_at,NULL|max:50', 'slug' => 'required|unique:genres,slug,NULL,id,deleted_at,NULL'];
     $validator = Validator::make(['name' => $this->_genreName, 'slug' => $slug], $rules);
     if ($validator->fails()) {
         return CommandResponse::fail($validator);
     }
     Genre::create(['name' => $this->_genreName, 'slug' => $slug]);
     return CommandResponse::succeed(['message' => 'Genre created!']);
 }
Esempio n. 4
0
 public function getAll()
 {
     return \Response::json(['licenses' => License::all()->toArray(), 'genres' => Genre::select('genres.*', DB::raw('(SELECT COUNT(id) FROM tracks WHERE tracks.genre_id = genres.id AND tracks.published_at IS NOT NULL) AS track_count'))->orderBy('name')->get()->toArray(), 'track_types' => TrackType::select('track_types.*', DB::raw('(SELECT COUNT(id) FROM tracks WHERE tracks.track_type_id = track_types.id AND tracks.published_at IS NOT NULL) AS track_count'))->where('id', '!=', TrackType::UNCLASSIFIED_TRACK)->get()->toArray(), 'show_songs' => ShowSong::select('title', 'id', 'slug', DB::raw('(SELECT COUNT(tracks.id) FROM show_song_track INNER JOIN tracks ON tracks.id = show_song_track.track_id WHERE show_song_track.show_song_id = show_songs.id AND tracks.published_at IS NOT NULL) AS track_count'))->get()->toArray()], 200);
 }
Esempio n. 5
0
 /**
  * Execute the console command.
  *
  * @return void
  */
 public function handle()
 {
     pcntl_signal(SIGINT, [$this, 'handleInterrupt']);
     $mlpmaPath = Config::get('ponyfm.files_directory') . '/mlpma';
     $tmpPath = Config::get('ponyfm.files_directory') . '/tmp';
     if (!File::exists($tmpPath)) {
         File::makeDirectory($tmpPath);
     }
     $UNKNOWN_GENRE = Genre::firstOrCreate(['name' => 'Unknown', 'slug' => 'unknown']);
     $this->comment('Enumerating MLP Music Archive source files...');
     $files = File::allFiles($mlpmaPath);
     $this->info(sizeof($files) . ' files found!');
     $this->comment('Enumerating artists...');
     $artists = File::directories($mlpmaPath);
     $this->info(sizeof($artists) . ' artists found!');
     $this->comment('Importing tracks...');
     $totalFiles = sizeof($files);
     $fileToStartAt = (int) $this->option('startAt') - 1;
     $this->comment("Skipping {$fileToStartAt} files..." . PHP_EOL);
     $files = array_slice($files, $fileToStartAt);
     $this->currentFile = $fileToStartAt;
     foreach ($files as $file) {
         $this->currentFile++;
         pcntl_signal_dispatch();
         if ($this->isInterrupted) {
             break;
         }
         $this->comment('[' . $this->currentFile . '/' . $totalFiles . '] Importing track [' . $file->getFilename() . ']...');
         if (in_array($file->getExtension(), $this->ignoredExtensions)) {
             $this->comment('This is not an audio file! Skipping...' . PHP_EOL);
             continue;
         }
         // Has this track already been imported?
         $importedTrack = DB::table('mlpma_tracks')->where('filename', '=', $file->getFilename())->first();
         if ($importedTrack) {
             $this->comment('This track has already been imported! Skipping...' . PHP_EOL);
             continue;
         }
         //==========================================================================================================
         // Extract the original tags.
         //==========================================================================================================
         $getId3 = new getID3();
         // all tags read by getID3, including the cover art
         $allTags = $getId3->analyze($file->getPathname());
         // tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
         $rawTags = [];
         // normalized tags used by Pony.fm
         $parsedTags = [];
         if (Str::lower($file->getExtension()) === 'mp3') {
             list($parsedTags, $rawTags) = $this->getId3Tags($allTags);
         } elseif (Str::lower($file->getExtension()) === 'm4a') {
             list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
         } elseif (Str::lower($file->getExtension()) === 'ogg') {
             list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
         } elseif (Str::lower($file->getExtension()) === 'flac') {
             list($parsedTags, $rawTags) = $this->getVorbisTags($allTags);
         } elseif (Str::lower($file->getExtension()) === 'wav') {
             list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
         }
         //==========================================================================================================
         // Determine the release date.
         //==========================================================================================================
         $modifiedDate = Carbon::createFromTimeStampUTC(File::lastModified($file->getPathname()));
         $taggedYear = $parsedTags['year'];
         $this->info('Modification year: ' . $modifiedDate->year);
         $this->info('Tagged year: ' . $taggedYear);
         if ($taggedYear !== null && $modifiedDate->year === $taggedYear) {
             $releasedAt = $modifiedDate;
         } elseif ($taggedYear !== null && Str::length((string) $taggedYear) !== 4) {
             $this->error('This track\'s tagged year makes no sense! Using the track\'s last modified date...');
             $releasedAt = $modifiedDate;
         } elseif ($taggedYear !== null && $modifiedDate->year !== $taggedYear) {
             $this->error('Release years don\'t match! Using the tagged year...');
             $releasedAt = Carbon::create($taggedYear);
         } else {
             // $taggedYear is null
             $this->error('This track isn\'t tagged with its release year! Using the track\'s last modified date...');
             $releasedAt = $modifiedDate;
         }
         // This is later used by the classification/publishing script to determine the publication date.
         $parsedTags['released_at'] = $releasedAt->toDateTimeString();
         //==========================================================================================================
         // Does this track have vocals?
         //==========================================================================================================
         $isVocal = $parsedTags['lyrics'] !== null;
         //==========================================================================================================
         // Fill in the title tag if it's missing.
         //==========================================================================================================
         if (!$parsedTags['title']) {
             $parsedTags['title'] = $file->getBasename('.' . $file->getExtension());
         }
         //==========================================================================================================
         // Determine the genre.
         //==========================================================================================================
         $genreName = $parsedTags['genre'];
         $genreSlug = Str::slug($genreName);
         $this->info('Genre: ' . $genreName);
         if ($genreName && $genreSlug !== '') {
             $genre = Genre::where('name', '=', $genreName)->first();
             if ($genre) {
                 $genreId = $genre->id;
             } else {
                 $genre = new Genre();
                 $genre->name = $genreName;
                 $genre->slug = $genreSlug;
                 $genre->save();
                 $genreId = $genre->id;
                 $this->comment('Created a new genre!');
             }
         } else {
             $genreId = $UNKNOWN_GENRE->id;
             // "Unknown" genre ID
         }
         //==========================================================================================================
         // Determine which artist account this file belongs to using the containing directory.
         //==========================================================================================================
         $this->info('Path to file: ' . $file->getRelativePath());
         $path_components = explode(DIRECTORY_SEPARATOR, $file->getRelativePath());
         $artist_name = $path_components[0];
         $album_name = array_key_exists(1, $path_components) ? $path_components[1] : null;
         $this->info('Artist: ' . $artist_name);
         $this->info('Album: ' . $album_name);
         $artist = User::where('display_name', '=', $artist_name)->first();
         if (!$artist) {
             $artist = new User();
             $artist->display_name = $artist_name;
             $artist->email = null;
             $artist->is_archived = true;
             $artist->slug = Str::slug($artist_name);
             $slugExists = User::where('slug', '=', $artist->slug)->first();
             if ($slugExists) {
                 $this->error('Horsefeathers! The slug ' . $artist->slug . ' is already taken!');
                 $artist->slug = $artist->slug . '-' . Str::random(4);
             }
             $artist->save();
         }
         //==========================================================================================================
         // Extract the cover art, if any exists.
         //==========================================================================================================
         $this->comment('Extracting cover art!');
         $coverId = null;
         if (array_key_exists('comments', $allTags) && array_key_exists('picture', $allTags['comments'])) {
             $image = $allTags['comments']['picture'][0];
             if ($image['image_mime'] === 'image/png') {
                 $extension = 'png';
             } elseif ($image['image_mime'] === 'image/jpeg') {
                 $extension = 'jpg';
             } elseif ($image['image_mime'] === 'image/gif') {
                 $extension = 'gif';
             } else {
                 $this->error('Unknown cover art format!');
             }
             // write temporary image file
             $imageFilename = $file->getFilename() . ".cover.{$extension}";
             $imageFilePath = "{$tmpPath}/" . $imageFilename;
             File::put($imageFilePath, $image['data']);
             $imageFile = new UploadedFile($imageFilePath, $imageFilename, $image['image_mime']);
             $cover = Image::upload($imageFile, $artist);
             $coverId = $cover->id;
         } else {
             $this->comment('No cover art found!');
         }
         //==========================================================================================================
         // Is this part of an album?
         //==========================================================================================================
         $albumId = null;
         $albumName = $parsedTags['album'];
         if ($albumName !== null) {
             $album = Album::where('user_id', '=', $artist->id)->where('title', '=', $albumName)->first();
             if (!$album) {
                 $album = new Album();
                 $album->title = $albumName;
                 $album->user_id = $artist->id;
                 $album->cover_id = $coverId;
                 $album->save();
             }
             $albumId = $album->id;
         }
         //==========================================================================================================
         // Save this track.
         //==========================================================================================================
         // "Upload" the track to Pony.fm
         $this->comment('Transcoding the track!');
         Auth::loginUsingId($artist->id);
         $trackFile = new UploadedFile($file->getPathname(), $file->getFilename(), $allTags['mime_type']);
         Input::instance()->files->add(['track' => $trackFile]);
         $upload = new UploadTrackCommand(true, true);
         $result = $upload->execute();
         if ($result->didFail()) {
             $this->error(json_encode($result->getMessages(), JSON_PRETTY_PRINT));
         } else {
             // Save metadata.
             $track = Track::find($result->getResponse()['id']);
             $track->title = $parsedTags['title'];
             $track->cover_id = $coverId;
             $track->album_id = $albumId;
             $track->genre_id = $genreId;
             $track->track_number = $parsedTags['track_number'];
             $track->released_at = $releasedAt;
             $track->description = $parsedTags['comments'];
             $track->is_downloadable = true;
             $track->lyrics = $parsedTags['lyrics'];
             $track->is_vocal = $isVocal;
             $track->license_id = 2;
             $track->save();
             // If we made it to here, the track is intact! Log the import.
             DB::table('mlpma_tracks')->insert(['track_id' => $result->getResponse()['id'], 'path' => $file->getRelativePath(), 'filename' => $file->getFilename(), 'extension' => $file->getExtension(), 'imported_at' => Carbon::now(), 'parsed_tags' => json_encode($parsedTags), 'raw_tags' => json_encode($rawTags)]);
         }
         echo PHP_EOL . PHP_EOL;
     }
 }
Esempio n. 6
0
 public function __construct($genreId, $destinationGenreId)
 {
     $this->_genreToDelete = Genre::find($genreId);
     $this->_destinationGenre = Genre::find($destinationGenreId);
 }
Esempio n. 7
0
 /**
  * Returns the ID of the given genre, creating it if necessary.
  *
  * @param string $genreName
  * @return int
  */
 protected function getGenreId(string $genreName)
 {
     return Genre::firstOrCreate(['name' => $genreName, 'slug' => Str::slug($genreName)])->id;
 }