From 4d011fa5e5d9c00a37fe3d0ae90cd087a0935d7b Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 9 Jan 2024 19:24:56 +0000 Subject: [PATCH] [core] remove add_image, send_event(DataUpload) is much clearer as to what it's actually doing --- core/imageboard/misc.php | 18 +++++------------- ext/bulk_add_csv/main.php | 7 ++++++- ext/bulk_import_export/main.php | 6 +++++- ext/cron_uploader/main.php | 6 +++++- ext/danbooru_api/main.php | 9 +++++++-- ext/eokm/test.php | 13 ------------- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 4730bd8d..d8cfaba6 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -24,7 +24,11 @@ function add_dir(string $base, ?array $extra_tags = []): array $tags = array_merge(path_to_tags($short_path), $extra_tags); try { - $dae = add_image($full_path, $filename, $tags); + $dae = send_event(new DataUploadEvent($full_path, [ + 'filename' => pathinfo($filename, PATHINFO_BASENAME), + 'tags' => $tags, + 'source' => null, + ])); foreach($dae->images as $image) { $results[] = new UploadSuccess($filename, $image->id); } @@ -36,18 +40,6 @@ function add_dir(string $base, ?array $extra_tags = []): array return $results; } -/** - * Sends a DataUploadEvent for a file. - */ -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' => $tags, - 'source' => $source, - ])); -} - function get_file_ext(string $filename): ?string { return pathinfo($filename)['extension'] ?? null; diff --git a/ext/bulk_add_csv/main.php b/ext/bulk_add_csv/main.php index be03a9ae..790be05e 100644 --- a/ext/bulk_add_csv/main.php +++ b/ext/bulk_add_csv/main.php @@ -50,7 +50,12 @@ class BulkAddCSV extends Extension */ private function add_image(string $tmpname, string $filename, array $tags, string $source, string $rating, string $thumbfile) { - $event = add_image($tmpname, $filename, $tags, $source); + $event = send_event(new DataUploadEvent($tmpname, [ + 'filename' => pathinfo($filename, PATHINFO_BASENAME), + 'tags' => $tags, + 'source' => $source, + ])); + if (count($event->images) == 0) { throw new UploadException("File type not recognised"); } else { diff --git a/ext/bulk_import_export/main.php b/ext/bulk_import_export/main.php index 3aa5b7d5..2f3039eb 100644 --- a/ext/bulk_import_export/main.php +++ b/ext/bulk_import_export/main.php @@ -52,7 +52,11 @@ class BulkImportExport extends DataHandlerExtension file_put_contents($tmpfile, $stream); - $images = add_image($tmpfile, $item->filename, $item->new_tags)->images; + $images = send_event(new DataUploadEvent($tmpfile, [ + 'filename' => pathinfo($item->filename, PATHINFO_BASENAME), + 'tags' => $item->new_tags, + 'source' => null, + ]))->images; if (count($images) == 0) { throw new SCoreException("Unable to import file $item->hash"); diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index 10b0fa2c..b3faa61f 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -466,7 +466,11 @@ class CronUploader extends Extension */ private function add_image(string $tmpname, string $filename, array $tags): DataUploadEvent { - $event = add_image($tmpname, $filename, $tags, null); + $event = send_event(new DataUploadEvent($tmpname, [ + 'filename' => pathinfo($filename, PATHINFO_BASENAME), + 'tags' => $tags, + 'source' => null, + ])); // Generate info message if (count($event->images) == 0) { diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index 93510b1d..6cdd6ce3 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -343,10 +343,15 @@ class DanbooruApi extends Extension try { // Fire off an event which should process the new file and add it to the db - $nevent = add_image($file, $filename, $posttags, $source); + $dae = send_event(new DataUploadEvent($file, [ + 'filename' => pathinfo($filename, PATHINFO_BASENAME), + 'tags' => $posttags, + 'source' => $source, + ])); + //log_debug("danbooru_api", "send_event(".var_export($nevent,TRUE).")"); // If it went ok, grab the id for the newly uploaded image and pass it in the header - $newimg = Image::by_hash($hash); // FIXME: Unsupported file doesn't throw an error? + $newimg = $dae->images[0]; $newid = make_link("post/view/" . $newimg->id); if ($danboorup_kludge) { $newid = make_http($newid); diff --git a/ext/eokm/test.php b/ext/eokm/test.php index b71e45e1..6c3d1ff7 100644 --- a/ext/eokm/test.php +++ b/ext/eokm/test.php @@ -15,17 +15,4 @@ class EokmTest extends ShimmiePHPUnitTestCase $this->assert_no_text("Image too small"); $this->assert_no_text("ratio"); } - - /* - public function testFail() - { - $this->log_in_as_user(); - try { - $this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot"); - $this->assertTrue(false, "Invalid-size image was allowed"); - } catch (UploadException $e) { - $this->assertEquals("Image too small", $e->getMessage()); - } - } - */ }