From 5bf5a7aaebabff669a674f19a4ec33cbca24f515 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 16 Apr 2020 16:31:07 +1000 Subject: [PATCH] MDL-68342 files: Add a faster xsendfile method to avoid db lookups --- lib/filelib.php | 2 +- lib/filestorage/file_storage.php | 15 +++++++++++++++ lib/filestorage/file_system.php | 15 +++++++++++++++ lib/upgrade.txt | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/filelib.php b/lib/filelib.php index ce314c76259..2a42f8e9ace 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -2169,7 +2169,7 @@ function readfile_accel($file, $mimetype, $accelerate) { if (is_object($file)) { $fs = get_file_storage(); if ($fs->supports_xsendfile()) { - if ($fs->xsendfile($file->get_contenthash())) { + if ($fs->xsendfile_file($file)) { return; } } diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index 14eb6c6df4a..9e599542e2e 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -1821,6 +1821,21 @@ class file_storage { } } + /** + * Serve file content using X-Sendfile header. + * Please make sure that all headers are already sent and the all + * access control checks passed. + * + * This alternate method to xsendfile() allows an alternate file system + * to use the full file metadata and avoid extra lookups. + * + * @param stored_file $file The file to send + * @return bool success + */ + public function xsendfile_file(stored_file $file): bool { + return $this->filesystem->xsendfile_file($file); + } + /** * Serve file content using X-Sendfile header. * Please make sure that all headers are already sent diff --git a/lib/filestorage/file_system.php b/lib/filestorage/file_system.php index 50156960d78..e8a78fb8160 100644 --- a/lib/filestorage/file_system.php +++ b/lib/filestorage/file_system.php @@ -435,6 +435,21 @@ abstract class file_system { return $image; } + /** + * Serve file content using X-Sendfile header. + * Please make sure that all headers are already sent and the all + * access control checks passed. + * + * This alternate method to xsendfile() allows an alternate file system + * to use the full file metadata and avoid extra lookups. + * + * @param stored_file $file The file to send + * @return bool success + */ + public function xsendfile_file(stored_file $file): bool { + return $this->xsendfile($file->get_contenthash()); + } + /** * Serve file content using X-Sendfile header. * Please make sure that all headers are already sent and the all diff --git a/lib/upgrade.txt b/lib/upgrade.txt index f9908ebfbdf..b9837e4d2d6 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -43,6 +43,7 @@ information provided here is intended especially for developers. * External functions can be called without requiring a session lock if they define 'readonlysession' => true in db/services.php. Note - this also requires $CFG->enable_read_only_sessions to be set to true. * database_manager::check_database_schema() now checks for missing and extra indexes. +* Implement a more direct xsendfile_file() method for an alternative_file_system_class === 3.8 === * Add CLI option to notify all cron tasks to stop: admin/cli/cron.php --stop