Add option to exclude multimedia files from direct download #1207

This commit is contained in:
Kijin Sung 2020-03-23 01:52:57 +09:00
parent b8665d73cb
commit 7633bc7b3a
2 changed files with 12 additions and 4 deletions

View file

@ -93,22 +93,25 @@ class FilenameFilter
* Check if a file has an extension that would allow direct download.
*
* @param string $filename
* @param bool $include_multimedia (optional)
* @return bool
*/
public static function isDirectDownload($filename)
public static function isDirectDownload($filename, $include_multimedia = true)
{
$images = 'gif|jpe?g|png|webp';
$audios = 'mp3|wav|ogg|flac|aac';
$videos = 'mp4|webm|ogv';
$legacy = 'avi|as[fx]|flv|m4[av]|midi?|mkv|moo?v|mpe?g|qt|r[am]m?|wm[av]';
if (preg_match(sprintf('/\.(?:%s|%s|%s|%s)$/i', $images, $audios, $videos, $legacy), $filename))
if ($include_multimedia)
{
return true;
$pattern = sprintf('/\.(?:%s|%s|%s|%s)$/i', $images, $audios, $videos, $legacy);
}
else
{
return false;
$pattern = sprintf('/\.(?:%s)$/i', $images);
}
return preg_match($pattern, $filename) ? true : false;
}
}

View file

@ -91,5 +91,10 @@ class FilenameFilterTest extends \Codeception\TestCase\Test
$this->assertFalse(FilenameFilter::isDirectDownload(''));
$this->assertFalse(FilenameFilter::isDirectDownload('http://www.google.com'));
$this->assertFalse(FilenameFilter::isDirectDownload('/'));
$this->assertTrue(FilenameFilter::isDirectDownload('foobar.jpg', false));
$this->assertTrue(FilenameFilter::isDirectDownload('foobar.webp', false));
$this->assertFalse(FilenameFilter::isDirectDownload('foobar.mp4', false));
$this->assertFalse(FilenameFilter::isDirectDownload('foobar.webm', false));
}
}