koel/app/Libraries/WatchRecord/InotifyWatchRecord.php

53 lines
1.6 KiB
PHP
Raw Normal View History

2016-02-04 15:04:53 +00:00
<?php
namespace App\Libraries\WatchRecord;
class InotifyWatchRecord extends WatchRecord implements WatchRecordInterface
{
/**
* {@inheritdoc}
*
* @param string $input
2016-02-04 15:04:53 +00:00
*/
public function __construct(string $input)
2016-02-04 15:04:53 +00:00
{
parent::__construct($input);
$this->parse($input);
2016-02-04 15:04:53 +00:00
}
/**
* Parse the inotifywait's output. The inotifywait command should be something like:
2016-02-04 15:48:15 +00:00
* $ inotifywait -rme move,close_write,delete --format "%e %w%f" $MEDIA_PATH.
2016-02-04 15:04:53 +00:00
*/
2018-08-24 15:27:19 +00:00
public function parse(string $string): void
2016-02-04 15:04:53 +00:00
{
list($events, $this->path) = explode(' ', $string, 2);
$this->events = explode(',', $events);
}
/**
* Determine if the object has just been deleted or moved from our watched directory.
*/
2018-08-24 15:27:19 +00:00
public function isDeleted(): bool
2016-02-04 15:04:53 +00:00
{
return $this->eventExists('DELETE') || $this->eventExists('MOVED_FROM');
}
/**
* Determine if the object has just been created or modified.
2016-04-02 13:09:09 +00:00
* For our purpose, we watch both the CREATE and the CLOSE_WRITE event, because some operating
* systems only support CREATE, but not CLOSE_WRITE and MOVED_TO.
2016-02-04 15:04:53 +00:00
* Additionally, a MOVED_TO (occurred after the object has been moved/renamed to another location
* **under our watched directory**) should be considered as "modified" also.
*/
2018-08-24 15:27:19 +00:00
public function isNewOrModified(): bool
2016-02-04 15:04:53 +00:00
{
return $this->eventExists('CLOSE_WRITE') || $this->eventExists('CREATE') || $this->eventExists('MOVED_TO');
2016-02-04 15:04:53 +00:00
}
2018-08-24 15:27:19 +00:00
public function isDirectory(): bool
2016-02-04 15:04:53 +00:00
{
return $this->eventExists('ISDIR');
}
}