diff --git a/core/extension.php b/core/extension.php
index 3822d9d1..7ef28fb2 100644
--- a/core/extension.php
+++ b/core/extension.php
@@ -313,43 +313,20 @@ abstract class DataHandlerExtension extends Extension
if (is_null($existing)) {
throw new UploadException("Post to replace does not exist!");
}
- if ($existing->hash === $event->metadata['hash']) {
+ if ($existing->hash === $event->hash) {
throw new UploadException("The uploaded post is the same as the one to replace.");
}
// even more hax..
$event->metadata['tags'] = $existing->get_tag_list();
- $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->metadata['hash']), $event->metadata);
- if (is_null($image)) {
- throw new UploadException("Data handler failed to create post object from data");
- }
- if (empty($image->get_mime())) {
- throw new UploadException("Unable to determine MIME for ". $event->tmpname);
- }
- try {
- send_event(new MediaCheckPropertiesEvent($image));
- } catch (MediaException $e) {
- throw new UploadException("Unable to scan media properties: ".$e->getMessage());
- }
+ $image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata);
send_event(new ImageReplaceEvent($event->replace_id, $image));
$_id = $event->replace_id;
assert(!is_null($_id));
$event->image_id = $_id;
} else {
$image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata);
- if (is_null($image)) {
- throw new UploadException("Data handler failed to create post object from data");
- }
- if (empty($image->get_mime())) {
- throw new UploadException("Unable to determine MIME for ". $event->tmpname);
- }
- try {
- send_event(new MediaCheckPropertiesEvent($image));
- } catch (MediaException $e) {
- throw new UploadException("Unable to scan media properties: ".$e->getMessage());
- }
-
$iae = send_event(new ImageAdditionEvent($image));
$event->image_id = $iae->image->id;
$event->merged = $iae->merged;
@@ -402,7 +379,7 @@ abstract class DataHandlerExtension extends Extension
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event)
{
- if ($this->supported_mime($event->mime)) {
+ if ($this->supported_mime($event->image->get_mime())) {
$this->media_check_properties($event);
}
}
@@ -411,19 +388,23 @@ abstract class DataHandlerExtension extends Extension
{
$image = new Image();
- $image->filesize = $metadata['size'];
- $image->hash = $metadata['hash'];
+ assert(is_readable($filename));
+ $image->filesize = filesize($filename);
+ $image->hash = md5_file($filename);
$image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename'];
-
- if (array_key_exists("extension", $metadata)) {
- $image->set_mime(MimeType::get_for_file($filename, $metadata["extension"]));
- } else {
- $image->set_mime(MimeType::get_for_file($filename));
- }
-
+ $image->set_mime(MimeType::get_for_file($filename, get_file_ext($metadata["filename"]) ?? null));
$image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']);
$image->source = $metadata['source'];
+ if (empty($image->get_mime())) {
+ throw new UploadException("Unable to determine MIME for $filename");
+ }
+ try {
+ send_event(new MediaCheckPropertiesEvent($image));
+ } catch (MediaException $e) {
+ throw new UploadException("Unable to scan media properties $filename / $image->filename / $image->hash: ".$e->getMessage());
+ }
+
return $image;
}
diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php
index b65938ab..e9944606 100644
--- a/core/imageboard/misc.php
+++ b/core/imageboard/misc.php
@@ -39,22 +39,18 @@ function add_dir(string $base): array
/**
* Sends a DataUploadEvent for a file.
*/
-function add_image(string $tmpname, string $filename, string $tags): int
+function add_image(string $tmpname, string $filename, string $tags, ?string $source=null): DataUploadEvent
{
- assert(file_exists($tmpname));
+ return send_event(new DataUploadEvent($tmpname, [
+ 'filename' => pathinfo($filename, PATHINFO_BASENAME),
+ 'tags' => Tag::explode($tags),
+ 'source' => $source,
+ ]));
+}
- $pathinfo = pathinfo($filename);
- $metadata = [];
- $metadata['filename'] = $pathinfo['basename'];
- if (array_key_exists('extension', $pathinfo)) {
- $metadata['extension'] = $pathinfo['extension'];
- }
-
- $metadata['tags'] = Tag::explode($tags);
- $metadata['source'] = null;
-
- $due = send_event(new DataUploadEvent($tmpname, $metadata));
- return $due->image_id;
+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 5bdb9252..6dfcb488 100644
--- a/ext/bulk_add_csv/main.php
+++ b/ext/bulk_add_csv/main.php
@@ -50,17 +50,7 @@ class BulkAddCSV extends Extension
*/
private function add_image(string $tmpname, string $filename, string $tags, string $source, string $rating, string $thumbfile)
{
- assert(file_exists($tmpname));
-
- $pathinfo = pathinfo($filename);
- $metadata = [];
- $metadata['filename'] = $pathinfo['basename'];
- if (array_key_exists('extension', $pathinfo)) {
- $metadata['extension'] = $pathinfo['extension'];
- }
- $metadata['tags'] = Tag::explode($tags);
- $metadata['source'] = $source;
- $event = send_event(new DataUploadEvent($tmpname, $metadata));
+ $event = add_image($tmpname, $filename, $tags, $source);
if ($event->image_id == -1) {
throw new UploadException("File type not recognised");
} else {
@@ -105,12 +95,11 @@ class BulkAddCSV extends Extension
$source = $csvdata[2];
$rating = $csvdata[3];
$thumbfile = $csvdata[4];
- $pathinfo = pathinfo($fullpath);
- $shortpath = $pathinfo["basename"];
+ $shortpath = pathinfo($fullpath, PATHINFO_BASENAME);
$list .= "
".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
if (file_exists($csvdata[0]) && is_file($csvdata[0])) {
try {
- $this->add_image($fullpath, $pathinfo["basename"], $tags, $source, $rating, $thumbfile);
+ $this->add_image($fullpath, $shortpath, $tags, $source, $rating, $thumbfile);
$list .= "ok\n";
} catch (\Exception $ex) {
$list .= "failed:
". $ex->getMessage();
diff --git a/ext/bulk_import_export/main.php b/ext/bulk_import_export/main.php
index 03982ae2..5f635784 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));
+ $id = add_image($tmpfile, $item->filename, Tag::implode($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 5e49a03d..8da3c467 100644
--- a/ext/cron_uploader/main.php
+++ b/ext/cron_uploader/main.php
@@ -466,29 +466,11 @@ class CronUploader extends Extension
*/
private function add_image(string $tmpname, string $filename, string $tags): DataUploadEvent
{
- assert(file_exists($tmpname));
-
- $tagArray = Tag::explode($tags);
- if (count($tagArray) == 0) {
- $tagArray[] = "tagme";
- }
-
- $pathinfo = pathinfo($filename);
- $metadata = [];
- $metadata ['filename'] = $pathinfo ['basename'];
- if (array_key_exists('extension', $pathinfo)) {
- $metadata ['extension'] = $pathinfo ['extension'];
- }
- $metadata ['tags'] = $tagArray;
- $metadata ['source'] = null;
- $event = send_event(new DataUploadEvent($tmpname, $metadata));
+ $event = add_image($tmpname, $filename, $tags, null);
// Generate info message
if ($event->image_id == -1) {
- if (array_key_exists("mime", $event->metadata)) {
- throw new UploadException("File type not recognised (".$event->metadata["mime"]."). Filename: {$filename}");
- }
- throw new UploadException("File type not recognised. Filename: {$filename}");
+ throw new UploadException("File type not recognised (".$event->mime."). Filename: {$filename}");
} elseif ($event->merged === true) {
$infomsg = "Post merged. ID: {$event->image_id} - Filename: {$filename}";
} else {
@@ -529,14 +511,12 @@ class CronUploader extends Extension
$ite = new \RecursiveDirectoryIterator($base, \FilesystemIterator::SKIP_DOTS);
foreach (new \RecursiveIteratorIterator($ite) as $fullpath => $cur) {
if (!is_link($fullpath) && !is_dir($fullpath) && !$this->is_skippable_file($fullpath)) {
- $pathinfo = pathinfo($fullpath);
-
$relativePath = substr($fullpath, strlen($base));
$tags = path_to_tags($relativePath);
yield [
0 => $fullpath,
- 1 => $pathinfo ["basename"],
+ 1 => pathinfo($fullpath, PATHINFO_BASENAME),
2 => $tags
];
}
diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php
index 9f0a84f8..6b46ef20 100644
--- a/ext/danbooru_api/main.php
+++ b/ext/danbooru_api/main.php
@@ -336,22 +336,13 @@ class DanbooruApi extends Extension
return;
}
- // Fire off an event which should process the new file and add it to the db
- $fileinfo = pathinfo($filename);
- $metadata = [];
- $metadata['filename'] = $fileinfo['basename'];
- if (array_key_exists('extension', $fileinfo)) {
- $metadata['extension'] = $fileinfo['extension'];
- }
- $metadata['tags'] = $posttags;
- $metadata['source'] = $source;
//log_debug("danbooru_api","========== NEW($filename) =========");
//log_debug("danbooru_api", "upload($filename): fileinfo(".var_export($fileinfo,TRUE)."), metadata(".var_export($metadata,TRUE).")...");
try {
- $nevent = new DataUploadEvent($file, $metadata);
+ // Fire off an event which should process the new file and add it to the db
+ $nevent = add_image($file, $filename, $posttags, $source);
//log_debug("danbooru_api", "send_event(".var_export($nevent,TRUE).")");
- send_event($nevent);
// 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?
$newid = make_link("post/view/" . $newimg->id);
diff --git a/ext/emoticons_list/theme.php b/ext/emoticons_list/theme.php
index d545e03e..1f01a34b 100644
--- a/ext/emoticons_list/theme.php
+++ b/ext/emoticons_list/theme.php
@@ -14,8 +14,7 @@ class EmoticonListTheme extends Themelet
$html .= "
:$name: | "; if ($n++ % 3 == 0) { $html .= "