[handle_archive] merge tags from inside the archive and the form, fixes #483

This commit is contained in:
Shish 2024-01-04 15:54:44 +00:00
parent e49fcfa0c7
commit ff04083ad5
7 changed files with 25 additions and 22 deletions

View file

@ -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,
]));
}

View file

@ -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")
);
}

View file

@ -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

View file

@ -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 .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
$list .= "<br>".html_escape("$shortpath (".implode(", ", $tags).")... ");
if (file_exists($csvdata[0]) && is_file($csvdata[0])) {
try {
$this->add_image($fullpath, $shortpath, $tags, $source, $rating, $thumbfile);

View file

@ -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");

View file

@ -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,

View file

@ -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));
}