Plugin support for mobile - file lists, which url to use?

Plugin support for mobile - file lists, which url to use?

sam marshall -
回帖数:1
Core developers的头像 Peer reviewers的头像 Plugin developers的头像

This is a question about the 'files' array that you can return from a Moodle plugin that supports mobile, for example in its 'view' method.

The array includes objects with, among others, a 'fileurl' field.

In one of my plugins, I'm returning an image URL in this field like

And in my template there is <img src="https://example.org/pluginfile.php/.../frog.jpg" alt="a frog">

The question is: should I actually be returning (and using in the img tag) a tokenpluginfile.php URL? Which seems to be the same except it doesn't need login and has a token in, like this code:

$token = get_user_key('core_files', $USER->id);
return $CFG->wwwroot . '/tokenpluginfile.php/' . $token . '/' . $context->id . [...]
The reason I ask is that I notice when you do it with pluginfile.php and it actually loads in the app, it roughly simultaneously loads both the pluginfile.php URL that I supplied, and a tokenpluginfile.php URL which the app has presumably generated itself. The pluginfile.php one, because of our server setup, causes a couple of redirects before failing and generally looks bad in the network panel 微笑 which I realise isn't a problem for real users but still. So I guess the question is:
  • Is it ok to use tokenpluginfile.php if I want (to remove the extra URL load) or is that incorrect behaviour and I should stick to using pluginfile?
  • Or, is it recommended to always return tokenpluginfile.php URLs?
--sam
回复sam marshall

Re: Plugin support for mobile - file lists, which url to use?

sam marshall -
Core developers的头像 Peer reviewers的头像 Plugin developers的头像

Dani responded to me in chat, so I'm pasting here for the record in case anyone else was wondering:

it's a known issue. Angular first renders the HTML, and that triggers a request to the file. Then Angular executes the directives (so we treat the URL and convert it into a tokenpluginfile URL) and renders it again, causing the second request. We have an issue open to look at it, but I guess it won't be easy to fix:

https://tracker.moodle.org/browse/MOBILE-3403

If you use a tokenpluginfile URL I think the app shouldn't change it, so Angular should skip the second request because the URL hasn't changed. But Moodle core never does that, so I guess it's not worth the effort to do that in a plugin