/** * Sync the media. Oh sync the media. * * @param string|null $path * @param SyncMedia $syncCommand The SyncMedia command object, to log to console if executed by artisan. */ public function sync($path = null, SyncMedia $syncCommand = null) { set_time_limit(env('APP_MAX_SCAN_TIME', 600)); $path = $path ?: Setting::get('media_path'); $results = ['good' => [], 'bad' => [], 'ugly' => []]; // For now we only care about mp3 and ogg files. // Support for other formats (AAC?) may be added in the future. $files = Finder::create()->files()->name('/\\.(mp3|ogg)$/')->in($path); foreach ($files as $file) { $song = $this->syncFile($file); if ($song === true) { $results['ugly'][] = $file; } elseif ($song === false) { $results['bad'][] = $file; } else { $results['good'][] = $file; } if ($syncCommand) { $syncCommand->logToConsole($file->getPathname(), $song); } } // Delete non-existing songs. $hashes = array_map(function ($f) { return $this->getHash($f->getPathname()); }, array_merge($results['ugly'], $results['good'])); Song::whereNotIn('id', $hashes)->delete(); // Empty albums and artists should be gone as well. $inUseAlbums = Song::select('album_id')->groupBy('album_id')->get()->lists('album_id'); $inUseAlbums[] = Album::UNKNOWN_ID; Album::whereNotIn('id', $inUseAlbums)->delete(); $inUseArtists = Album::select('artist_id')->groupBy('artist_id')->get()->lists('artist_id'); $inUseArtists[] = Artist::UNKNOWN_ID; Artist::whereNotIn('id', $inUseArtists)->delete(); }
/** * Fired every time a LibraryChanged event is triggered. * Remove empty albums and artists from our system. */ public function handle() { $inUseAlbums = Song::select('album_id')->groupBy('album_id')->get()->lists('album_id'); $inUseAlbums[] = Album::UNKNOWN_ID; Album::whereNotIn('id', $inUseAlbums)->delete(); $inUseArtists = Album::select('artist_id')->groupBy('artist_id')->get()->lists('artist_id'); $inUseArtists[] = Artist::UNKNOWN_ID; Artist::whereNotIn('id', $inUseArtists)->delete(); }
/** * Tidy up the library by deleting empty albums and artists. */ public function tidy() { $inUseAlbums = Song::select('album_id')->groupBy('album_id')->get()->lists('album_id')->toArray(); $inUseAlbums[] = Album::UNKNOWN_ID; Album::whereNotIn('id', $inUseAlbums)->delete(); $inUseArtists = Album::select('artist_id')->groupBy('artist_id')->get()->lists('artist_id')->toArray(); $contributingArtists = Song::distinct()->select('contributing_artist_id')->groupBy('contributing_artist_id')->get()->lists('contributing_artist_id')->toArray(); $inUseArtists = array_merge($inUseArtists, $contributingArtists); $inUseArtists[] = Artist::UNKNOWN_ID; $inUseArtists[] = Artist::VARIOUS_ID; Artist::whereNotIn('id', $inUseArtists)->delete(); }