Commit d5dd0540 authored by Petr Skoda's avatar Petr Skoda
Browse files

MDL-26028 send_file performance improvements

Includes X-Sendfile support, ETags for stored_files and general cleanup.
parent 99302c5e
......@@ -220,6 +220,12 @@ $CFG->admin = 'admin';
// about students being served outdated versions of uploaded files.
// $CFG->filelifetime = 86400;
//
// Some web servers can offload the file serving from PHP process,
// comment out one the following options to enable it in Moodle:
// $CFG->xsendfile = 'X-Sendfile'; // Apache {@see https://tn123.org/mod_xsendfile/}
// $CFG->xsendfile = 'X-LIGHTTPD-send-file'; // Lighttpd {@see http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file}
// $CFG->xsendfile = 'X-Accel-Redirect'; // Nginx {@see http://wiki.nginx.org/XSendfile}
//
// This setting will prevent the 'My Courses' page being displayed when a student
// logs in. The site front page will always show the same (logged-out) view.
// $CFG->disablemycourses = true;
......
This diff is collapsed.
......@@ -1294,6 +1294,22 @@ class file_storage {
return array($contenthash, $filesize, $newfile);
}
/**
* Serve file content using X-Sendfile header.
* Please make sure that all headers are already sent
* and the all access control checks passed.
*
* @param string $contenthash sah1 hash of the file content to be served
* @return bool success
*/
public function xsendfile($contenthash) {
global $CFG;
require_once("$CFG->libdir/xsendfilelib.php");
$hashpath = $this->path_from_hash($contenthash);
return xsendfile("$hashpath/$contenthash");
}
/**
* Return path to file with given hash.
*
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* X-Sendfile support
*
* @package core_files
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//NOTE: do not verify MOODLE_INTERNAL here, this is used from themes too
/**
* Serve file using X-Sendfile header, this needs special server module
* or configuration. Please make sure that all headers are already sent
* and the all access control checks passed.
*
* @param string $filepath
* @return bool success
*/
function xsendfile($filepath) {
global $CFG;
if (empty($CFG->xsendfile)) {
return false;
}
if (!file_exists($filepath)) {
return false;
}
if (headers_sent()) {
return false;
}
if ($CFG->xsendfile === 'X-LIGHTTPD-send-file') {
// http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file says 1.4 it does not support byteserving
header('Accept-Ranges: none');
} else if ($CFG->xsendfile === 'X-Accel-Redirect') {
// http://wiki.nginx.org/XSendfile
// Nginx is using relative path to protected folder, please note you can not use cache dir, tempdir and file pool outside of dataroot!
$filepath = realpath($filepath);
$dataroot = realpath($CFG->dataroot);
if (strpos($filepath, $dataroot) !== 0) {
return false;
}
$filepath = substr($filepath, strlen($dataroot));
}
header("$CFG->xsendfile: $filepath");
return true;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment