From a828c3e0e5bf3455aa56a92862460bae6fe4d933 Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 27 Oct 2022 16:56:12 +0100 Subject: [PATCH] more thorough testing for path_to_tags, and handle more edge cases --- core/imageboard/misc.php | 3 --- core/tests/util.test.php | 40 ++++++++++++++++++++++++++++++++++++++++ core/util.php | 7 +++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index d1816e5f..a1f66eca 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -20,9 +20,6 @@ function add_dir(string $base): array $filename = basename($full_path); $tags = path_to_tags($short_path); - if ($tags[0] == "\\") { - $tags = ""; - } $result = "$short_path (".str_replace(" ", ", ", $tags).")... "; try { add_image($full_path, $filename, $tags); diff --git a/core/tests/util.test.php b/core/tests/util.test.php index aea73971..bf2906b9 100644 --- a/core/tests/util.test.php +++ b/core/tests/util.test.php @@ -85,4 +85,44 @@ class UtilTest extends TestCase load_balance_url("https://{foo=10,bar=5,baz=5}.mycdn.com/$hash.$ext", $hash, 1) ); } + + public function test_path_to_tags() + { + $this->assertEquals( + "", + path_to_tags("nope.jpg") + ); + $this->assertEquals( + "", + path_to_tags("\\") + ); + $this->assertEquals( + "", + path_to_tags("/") + ); + $this->assertEquals( + "", + path_to_tags("C:\\") + ); + $this->assertEquals( + "test tag", + path_to_tags("123 - test tag.jpg") + ); + $this->assertEquals( + "foo bar", + path_to_tags("/foo/bar/baz.jpg") + ); + $this->assertEquals( + "cake pie foo bar", + path_to_tags("/foo/bar/123 - cake pie.jpg") + ); + $this->assertEquals( + "bacon lemon", + path_to_tags("\\bacon\\lemon\\baz.jpg") + ); + $this->assertEquals( + "category:tag", + path_to_tags("/category:/tag/baz.jpg") + ); + } } diff --git a/core/util.php b/core/util.php index ab60089b..2e27e16f 100644 --- a/core/util.php +++ b/core/util.php @@ -354,10 +354,13 @@ function path_to_tags(string $path): string $tags = explode(" ", $matches[1]); } - $path = dirname($path); + $path = str_replace("\\", "/", $path); $path = str_replace(";", ":", $path); $path = str_replace("__", " ", $path); - + $path = dirname($path); + if ($path == "\\" || $path == "/" || $path == ".") { + $path = ""; + } $category = ""; foreach (explode("/", $path) as $dir) {