public syncByWatchRecord ( App\Libraries\WatchRecord\WatchRecordInterface $record, |
||
$record | App\Libraries\WatchRecord\WatchRecordInterface | The watch record. |
$syncCommand | The SyncMedia command object, to log to console if executed by artisan. |
public function syncByWatchRecord(WatchRecordInterface $record, SyncMedia $syncCommand = null)
{
Log::info("New watch record received: '{$record}'");
$path = $record->getPath();
if ($record->isFile()) {
Log::info("'{$path}' is a file.");
// If the file has been deleted...
if ($record->isDeleted()) {
// ...and it has a record in our database, remove it.
if ($song = Song::byPath($path)) {
$song->delete();
Log::info("{$path} deleted.");
event(new LibraryChanged());
} else {
Log::info("{$path} doesn't exist in our database--skipping.");
}
} elseif ($record->isNewOrModified()) {
$result = (new File($path))->sync($this->tags);
Log::info($result instanceof Song ? "Synchronized {$path}" : "Invalid file {$path}");
}
return;
}
// Record is a directory.
Log::info("'{$path}' is a directory.");
if ($record->isDeleted()) {
// The directory is removed. We remove all songs in it.
if ($count = Song::inDirectory($path)->delete()) {
Log::info("Deleted {$count} song(s) under {$path}");
event(new LibraryChanged());
} else {
Log::info("{$path} is empty--no action needed.");
}
} elseif ($record->isNewOrModified()) {
foreach ($this->gatherFiles($path) as $file) {
(new File($file))->sync($this->tags);
}
Log::info("Synced all song(s) under {$path}");
}
}
public function testWatchDirectoryDeleted() { $this->expectsEvents(LibraryChanged::class); $media = new Media(); $media->sync($this->mediaPath); $media->syncByWatchRecord(new InotifyWatchRecord("MOVED_FROM,ISDIR {$this->mediaPath}/subdir")); $this->notSeeInDatabase('songs', ['path' => $this->mediaPath . '/subdir/sic.mp3']); $this->notSeeInDatabase('songs', ['path' => $this->mediaPath . '/subdir/no-name.MP3']); $this->notSeeInDatabase('songs', ['path' => $this->mediaPath . '/subdir/back-in-black.mp3']); }