From: Hunter Fuller Date: Thu, 17 Mar 2022 04:17:42 +0000 (-0500) Subject: Add support for the X-Sendfile header/module to offload downloads. X-Git-Tag: 4.5.0~32 X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/commitdiff_plain/refs/merge-requests/102/head?hp=5058cbd048a108666d4078c692cb12ce96e521db Add support for the X-Sendfile header/module to offload downloads. With the appropriate config option enabled, Jirafeau will send the X-Sendfile header instead of sending the file itself. As a result, Apache/lighttpd will send the file without the intervention of Jirafeau, and download resumes/seeking will also be enabled for this download. If the user requested an encrypted file then this will not work, but it should work in all other cases. --- diff --git a/f.php b/f.php index e2547d7..f7f3786 100644 --- a/f.php +++ b/f.php @@ -272,11 +272,16 @@ elseif ($link['crypted']) { } /* Read file. */ else { - $r = fopen(VAR_FILES . $p . $link['hash'], 'r'); - while (!feof($r)) { - print fread($r, 1024); + if ($cfg['use_xsendfile']) { + $file_web_path = preg_replace('#^' . $_SERVER['DOCUMENT_ROOT'] . '#', '', VAR_FILES); + header('X-Sendfile: ' . $file_web_path . $p . $link['hash']); + } else { + $r = fopen(VAR_FILES . $p . $link['hash'], 'r'); + while (!feof($r)) { + print fread($r, 1024); + } + fclose($r); } - fclose($r); } if ($link['onetime'] == 'O') { diff --git a/lib/config.original.php b/lib/config.original.php index 365b5aa..ecca24f 100644 --- a/lib/config.original.php +++ b/lib/config.original.php @@ -183,6 +183,16 @@ $cfg['file_hash'] = 'md5'; */ $cfg['litespeed_workaround'] = false; +/* Use the X-Sendfile header which should cause your webserver to handle + * the sending of the file. The webserver must be configured to do this + * using the mod_xsendfile module in Apache or the appropriate config in + * lighttpd. The offload will not happen in the case of server-side encrypted + * files, but all other cases should work. Benefits include being able + * to resume downloads and seek instantly in media players like VLC or + * the Firefox/Discord/Chrome embedded player. + */ +$cfg['use_xsendfile'] = false; + /* Store uploader's IP along with 'link' file. * Depending of your legislation, you may have to adjust this parameter. */