From 3f83f35a5cec432409ea3a4d2dbd87eee0dd06cf Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo Date: Thu, 18 Nov 2021 18:17:02 -0300 Subject: [PATCH] MDL-73119 contentbank: Add pluginfile callback to content type plugins --- contentbank/tests/contenttype_test.php | 13 ++++++++++++ .../tests/fixtures/testable_contenttype.php | 16 +++++++++++++++ lib/filelib.php | 20 ++++++++++++++----- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/contentbank/tests/contenttype_test.php b/contentbank/tests/contenttype_test.php index 11a424600a1..b6a9b82cbd4 100644 --- a/contentbank/tests/contenttype_test.php +++ b/contentbank/tests/contenttype_test.php @@ -658,4 +658,17 @@ class contenttype_test extends \advanced_testcase { $url = $contenttype->get_download_url($content); $this->assertEmpty($url); } + + /** + * Tests pluginfile result. + * + * @covers ::pluginfile + */ + public function test_pluginfile() { + $this->resetAfterTest(); + $this->setAdminUser(); + $systemcontext = context_system::instance(); + $contenttype = new contenttype($systemcontext); + $this->assertIsCallable([$contenttype, 'pluginfile']); + } } diff --git a/contentbank/tests/fixtures/testable_contenttype.php b/contentbank/tests/fixtures/testable_contenttype.php index 2a5411d5455..2bb242e06c7 100644 --- a/contentbank/tests/fixtures/testable_contenttype.php +++ b/contentbank/tests/fixtures/testable_contenttype.php @@ -100,4 +100,20 @@ class contenttype extends \core_contentbank\contenttype { return true; } + + /** + * This implements custom file serving. + * + * @param stdClass $course the course object + * @param stdClass $cm the course module object + * @param \context $context the context + * @param string $filearea the name of the file area + * @param array $args extra arguments (itemid, path) + * @param bool $forcedownload whether or not force download + * @param array $options additional options affecting the file serving + * @return void + */ + public static function pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []): void { + return; + } } diff --git a/lib/filelib.php b/lib/filelib.php index ba22c9da3cc..0d138cdc11d 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -5129,16 +5129,26 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin send_file_not_found(); } + $componentargs = fullclone($args); $itemid = (int)array_shift($args); $filename = array_pop($args); $filepath = $args ? '/'.implode('/', $args).'/' : '/'; - if (!$file = $fs->get_file($context->id, $component, $filearea, $itemid, $filepath, $filename) or - $file->is_directory()) { - send_file_not_found(); - } \core\session\manager::write_close(); // Unlock session during file serving. - send_stored_file($file, 0, 0, true, $sendfileoptions); // must force download - security! + + $contenttype = $DB->get_field('contentbank_content', 'contenttype', ['id' => $itemid]); + if (component_class_callback("\\{$contenttype}\\contenttype", 'pluginfile', + [$course, null, $context, $filearea, $componentargs, $forcedownload, $sendfileoptions], false) === false) { + + if (!$file = $fs->get_file($context->id, $component, $filearea, $itemid, $filepath, $filename) or + + $file->is_directory()) { + send_file_not_found(); + + } else { + send_stored_file($file, 0, 0, true, $sendfileoptions); // Must force download - security! + } + } } else if (strpos($component, 'mod_') === 0) { $modname = substr($component, 4); if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {