/** * @throws \Exception * @return CommandResponse */ public function execute() { $rules = ['title' => 'required|min:3|max:50', 'cover' => 'image|mimes:png|min_width:350|min_height:350', 'cover_id' => 'exists:images,id', 'track_ids' => 'exists:tracks,id']; $validator = Validator::make($this->_input, $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } $album = new Album(); $album->user_id = Auth::user()->id; $album->title = $this->_input['title']; $album->description = $this->_input['description']; if (isset($this->_input['cover_id'])) { $album->cover_id = $this->_input['cover_id']; } else { if (isset($this->_input['cover'])) { $cover = $this->_input['cover']; $album->cover_id = Image::upload($cover, Auth::user())->id; } else { if (isset($this->_input['remove_cover']) && $this->_input['remove_cover'] == 'true') { $album->cover_id = null; } } } $trackIds = explode(',', $this->_input['track_ids']); $album->save(); $album->syncTrackIds($trackIds); return CommandResponse::succeed(['id' => $album->id]); }
/** * @throws \Exception * @return CommandResponse */ public function execute() { foreach ($this->_album->tracks as $track) { $track->album_id = null; $track->track_number = null; $track->updateTags(); $track->save(); } $this->_album->delete(); return CommandResponse::succeed(); }
public function getAlbums() { $query = Favourite::whereUserId(Auth::user()->id)->whereNotNull('album_id')->with(['album' => function ($query) { $query->userDetails(); }, 'album.user', 'album.user.avatar', 'album.cover']); $albums = []; foreach ($query->get() as $fav) { if ($fav->album == null) { continue; } $albums[] = Album::mapPublicAlbumSummary($fav->album); } return Response::json(["albums" => $albums], 200); }
/** * @throws \Exception * @return CommandResponse */ public function execute() { $rules = ['content' => 'required', 'track_id' => 'exists:tracks,id', 'albums_id' => 'exists:albums,id', 'playlist_id' => 'exists:playlists,id', 'profile_id' => 'exists:users,id']; $validator = Validator::make($this->_input, $rules); if ($validator->fails()) { return CommandResponse::fail($validator); } $comment = new Comment(); $comment->user_id = Auth::user()->id; $comment->content = $this->_input['content']; if ($this->_type == 'track') { $column = 'track_id'; } else { if ($this->_type == 'user') { $column = 'profile_id'; } else { if ($this->_type == 'album') { $column = 'album_id'; } else { if ($this->_type == 'playlist') { $column = 'playlist_id'; } else { App::abort(500); } } } } $comment->{$column} = $this->_id; $comment->save(); // Recount the track's comments, if this is a track comment if ($this->_type === 'track') { $entity = Track::find($this->_id); } elseif ($this->_type === 'album') { $entity = Album::find($this->_id); } elseif ($this->_type === 'playlist') { $entity = Playlist::find($this->_id); } elseif ($this->_type === 'user') { $entity = User::find($this->_id); } else { App::abort(400, 'This comment is being added to an invalid entity!'); } $entity->comment_count = Comment::where($column, $this->_id)->count(); $entity->save(); return CommandResponse::succeed(Comment::mapPublic($comment)); }
public function getDownload($id, $extension) { $album = Album::with('tracks', 'user')->find($id); if (!$album) { App::abort(404); } $format = null; $formatName = null; foreach (Track::$Formats as $name => $item) { if ($item['extension'] == $extension) { $format = $item; $formatName = $name; break; } } if ($format == null) { App::abort(404); } ResourceLogItem::logItem('album', $id, ResourceLogItem::DOWNLOAD, $format['index']); $downloader = new AlbumDownloader($album, $formatName); $downloader->download(); }
public function getContent($slug) { $user = User::whereSlug($slug)->first(); if (!$user) { App::abort(404); } $query = Track::summary()->published()->listed()->explicitFilter()->with('genre', 'cover', 'user')->userDetails()->whereUserId($user->id)->whereNotNull('published_at'); $tracks = []; $singles = []; foreach ($query->get() as $track) { if ($track->album_id != null) { $tracks[] = Track::mapPublicTrackSummary($track); } else { $singles[] = Track::mapPublicTrackSummary($track); } } $query = Album::summary()->with('user')->orderBy('created_at', 'desc')->where('track_count', '>', 0)->whereUserId($user->id); $albums = []; foreach ($query->get() as $album) { $albums[] = Album::mapPublicAlbumSummary($album); } return Response::json(['singles' => $singles, 'albumTracks' => $tracks, 'albums' => $albums], 200); }
/** * Execute the console command. * * @return mixed */ public function handle() { // Get list of affected users $usernames = DB::table('users')->select(['username', DB::raw('COUNT(*) as count')])->whereNull('disabled_at')->whereNotNull('username')->groupBy(DB::raw('LOWER(username)'))->having('count', '>=', 2)->lists('username'); foreach ($usernames as $username) { // Find the relevant accounts // ========================== /** @var Collection $accounts */ $accounts = User::where('username', $username)->orderBy('created_at', 'ASC')->get(); $firstAccount = $accounts[0]; $accounts->forget(0); $accountIds = $accounts->pluck('id'); // Reassign content // ================ // This is done with the less-efficient-than-raw-SQL Eloquent // methods to generate appropriate revision logs. $this->info('Merging duplicates for: ' . $firstAccount->username); DB::transaction(function () use($accounts, $accountIds, $firstAccount) { foreach (Album::whereIn('user_id', $accountIds)->get() as $album) { $album->user_id = $firstAccount->id; $album->save(); } foreach (Comment::whereIn('user_id', $accountIds)->get() as $comment) { $comment->user_id = $firstAccount->id; $comment->save(); } foreach (Favourite::whereIn('user_id', $accountIds)->get() as $favourite) { $favourite->user_id = $firstAccount->id; $favourite->save(); } foreach (Follower::whereIn('artist_id', $accountIds)->get() as $follow) { $follow->artist_id = $firstAccount->id; $follow->save(); } foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) { $image->uploaded_by = $firstAccount->id; $image->save(); } foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) { $image->uploaded_by = $firstAccount->id; $image->save(); } DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $firstAccount->id]); foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) { $playlist->user_id = $firstAccount->id; $playlist->save(); } foreach (Playlist::whereIn('user_id', $accountIds)->get() as $playlist) { $playlist->user_id = $firstAccount->id; $playlist->save(); } foreach (ResourceLogItem::whereIn('user_id', $accountIds)->get() as $item) { $item->user_id = $firstAccount->id; $item->save(); } foreach (ResourceUser::whereIn('user_id', $accountIds)->get() as $item) { $item->user_id = $firstAccount->id; $item->save(); } foreach (Track::whereIn('user_id', $accountIds)->get() as $track) { $track->user_id = $firstAccount->id; $track->save(); } foreach ($accounts as $account) { $account->disabled_at = Carbon::now(); $account->save(); } }); } }
private function removeTrackFromAlbum($track) { $album = $track->album; $index = 0; foreach ($album->tracks as $track) { if ($track->id == $this->_trackId) { continue; } $track->track_number = ++$index; $track->updateTags(); $track->save(); } Album::whereId($album->id)->update(['track_count' => DB::raw('(SELECT COUNT(id) FROM tracks WHERE album_id = ' . $album->id . ')')]); }
public static function mapPublicAlbumSummary(Album $album) { $userData = ['stats' => ['views' => 0, 'downloads' => 0], 'is_favourited' => false]; if (Auth::check() && $album->users->count()) { $userRow = $album->users[0]; $userData = ['stats' => ['views' => (int) $userRow->view_count, 'downloads' => (int) $userRow->download_count], 'is_favourited' => (bool) $userRow->is_favourited]; } return ['id' => (int) $album->id, 'track_count' => (int) $album->track_count, 'title' => $album->title, 'slug' => $album->slug, 'created_at' => $album->created_at->format('c'), 'stats' => ['views' => (int) $album->view_count, 'downloads' => (int) $album->download_count, 'comments' => (int) $album->comment_count, 'favourites' => (int) $album->favourite_count], 'covers' => ['small' => $album->getCoverUrl(Image::SMALL), 'normal' => $album->getCoverUrl(Image::NORMAL), 'original' => $album->getCoverUrl(Image::ORIGINAL)], 'url' => $album->url, 'user' => ['id' => (int) $album->user->id, 'name' => $album->user->display_name, 'url' => $album->user->url], 'user_data' => $userData, 'permissions' => ['delete' => Auth::check() && Auth::user()->id == $album->user_id, 'edit' => Auth::check() && Auth::user()->id == $album->user_id]]; }
public function getEdit($id) { $album = Album::with('tracks')->find($id); if (!$album) { return $this->notFound('Album ' . $id . ' not found!'); } if ($album->user_id != Auth::user()->id) { return $this->notAuthorized(); } $tracks = []; foreach ($album->tracks as $track) { $tracks[] = ['id' => $track->id, 'title' => $track->title]; } return Response::json(['id' => $album->id, 'title' => $album->title, 'user_id' => $album->user_id, 'slug' => $album->slug, 'created_at' => $album->created_at, 'published_at' => $album->published_at, 'description' => $album->description, 'cover_url' => $album->hasCover() ? $album->getCoverUrl(Image::NORMAL) : null, 'real_cover_url' => $album->getCoverUrl(Image::NORMAL), 'tracks' => $tracks], 200); }
/** * 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; } }
/** * Returns the ID of the given album, creating it if necessary. * The cover ID is only used if a new album is created - it will not be * written to an existing album. * * @param int $artistId * @param string|null $albumName * @param null $coverId * @return int|null */ protected function getAlbumId(int $artistId, $albumName, $coverId = null) { if (null !== $albumName) { $album = Album::firstOrNew(['user_id' => $artistId, 'title' => $albumName]); if (null === $album->id) { $album->description = ''; $album->track_count = 0; $album->cover_id = $coverId; $album->save(); } return $album->id; } else { return null; } }