App\services\Media::sync PHP Method

sync() public method

Sync the media. Oh sync the media.
public sync ( string | null $path = null, array $tags = [], boolean $force = false, SyncMedia $syncCommand = null )
$path string | null
$tags array The tags to sync. Only taken into account for existing records. New records will have all tags synced in regardless.
$force boolean Whether to force syncing even unchanged files
$syncCommand App\Console\Commands\SyncMedia The SyncMedia command object, to log to console if executed by artisan.
    public function sync($path = null, $tags = [], $force = false, SyncMedia $syncCommand = null)
    {
        if (!app()->runningInConsole()) {
            set_time_limit(config('koel.sync.timeout'));
        }
        $path = $path ?: Setting::get('media_path');
        $this->setTags($tags);
        $results = ['good' => [], 'bad' => [], 'ugly' => []];
        $getID3 = new getID3();
        $files = $this->gatherFiles($path);
        if ($syncCommand) {
            $syncCommand->createProgressBar(count($files));
        }
        foreach ($files as $file) {
            $file = new File($file, $getID3);
            $song = $file->sync($this->tags, $force);
            if ($song === true) {
                $results['ugly'][] = $file;
            } elseif ($song === false) {
                $results['bad'][] = $file;
            } else {
                $results['good'][] = $file;
            }
            if ($syncCommand) {
                $syncCommand->updateProgressBar();
                $syncCommand->logToConsole($file->getPath(), $song, $file->getSyncError());
            }
        }
        // Delete non-existing songs.
        $hashes = array_map(function ($f) {
            return self::getHash($f->getPath());
        }, array_merge($results['ugly'], $results['good']));
        Song::deleteWhereIDsNotIn($hashes);
        // Trigger LibraryChanged, so that TidyLibrary handler is fired to, erm, tidy our library.
        event(new LibraryChanged());
    }

Usage Example

Example #1
0
 public function testSync()
 {
     $media = new Media();
     $media->sync($this->mediaPath);
     // Standard mp3 files under root path should be recognized
     $this->seeInDatabase('songs', ['path' => $this->mediaPath . '/full.mp3']);
     // Ogg files and audio files in subdirectories should be recognized
     $this->seeInDatabase('songs', ['path' => $this->mediaPath . '/subdir/back-in-black.ogg']);
     // Non-audio files shouldn't be recognized
     $this->notSeeInDatabase('songs', ['path' => $this->mediaPath . '/rubbish.log']);
     // Broken/corrupted audio files shouldn't be recognized
     $this->notSeeInDatabase('songs', ['path' => $this->mediaPath . '/fake.mp3']);
     // Artists should be created
     $this->seeInDatabase('artists', ['name' => 'Cuckoo']);
     $this->seeInDatabase('artists', ['name' => 'Koel']);
     // Albums should be created
     $this->seeInDatabase('albums', ['name' => 'Koel Testing Vol. 1']);
     // Albums and artists should be correctly linked
     $album = Album::whereName('Koel Testing Vol. 1')->first();
     $this->assertEquals('Koel', $album->artist->name);
     $currentCover = $album->cover;
     $song = Song::orderBy('id', 'desc')->first();
     // Modified file should be recognized
     touch($song->path, $time = time());
     $media->sync($this->mediaPath);
     $song = Song::find($song->id);
     $this->assertEquals($time, $song->mtime);
     // Albums with a non-default cover should have their covers overwritten
     $this->assertEquals($currentCover, Album::find($album->id)->cover);
 }
All Usage Examples Of App\services\Media::sync