Limit gzip compression to smaller files, also exclude other kinds of files

This commit is contained in:
bohwaz 2022-12-23 20:42:01 +01:00
parent d8e98506c2
commit 59669cb6c9

View file

@ -34,6 +34,8 @@ namespace KD2\WebDAV
const SHARED_LOCK = 'shared';
const EXCLUSIVE_LOCK = 'exclusive';
protected bool $enable_gzip = true;
protected string $base_uri;
public string $original_uri;
@ -344,17 +346,20 @@ namespace KD2\WebDAV
$this->log('HTTP Range requested: %s-%s', $start, $end);
}
elseif (isset($_SERVER['HTTP_ACCEPT_ENCODING'])
elseif ($this->enable_gzip
&& isset($_SERVER['HTTP_ACCEPT_ENCODING'])
&& false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')
&& isset($props['DAV::getcontentlength'])
// Don't compress if size is larger than 8 MiB
&& $props['DAV::getcontentlength'] < 8*1024*1024
// Don't compress already compressed content
&& !preg_match('/\.(?:mp4|m4a|zip|docx|xlsx|ods|odt|odp|7z|gz|bz2|rar|webm|ogg|mp3|ogm|flac|ogv|mkv|avi)$/i', $uri)) {
&& !preg_match('/\.(?:cbz|cbr|cb7|mp4|m4a|zip|docx|xlsx|pptx|ods|odt|odp|7z|gz|bz2|lzma|lz|xz|apk|dmg|jar|rar|webm|ogg|mp3|ogm|flac|ogv|mkv|avi)$/i', $uri)) {
$gzip = true;
header('Content-Encoding: gzip', true);
}
// Try to avoid common issues with output buffering and stuff
if (function_exists('apache_setenv'))
{
if (function_exists('apache_setenv')) {
@apache_setenv('no-gzip', 1);
}
@ -436,7 +441,7 @@ namespace KD2\WebDAV
if ($gzip) {
$this->log('Using gzip output compression');
$gzip = deflate_init(ZLIB_ENCODING_GZIP, ['level' => 9]);
$gzip = deflate_init(ZLIB_ENCODING_GZIP);
$fp = fopen('php://temp', 'wb');
@ -1883,11 +1888,11 @@ RewriteRule ^.*$ /index.php [END]
$fp = fopen(__FILE__, 'r');
if ($relative_uri == '.webdav/webdav.js') {
fseek($fp, 50401, SEEK_SET);
fseek($fp, 50637, SEEK_SET);
echo fread($fp, 27769);
}
else {
fseek($fp, 50401 + 27769, SEEK_SET);
fseek($fp, 50637 + 27769, SEEK_SET);
echo fread($fp, 7004);
}