php 是否可以在nextcloud中操作Node的文件缓存大小?

hc2pp10m  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(41)

我正在开发一个NextCloud应用程序。
我有一个链接到另一个目录的目录,所以Nextcloud认为我使用了双倍的空间,但实际上并非如此,所以我想操纵文件缓存条目的大小。
我已经搜索了一点,但只找到一些过时的东西,其中使用挂钩,因为它是不推荐的,我想使用另一种技术。目前,我试图更新条目后,“NodeAddedToCache”或“FolderScannedEvent”使用Eventlistener类。但它似乎没有被执行,我真的不知道我应该如何测试/调试,因为我找不到一个很好的文档,哪些事件将在什么情况下被触发,或者在哪里我可以找到这个东西的脚本。
我的当前代码看起来像这样:
lib/AppInfo/Application.php

namespace OCA\Application\AppInfo;

use OCA\Application\Event\RegistrationEventListener;
use OCP\AppFramework\App;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\FolderScannedEvent;

class Application extends App {
    public const APP_ID = 'application';

    public function __construct(IEventDispatcher $dispatcher) {
        parent::__construct(self::APP_ID);
        $dispatcher->addServiceListener(FolderScannedEvent::class, RegistrationEventListener::class, 1);
    }
}

字符串
RegistrationEventListener.php

<?php
namespace OCA\Application\Event;

use OC\Files\Storage\Storage;
use OCA\AdminAudit\AuditLogger;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\FolderScannedEvent;

class RegistrationEventListener implements IEventListener {
    private $logger;
    private $storage;

    public function __construct(AuditLogger $logger, Storage $storage)
    {
        $this->logger = $logger;
        $this->storage = $storage;
    }

    /**
     * @inheritDoc
     */
    public function handle(FolderScannedEvent|Event $event): void
    {
        if(is_link($event->getAbsolutePath())) {
            $fileId = $this->storage->getCache()->getId($event->getAbsolutePath());
            $this->storage->getCache()->update($fileId, [
                'size'  => 0
            ]);
        }
    }
}

qhhrdooz

qhhrdooz1#

为了能够使用Nextcloud Filescanner事件,您应该像这样使用Application类的IBootstrap->register()方法:
AppInfo/Application.php

<?php

namespace OCA\Application\AppInfo;

use OCA\Application\Listener\FileCacheListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\FileCacheUpdated;
use OCP\Files\Events\NodeAddedToCache;
use OCP\Files\Events\NodeRemovedFromCache;

class Application extends App implements IBootstrap {
    public const APP_NAME = 'application';

    public function __construct(array $urlParams = []) {
        parent::__construct(self::APP_NAME, $urlParams);
    }

    /**
     * @inheritdoc
     */
    public function register(IRegistrationContext $context): void {
        $context->registerEventListener(NodeAddedToCache::class, FileCacheListener::class);
        $context->registerEventListener(FileCacheUpdated::class, FileCacheListener::class);
        $context->registerEventListener(NodeRemovedFromCache::class, FileCacheListener::class);
    }

    /**
     * @inheritdoc
     */
    public function boot(IBootContext $context): void {
    }
}

字符串
你的监听器可能看起来像这样(Listener/FileCacheListener.php):

<?php

namespace OCA\Application\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\FileCacheUpdated;
use OCP\Files\Events\NodeAddedToCache;
use OCP\Files\Events\NodeRemovedFromCache;

class FileCacheListener implements IEventListener {
    public function handle(Event $event) : void {
        if (!($event instanceof NodeAddedToCache) 
            && !($event instanceof FileCacheUpdated)
            && !($event instanceof NodeRemovedFromCache)
        ) {
            return;
        }

        // Do something here ...
        
    }
}


您可以通过直接在文件系统上添加/更新/删除文件来检查是否调用了侦听器,然后调用php occ files:scan --all
请注意,上述事件仅在文件被扫描且已更改时才会触发。如果您想捕获单个事件(如文件上传等),您可以在这里找到一个工作示例。祝您好运:-)

相关问题