diff --git a/inc/display.php b/inc/display.php
index c836247e..a859fbdb 100644
--- a/inc/display.php
+++ b/inc/display.php
@@ -253,6 +253,16 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) {
return $body;
}
+function truncate_filename($filename, $length) {
+ if (strlen($filename) <= $length) return $filename;
+ $matches = array();
+ if (!preg_match("/.*(\\..+)/", $filename, $matches)) return $filename // what, no extension
+ $length -= strlen($matches[1]);
+ if ($length <= 0) return '(...)' . $matches[1]; // lmao
+ $filename = substr($filename, 0, $length) . '(...)' . $matches[1];
+ return $filename;
+}
+
function bidi_cleanup($data) {
// Closes all embedded RTL and LTR unicode formatting blocks in a string so that
// it can be used inside another without controlling its direction.
diff --git a/inc/lib/Twig/Extensions/Extension/Tinyboard.php b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
index 960bb844..e5dff993 100644
--- a/inc/lib/Twig/Extensions/Extension/Tinyboard.php
+++ b/inc/lib/Twig/Extensions/Extension/Tinyboard.php
@@ -13,6 +13,7 @@ class Twig_Extensions_Extension_Tinyboard extends Twig_Extension
new Twig_SimpleFilter('filesize', 'format_bytes'),
new Twig_SimpleFilter('truncate', 'twig_truncate_filter'),
new Twig_SimpleFilter('truncate_body', 'truncate'),
+ new Twig_SimpleFilter('truncate_filename', 'truncate_filename'),
new Twig_SimpleFilter('extension', 'twig_extension_filter'),
new Twig_SimpleFilter('sprintf', 'sprintf'),
new Twig_SimpleFilter('capcode', 'capcode'),
diff --git a/templates/post/fileinfo.html b/templates/post/fileinfo.html
index dcbb249d..c2a0aa6a 100644
--- a/templates/post/fileinfo.html
+++ b/templates/post/fileinfo.html
@@ -22,7 +22,7 @@
{% if config.show_filename and file.filename %}
,
{% if file.filename|length > config.max_filename_display %}
- {{ file.filename|truncate(config.max_filename_display)|bidi_cleanup }}
+ {{ file.filename|truncate_filename(config.max_filename_display)|bidi_cleanup }}
{% else %}
{{ file.filename|e|bidi_cleanup }}
{% endif %}