FileHook not working


I am trying to connect to the post_delete Filesystem hook to cleanup the Audio Player library.
for some reason, the hook is not triggered when I remove a file.
any suggestion?

Give a try to those:

File is deleted and is going to Trashbin if trashbin app is enabled:

 Util::connectHook('OC_Filesystem', 'delete', '', '');

File is restored from the trashbin

Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', '', '');

File is deleted from the trashbin

Util::connectHook('\OCP\Trashbin', 'preDelete', '', '');

Also works on the activity app.

Not sure what’s wrong with the post hook thou?

I think it’s postDelete

I really don´t get it.
I am trying all combinations, but my Hook is not called - no entry in log. nothing

\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack'); 
\OCP\Util::connectHook('OC_Filesystem', 'postDelete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack'); 
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack'); 
\OCP\Util::connectHook('\OCP\Trashbin', 'preDelete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack'); 

these lines in the app.php

did I miss anything else?
I even restarted php to reset the caches

can’t create branch on your repo :confused:

Please check this is working, if you want to compare to your work and see what was going wrong.
Note that I moved few thing from app.php to a method in Application.php and call that method. I find it cleaner this way.

I will check.

thank you! always learning something new about app/application split…

I split all known actions into several functions for debugging

Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack1'); 
Util::connectHook('OC_Filesystem', 'postDelete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack2'); 
Util::connectHook('OC_Filesystem', 'delete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack3'); 
Util::connectHook('\OCP\Trashbin', 'preDelete', 'OCA\audioplayer\Hooks\FileHooks', 'deleteTrack4'); 
Util::connectHook('OC_Filesystem', 'delete', '\OCA\audioplayer\Hooks\FileHooks', 'deleteTrack5');

only option 4 - Trashbin preDelete works.
when disabling Trashbin, non of the functions is triggered

this does not really help me because I need the deleted-option from the files app to remove the title from my audioplayer library.

any other idea?

according to the docu, it should be "delete"

My idea is that your path for the class need a \ (backslash) :slight_smile:

try this:

Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\audioplayer\Hooks\FileHooks', 'deleteTrack1'); 
Util::connectHook('OC_Filesystem', 'postDelete', '\OCA\audioplayer\Hooks\FileHooks', 'deleteTrack2'); 
Util::connectHook('OC_Filesystem', 'delete', '\OCA\audioplayer\Hooks\FileHooks', 'deleteTrack3'); 
Util::connectHook('\OCP\Trashbin', 'preDelete', '\OCA\audioplayer\Hooks\FileHooks', 'deleteTrack4');

no success
it works for Trashbin without backslash

really clueless. Tried in NC12 and OC9; makes no difference

but you tried my branch ? It looks like it is working there

this dies not seem right in your branch

function inside __construct.
I closed the construct with an }

but I am only getting
Object of class OCA\\audioplayer\\AppInfo\\Application could not be converted to string at \/var\/www\/nextcloud\/lib\/private\/legacy\/app.php#80

oups, lost few lines …

I pushed a fix, and this is what I got in the log when deleting a file or a folder:

thank you for not giving up.

I have no clue and skipping this feature.
I do not get it triggered. I even cloned your whole repo - but nothing on 2 separate test instances.

there needs to be something wrong with the file-hooks. because the user-deletion-hook is still working without issues. Don´t know if its a combination of enabled apps or something. But when I am having the issue, others could also have it.

As this does not seem to be something stable to rely on, I will not implement it.

one question: what is “version 13” in your screenshot?

I have to admit that I am using the master from nextcloud/server (13.0.0)

Anyway, try something before giving up !

  • Add this to your info.xml:
   <filesystem />
  • disable your app

./occ app:disable audioplayer

  • enable your app

./occ app:enable audioplayer

delete a file and check your log. (you might need to set the loglevel to 1 in config/config.php)

1 Like

ok, this was the usefull information!!!
the entry in info.xml is required
app needs to be re-enabled to have effect

can you tell the background of the xml entry?

in generell the hook is called. unfortunately not via a Node object. just the path.
Now I need myself from that to the file_id…

Yes, I am not an expert, but I would try this:

$view = OC\Files\Filesystem::getView();
$fileInfo = $view->getFileInfo($path);

if it is not working, set $view this way instead:

$view = new \OC\Files\View('/' . \OC::$server->getUserSession()->getUser());

Then, you get whatever you want (\OC\Files\FileInfo):


thank you!
it works!

I flagged your post with the info.xml and the enable/disable as solution because this was the key to make it work

1 Like