/**
* Sync media using a watch record.
*
* @param WatchRecordInterface $record The watch record.
* @param SyncMedia|null $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}");
}
}