~~UPDATE ~~ : file.create:after hook which makes call to external API instead of plugin

Hi!

Unfortunately I never really bothered to learn how to code a proper hook and wanted to ask you if someone was interested in coding a file.create:after hook.

In detail:
A small client of mine wishes to be able to upload videos to her website via the Kirby panel. No big deal there, however , after uploading a video to the Kirby panel, the same video should upload to her JWPlayer-account. JWPlayer is a streaming platform similar to Vimeo . They provide an API especially for that kind of function.

The reason for that is, she doesn’t want to have multiple logins to upload videos but only the Kirby panel login and not having to login to the JWPlayer’s dashboard.

The API call is to simply provide a video URL to the JWPlayer API via PHP and they automatically fetch the video from the provided URL.

They call this API call PHP URL fetch . So the hook’s function would be to make the API call via PHP to the JWPlayer’s API after a video has been uploaded via the native files-section – the JWPlayer’s API then automatically fetches the Video and returns the new Video-URL.

The newly returned URL would then be stored as a simple string inside a field along a sorting number (like in the native files section). It would not need to output anything else than the URLs and sorting number because embedding the videos to the actual website would be done on the frontend.

Unfortunately, this task is not within my current skill range.

The JWPlayer’s API: Uploading content (URL fetch)
Archived info on the PHP URL fetch (some other person): setting up website in php | JW Player
API library: GitHub - jwplayer/jwplatform-php: PHP library for the JW Platform API

Would anyone be interested in a job coding such a plugin?

Looking forward to hearing from you.

Best,
Tobias

I was told by @jimbobrjames that maybe @nilshoerrmann or @thguenther could help? :grinning:

Extending core sections is not very straight-forward unfortunately (at least for me). However if I understand your use case correctly you could simply use a file.create:after hook and put your API call there.

By using a file blueprint for this files section you can check for it in the hook and only upload files uploaded to this one specific files section.

1 Like

:+1: for @thguenther’s suggestion.

1 Like

The problem with the file.create:after hook is that there is no way of returning the success to the Panel, see: File.create:after hook – catch callback from external API - #14 by teh
(or only as error message)

Yeah that’s what I initially thought and tried however, as easy it might be for some, it’s beyond my skills :confused:

Is the success message critical? Maybe it could work in the other direction: The hook waits until the API has confirmed the upload. If there was an error, that error is passed back to the Panel from the hook and if there wasn’t, it all succeeds like normal.

1 Like

That’s what I already suggested in the linked thread… That would be the easiest solution.

1 Like

Yeah that would be fine as well, however I’m not sure yet if the hook can wait for the API callback.

@texnixe suggested to use sleep(X) but I don’t know if sleep uses a hardcoded amount of seconds or not. Like if sleep(10) is used but the upload fails after 20 seconds the hook won’t know. Or am I misunderstanding?

The sleep thing was just an example to test, this is nothing that should be used in the real hook.

1 Like

Ah ok now I get it, thx for the info!

Would anyone be interested in coding such a file.create:after hook? @thguenther @lukasbestle @pixelijn @nilshoerrmann ?