How can I use the BeforeDirectFileDownloadEvent
?
Following this guide, I successfully created a custom app called “Hello World.” I am working in a local environment using Docker.
What I want to achieve:
I want to detect when a user downloads an image or a file.
Currently, I am trying to detect this action and log it, but I am not able to make it work.
Here is the code I am working with:
nextcloud-docker-dev/workspace/server/apps-extra/helloworld/lib/AppInfo/Application.php
<?php
namespace OCA\HelloWorld\AppInfo;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
class Application extends App implements IBootstrap
{
public const APP_ID = 'helloworld';
/** @var LoggerInterface */
private $logger;
/** @psalm-suppress PossiblyUnusedMethod */
public function __construct()
{
parent::__construct(self::APP_ID);
$this->logger = $this->getContainer()->get(LoggerInterface::class);
$this->logger->error('Application cunstructor------------');
}
public function register(IRegistrationContext $context): void
{
$container = $this->getContainer();
$eventDispatcher = $container->get(IEventDispatcher::class);
$eventDispatcher->addListener(BeforeDirectFileDownloadEvent::class, function (BeforeDirectFileDownloadEvent $event) {
$filePath = $event->getPath();
$this->logger->error('Before file download---------: ' . $filePath);
});
}
public function boot(IBootContext $context): void {}
}
Question:
I found msg “'Application cunstructor------------” in nextcloud.log
So, process reach this constructor, But I can’t find “Before file download---------” .
How can I detect when a file download happens?
By the way, I can check the list of available events here. (BeforeDirectFileDownloadEvent too.)
https://docs.nextcloud.com/server/latest/developer_manual/basics/events.html#ocp-files-events-beforedirectfiledownloadevent