refactor a bunch of weirdness in image replacement
This commit is contained in:
parent
2cae6cd273
commit
72645af9a4
2 changed files with 21 additions and 46 deletions
|
@ -298,14 +298,11 @@ abstract class DataHandlerExtension extends Extension
|
||||||
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
send_event(new ThumbnailGenerationEvent($event->hash, $event->type));
|
||||||
|
|
||||||
/* Check if we are replacing an image */
|
/* Check if we are replacing an image */
|
||||||
if (array_key_exists('replace', $event->metadata) && isset($event->metadata['replace'])) {
|
if (!is_null($event->replace_id)) {
|
||||||
/* hax: This seems like such a dirty way to do this.. */
|
/* hax: This seems like such a dirty way to do this.. */
|
||||||
|
|
||||||
/* Validate things */
|
|
||||||
$image_id = int_escape($event->metadata['replace']);
|
|
||||||
|
|
||||||
/* Check to make sure the image exists. */
|
/* Check to make sure the image exists. */
|
||||||
$existing = Image::by_id($image_id);
|
$existing = Image::by_id($event->replace_id);
|
||||||
|
|
||||||
if (is_null($existing)) {
|
if (is_null($existing)) {
|
||||||
throw new UploadException("Image to replace does not exist!");
|
throw new UploadException("Image to replace does not exist!");
|
||||||
|
@ -326,8 +323,8 @@ abstract class DataHandlerExtension extends Extension
|
||||||
throw new UploadException("Unable to scan media properties: ".$e->getMessage());
|
throw new UploadException("Unable to scan media properties: ".$e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
send_event(new ImageReplaceEvent($image_id, $image));
|
send_event(new ImageReplaceEvent($event->replace_id, $image));
|
||||||
$event->image_id = $image_id;
|
$event->image_id = $event->replace_id;
|
||||||
} else {
|
} else {
|
||||||
$image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata);
|
$image = $this->create_image_from_data(warehouse_path(Image::IMAGE_DIR, $event->hash), $event->metadata);
|
||||||
if (is_null($image)) {
|
if (is_null($image)) {
|
||||||
|
|
|
@ -15,6 +15,8 @@ class DataUploadEvent extends Event
|
||||||
public $type = "";
|
public $type = "";
|
||||||
/** @var int */
|
/** @var int */
|
||||||
public $image_id = -1;
|
public $image_id = -1;
|
||||||
|
/** @var int */
|
||||||
|
public $replace_id = null;
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public $handled = false;
|
public $handled = false;
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
|
@ -192,49 +194,33 @@ class Upload extends Extension
|
||||||
if ($this->is_full) {
|
if ($this->is_full) {
|
||||||
throw new UploadException("Can not replace Image: disk nearly full");
|
throw new UploadException("Can not replace Image: disk nearly full");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get the image ID
|
// Try to get the image ID
|
||||||
if ($event->count_args() >= 1) {
|
if ($event->count_args() >= 1) {
|
||||||
$image_id = int_escape($event->get_arg(0));
|
$image_id = int_escape($event->get_arg(0));
|
||||||
} elseif (isset($_POST['image_id'])) {
|
} elseif (isset($_POST['image_id'])) {
|
||||||
$image_id = $_POST['image_id'];
|
$image_id = int_escape($_POST['image_id']);
|
||||||
} else {
|
} else {
|
||||||
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
throw new UploadException("Can not replace Image: No valid Image ID given.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_old = Image::by_id($image_id);
|
$image_old = Image::by_id($image_id);
|
||||||
if (is_null($image_old)) {
|
if (is_null($image_old)) {
|
||||||
$this->theme->display_error(404, "Image not found", "No image in the database has the ID #$image_id");
|
throw new UploadException("Can not replace Image: No image with ID $image_id");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($_FILES) + count($_POST) > 0) {
|
$source = $_POST['source'] ?? null;
|
||||||
if (count($_FILES) > 1) {
|
|
||||||
throw new UploadException("Can not upload more than one image for replacing.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$source = isset($_POST['source']) ? $_POST['source'] : null;
|
if (!empty($_POST["url"])) {
|
||||||
$tags = []; // Tags aren't changed when replacing. Set to empty to stop PHP warnings.
|
$ok = $this->try_transload($_POST["url"], [], $source, $image_id);
|
||||||
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$ok = false;
|
$this->theme->display_upload_status($page, $ok);
|
||||||
if (count($_FILES)) {
|
} elseif (count($_FILES) > 0) {
|
||||||
foreach ($_FILES as $file) {
|
$ok = $this->try_upload($_FILES["data"], [], $source, $image_id);
|
||||||
$ok = $this->try_upload($file, $tags, $source, $image_id);
|
|
||||||
break; // leave the foreach loop.
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
foreach ($_POST as $name => $value) {
|
|
||||||
if (substr($name, 0, 3) == "url" && strlen($value) > 0) {
|
|
||||||
$ok = $this->try_transload($value, $tags, $source, $image_id);
|
|
||||||
break; // leave the foreach loop.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$cache->delete("thumb-block:{$image_id}");
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$this->theme->display_upload_status($page, $ok);
|
$this->theme->display_upload_status($page, $ok);
|
||||||
} elseif (!empty($_GET['url'])) {
|
} elseif (!empty($_GET['url'])) {
|
||||||
$url = $_GET['url'];
|
$ok = $this->try_transload($_GET['url'], [], $source, $image_id);
|
||||||
$tags = isset($_GET['tags']) ? Tag::explode($_GET['tags']) : 'tagme';
|
|
||||||
$source = isset($_GET['source']) ? $_GET['source'] : $url;
|
|
||||||
$ok = $this->try_transload($url, $tags, $source, $image_id);
|
|
||||||
$cache->delete("thumb-block:{$image_id}");
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$this->theme->display_upload_status($page, $ok);
|
$this->theme->display_upload_status($page, $ok);
|
||||||
} else {
|
} else {
|
||||||
|
@ -332,7 +318,7 @@ class Upload extends Extension
|
||||||
* #param string[] $file
|
* #param string[] $file
|
||||||
* #param string[] $tags
|
* #param string[] $tags
|
||||||
*/
|
*/
|
||||||
private function try_upload(array $file, array $tags, ?string $source = null, int $replace = -1): bool
|
private function try_upload(array $file, array $tags, ?string $source = null, ?int $replace_id = null): bool
|
||||||
{
|
{
|
||||||
global $page;
|
global $page;
|
||||||
|
|
||||||
|
@ -359,12 +345,8 @@ class Upload extends Extension
|
||||||
$metadata['tags'] = $tags;
|
$metadata['tags'] = $tags;
|
||||||
$metadata['source'] = $source;
|
$metadata['source'] = $source;
|
||||||
|
|
||||||
/* check if we have been given an image ID to replace */
|
|
||||||
if ($replace >= 0) {
|
|
||||||
$metadata['replace'] = $replace;
|
|
||||||
}
|
|
||||||
|
|
||||||
$event = new DataUploadEvent($file['tmp_name'], $metadata);
|
$event = new DataUploadEvent($file['tmp_name'], $metadata);
|
||||||
|
$event->replace_id = $replace_id;
|
||||||
send_event($event);
|
send_event($event);
|
||||||
if ($event->image_id == -1) {
|
if ($event->image_id == -1) {
|
||||||
throw new UploadException("File type not supported: " . $metadata['extension']);
|
throw new UploadException("File type not supported: " . $metadata['extension']);
|
||||||
|
@ -383,7 +365,7 @@ class Upload extends Extension
|
||||||
return $ok;
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function try_transload(string $url, array $tags, string $source = null, int $replace = -1): bool
|
private function try_transload(string $url, array $tags, string $source = null, ?int $replace_id = null): bool
|
||||||
{
|
{
|
||||||
global $page, $config, $user;
|
global $page, $config, $user;
|
||||||
|
|
||||||
|
@ -454,13 +436,9 @@ class Upload extends Extension
|
||||||
$metadata['rating'] = $rating;
|
$metadata['rating'] = $rating;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check if we have been given an image ID to replace */
|
|
||||||
if ($replace >= 0) {
|
|
||||||
$metadata['replace'] = $replace;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$event = new DataUploadEvent($tmp_filename, $metadata);
|
$event = new DataUploadEvent($tmp_filename, $metadata);
|
||||||
|
$event->replace_id = $replace_id;
|
||||||
send_event($event);
|
send_event($event);
|
||||||
if ($event->image_id == -1) {
|
if ($event->image_id == -1) {
|
||||||
throw new UploadException("File type not supported: " . $metadata['extension']);
|
throw new UploadException("File type not supported: " . $metadata['extension']);
|
||||||
|
|
Reference in a new issue