From ff04083ad5454b01a28f5cd8bc9e402301cc84ba Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 4 Jan 2024 15:54:44 +0000 Subject: [PATCH] [handle_archive] merge tags from inside the archive and the form, fixes #483 --- core/imageboard/misc.php | 10 +++++----- core/tests/UtilTest.php | 18 +++++++++--------- core/util.php | 7 +++++-- ext/bulk_add_csv/main.php | 6 +++--- ext/bulk_import_export/main.php | 2 +- ext/cron_uploader/main.php | 2 +- ext/handle_archive/main.php | 2 +- 7 files changed, 25 insertions(+), 22 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 6a96baee..083fdcdc 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -14,7 +14,7 @@ namespace Shimmie2; * @param string $base * @return array */ -function add_dir(string $base): array +function add_dir(string $base, ?array $extra_tags = []): array { $results = []; @@ -22,8 +22,8 @@ function add_dir(string $base): array $short_path = str_replace($base, "", $full_path); $filename = basename($full_path); - $tags = path_to_tags($short_path); - $result = "$short_path (".str_replace(" ", ", ", $tags).")... "; + $tags = array_merge(path_to_tags($short_path), $extra_tags); + $result = "$short_path (".implode(", ", $tags).")... "; try { add_image($full_path, $filename, $tags); $result .= "ok"; @@ -39,11 +39,11 @@ function add_dir(string $base): array /** * Sends a DataUploadEvent for a file. */ -function add_image(string $tmpname, string $filename, string $tags, ?string $source = null): DataUploadEvent +function add_image(string $tmpname, string $filename, array $tags, ?string $source = null): DataUploadEvent { return send_event(new DataUploadEvent($tmpname, [ 'filename' => pathinfo($filename, PATHINFO_BASENAME), - 'tags' => Tag::explode($tags), + 'tags' => $tags, 'source' => $source, ])); } diff --git a/core/tests/UtilTest.php b/core/tests/UtilTest.php index 30fffbf7..e4312307 100644 --- a/core/tests/UtilTest.php +++ b/core/tests/UtilTest.php @@ -126,39 +126,39 @@ class UtilTest extends TestCase 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", + ["test", "tag"], path_to_tags("123 - test tag.jpg") ); $this->assertEquals( - "foo bar", + ["foo", "bar"], path_to_tags("/foo/bar/baz.jpg") ); $this->assertEquals( - "cake pie foo bar", + ["cake", "pie", "foo", "bar"], path_to_tags("/foo/bar/123 - cake pie.jpg") ); $this->assertEquals( - "bacon lemon", + ["bacon", "lemon"], path_to_tags("\\bacon\\lemon\\baz.jpg") ); $this->assertEquals( - "category:tag", + ["category:tag"], path_to_tags("/category:/tag/baz.jpg") ); } diff --git a/core/util.php b/core/util.php index ba18d8bc..62081a4d 100644 --- a/core/util.php +++ b/core/util.php @@ -346,7 +346,10 @@ function fetch_url(string $url, string $mfile): ?array return null; } -function path_to_tags(string $path): string +/** + * @return string[] + */ +function path_to_tags(string $path): array { $matches = []; $tags = []; @@ -390,7 +393,7 @@ function path_to_tags(string $path): string $category = $category_to_inherit; } - return implode(" ", $tags); + return $tags; } function get_dir_contents(string $dir): array diff --git a/ext/bulk_add_csv/main.php b/ext/bulk_add_csv/main.php index f04e93ed..f49e889f 100644 --- a/ext/bulk_add_csv/main.php +++ b/ext/bulk_add_csv/main.php @@ -48,7 +48,7 @@ class BulkAddCSV extends Extension /** * Generate the necessary DataUploadEvent for a given image and tags. */ - private function add_image(string $tmpname, string $filename, string $tags, string $source, string $rating, string $thumbfile) + private function add_image(string $tmpname, string $filename, array $tags, string $source, string $rating, string $thumbfile) { $event = add_image($tmpname, $filename, $tags, $source); if ($event->image_id == -1) { @@ -91,12 +91,12 @@ class BulkAddCSV extends Extension } } $fullpath = $csvdata[0]; - $tags = trim($csvdata[1]); + $tags = Tag::explode(trim($csvdata[1])); $source = $csvdata[2]; $rating = $csvdata[3]; $thumbfile = $csvdata[4]; $shortpath = pathinfo($fullpath, PATHINFO_BASENAME); - $list .= "
".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... "); + $list .= "
".html_escape("$shortpath (".implode(", ", $tags).")... "); if (file_exists($csvdata[0]) && is_file($csvdata[0])) { try { $this->add_image($fullpath, $shortpath, $tags, $source, $rating, $thumbfile); diff --git a/ext/bulk_import_export/main.php b/ext/bulk_import_export/main.php index 3dd921cb..eddd27af 100644 --- a/ext/bulk_import_export/main.php +++ b/ext/bulk_import_export/main.php @@ -52,7 +52,7 @@ class BulkImportExport extends DataHandlerExtension file_put_contents($tmpfile, $stream); - $id = add_image($tmpfile, $item->filename, Tag::implode($item->tags))->image_id; + $id = add_image($tmpfile, $item->filename, $item->tags)->image_id; if ($id == -1) { throw new SCoreException("Unable to import file $item->hash"); diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index 22ef18ed..1f97a0f9 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -512,7 +512,7 @@ class CronUploader extends Extension foreach (new \RecursiveIteratorIterator($ite) as $fullpath => $cur) { if (!is_link($fullpath) && !is_dir($fullpath) && !$this->is_skippable_file($fullpath)) { $relativePath = substr($fullpath, strlen($base)); - $tags = path_to_tags($relativePath); + $tags = Tag::implode(path_to_tags($relativePath)); yield [ 0 => $fullpath, diff --git a/ext/handle_archive/main.php b/ext/handle_archive/main.php index a975643b..291a95d4 100644 --- a/ext/handle_archive/main.php +++ b/ext/handle_archive/main.php @@ -34,7 +34,7 @@ class ArchiveFileHandler extends DataHandlerExtension exec($cmd); if (file_exists($tmpdir)) { try { - $results = add_dir($tmpdir); + $results = add_dir($tmpdir, $event->metadata['tags']); if (count($results) > 0) { $page->flash("Adding files" . implode("\n", $results)); }