[upload] saner error reporting
This commit is contained in:
parent
ec85a0bd76
commit
d04ec4296b
2 changed files with 85 additions and 75 deletions
|
@ -58,6 +58,15 @@ class UploadException extends SCoreException
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UploadError
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $name,
|
||||||
|
public string $error
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main upload class.
|
* Main upload class.
|
||||||
* All files that are uploaded to the site are handled through this class.
|
* All files that are uploaded to the site are handled through this class.
|
||||||
|
@ -209,17 +218,17 @@ class Upload extends Extension
|
||||||
$source = $_POST['source'] ?? null;
|
$source = $_POST['source'] ?? null;
|
||||||
|
|
||||||
if (!empty($_POST["url"])) {
|
if (!empty($_POST["url"])) {
|
||||||
$image_ids = $this->try_transload($_POST["url"], [], $source, $image_id);
|
[$image_ids, $errors] = $this->try_transload($_POST["url"], [], $source, $image_id);
|
||||||
$cache->delete("thumb-block:{$image_id}");
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$this->theme->display_upload_status($page, $image_ids);
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||||
} elseif (count($_FILES) > 0) {
|
} elseif (count($_FILES) > 0) {
|
||||||
$image_ids = $this->try_upload($_FILES["data"], [], $source, $image_id);
|
[$image_ids, $errors] = $this->try_upload($_FILES["data"], [], $source, $image_id);
|
||||||
$cache->delete("thumb-block:{$image_id}");
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$this->theme->display_upload_status($page, $image_ids);
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||||
} elseif (!empty($_GET['url'])) {
|
} elseif (!empty($_GET['url'])) {
|
||||||
$image_ids = $this->try_transload($_GET['url'], [], $source, $image_id);
|
[$image_ids, $errors] = $this->try_transload($_GET['url'], [], $source, $image_id);
|
||||||
$cache->delete("thumb-block:{$image_id}");
|
$cache->delete("thumb-block:{$image_id}");
|
||||||
$this->theme->display_upload_status($page, $image_ids);
|
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||||
} else {
|
} else {
|
||||||
$this->theme->display_replace_page($page, $image_id);
|
$this->theme->display_replace_page($page, $image_id);
|
||||||
}
|
}
|
||||||
|
@ -232,26 +241,37 @@ class Upload extends Extension
|
||||||
$this->theme->display_error(507, "Error", "Can't upload images: disk nearly full");
|
$this->theme->display_error(507, "Error", "Can't upload images: disk nearly full");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if(count($_POST) == 0 && empty($_GET['url'])) {
|
||||||
|
$this->theme->display_page($page);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Regular Upload Image */
|
$all_image_ids = [];
|
||||||
if (count($_FILES) > 0 || count($_POST) > 0) {
|
$all_errors = [];
|
||||||
$image_ids = [];
|
|
||||||
|
|
||||||
foreach ($_FILES as $name => $file) {
|
$files = array_filter($_FILES, function ($file) {
|
||||||
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 4)));
|
return !empty($file['name']);
|
||||||
$source = $_POST['source'] ?? null;
|
});
|
||||||
$image_ids += $this->try_upload($file, $tags, $source);
|
$urls = array_filter($_POST, function ($value, $key) {
|
||||||
}
|
return str_starts_with($key, "url") && strlen($value) > 0;
|
||||||
foreach ($_POST as $name => $value) {
|
}, ARRAY_FILTER_USE_BOTH);
|
||||||
if (str_starts_with($name, "url") && strlen($value) > 0) {
|
|
||||||
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 3)));
|
|
||||||
$source = $_POST['source'] ?? $value;
|
|
||||||
$image_ids += $this->try_transload($value, $tags, $source);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->theme->display_upload_status($page, $image_ids);
|
foreach ($files as $name => $file) {
|
||||||
} elseif (!empty($_GET['url'])) {
|
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 4)));
|
||||||
|
$source = $_POST['source'] ?? null;
|
||||||
|
[$image_ids, $errors] = $this->try_upload($file, $tags, $source);
|
||||||
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
||||||
|
$all_errors = array_merge($all_errors, $errors);
|
||||||
|
}
|
||||||
|
foreach ($urls as $name => $value) {
|
||||||
|
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 3)));
|
||||||
|
$source = $_POST['source'] ?? $value;
|
||||||
|
[$image_ids, $errors] = $this->try_transload($value, $tags, $source);
|
||||||
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
||||||
|
$all_errors = array_merge($all_errors, $errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($_GET['url'])) {
|
||||||
$url = $_GET['url'];
|
$url = $_GET['url'];
|
||||||
$source = $_GET['source'] ?? $url;
|
$source = $_GET['source'] ?? $url;
|
||||||
$tags = ['tagme'];
|
$tags = ['tagme'];
|
||||||
|
@ -259,11 +279,12 @@ class Upload extends Extension
|
||||||
$tags = Tag::explode($_GET['tags']);
|
$tags = Tag::explode($_GET['tags']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_ids = $this->try_transload($url, $tags, $source);
|
[$image_ids, $errors] = $this->try_transload($url, $tags, $source);
|
||||||
$this->theme->display_upload_status($page, $image_ids);
|
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
||||||
} else {
|
$all_errors = array_merge($all_errors, $errors);
|
||||||
$this->theme->display_page($page);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->theme->display_upload_status($page, $all_image_ids, $all_errors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,12 +348,13 @@ class Upload extends Extension
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_ids = [];
|
$image_ids = [];
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
$num_files = count($file['name']);
|
$num_files = count($file['name']);
|
||||||
$limit = $config->get_int(UploadConfig::COUNT);
|
$limit = $config->get_int(UploadConfig::COUNT);
|
||||||
try {
|
try {
|
||||||
if ($num_files > $limit) {
|
if ($num_files > $limit) {
|
||||||
throw new UploadException("Upload limited to $limit");
|
throw new UploadException("Upload limited to $limit files at a time");
|
||||||
}
|
}
|
||||||
|
|
||||||
for ($i = 0; $i < $num_files; $i++) {
|
for ($i = 0; $i < $num_files; $i++) {
|
||||||
|
@ -358,22 +380,14 @@ class Upload extends Extension
|
||||||
$image_ids[] = $event->image_id;
|
$image_ids[] = $event->image_id;
|
||||||
$page->add_http_header("X-Shimmie-Post-ID: " . $event->image_id);
|
$page->add_http_header("X-Shimmie-Post-ID: " . $event->image_id);
|
||||||
} catch (UploadException $ex) {
|
} catch (UploadException $ex) {
|
||||||
$this->theme->display_upload_error(
|
$errors[] = new UploadError($file['name'][$i], $ex->getMessage());
|
||||||
$page,
|
|
||||||
"Error with " . html_escape($file['name'][$i]),
|
|
||||||
$ex->getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (UploadException $ex) {
|
} catch (UploadException $ex) {
|
||||||
$this->theme->display_upload_error(
|
$errors[] = new UploadError('unknown', $ex->getMessage());
|
||||||
$page,
|
|
||||||
"Error with upload",
|
|
||||||
$ex->getMessage()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $image_ids;
|
return [$image_ids, $errors];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function try_transload(string $url, array $tags, string $source = null, ?int $replace_id = null): array
|
private function try_transload(string $url, array $tags, string $source = null, ?int $replace_id = null): array
|
||||||
|
@ -381,6 +395,7 @@ class Upload extends Extension
|
||||||
global $page, $config, $user;
|
global $page, $config, $user;
|
||||||
|
|
||||||
$image_ids = [];
|
$image_ids = [];
|
||||||
|
$errors = [];
|
||||||
$tmp_filename = tempnam(ini_get('upload_tmp_dir'), "shimmie_transload");
|
$tmp_filename = tempnam(ini_get('upload_tmp_dir'), "shimmie_transload");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -419,17 +434,13 @@ class Upload extends Extension
|
||||||
}
|
}
|
||||||
$image_ids[] = $event->image_id;
|
$image_ids[] = $event->image_id;
|
||||||
} catch (UploadException $ex) {
|
} catch (UploadException $ex) {
|
||||||
$this->theme->display_upload_error(
|
$errors[] = new UploadError($url, $ex->getMessage());
|
||||||
$page,
|
|
||||||
"Error with " . html_escape($url),
|
|
||||||
$ex->getMessage()
|
|
||||||
);
|
|
||||||
} finally {
|
} finally {
|
||||||
if (file_exists($tmp_filename)) {
|
if (file_exists($tmp_filename)) {
|
||||||
unlink($tmp_filename);
|
unlink($tmp_filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $image_ids;
|
return [$image_ids, $errors];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,6 @@ use function MicroHTML\P;
|
||||||
|
|
||||||
class UploadTheme extends Themelet
|
class UploadTheme extends Themelet
|
||||||
{
|
{
|
||||||
protected bool $has_errors = false;
|
|
||||||
|
|
||||||
public function display_block(Page $page): void
|
public function display_block(Page $page): void
|
||||||
{
|
{
|
||||||
$b = new Block("Upload", (string)$this->build_upload_block(), "left", 20);
|
$b = new Block("Upload", (string)$this->build_upload_block(), "left", 20);
|
||||||
|
@ -71,8 +69,8 @@ class UploadTheme extends Themelet
|
||||||
);
|
);
|
||||||
$html = emptyHTML(
|
$html = emptyHTML(
|
||||||
$form,
|
$form,
|
||||||
SMALL("(Max file size is $max_kb)"),
|
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||||
SMALL(BR(), "(Max total size is $max_total_kb)"),
|
$max_total_size > 0 ? SMALL(BR(), "(Max total size is $max_total_kb)") : null,
|
||||||
rawHTML("<script>
|
rawHTML("<script>
|
||||||
function fileSize(size){
|
function fileSize(size){
|
||||||
var i = Math.floor(Math.log(size) / Math.log(1024));
|
var i = Math.floor(Math.log(size) / Math.log(1024));
|
||||||
|
@ -91,7 +89,7 @@ class UploadTheme extends Themelet
|
||||||
cancelbtn.style.visibility = 'visible';
|
cancelbtn.style.visibility = 'visible';
|
||||||
for (var i = 0; i<n.files.length; i++){
|
for (var i = 0; i<n.files.length; i++){
|
||||||
size += n.files[i].size;
|
size += n.files[i].size;
|
||||||
if (n.files[i].size > $max_size){
|
if ($max_size > 0 && n.files[i].size > $max_size){
|
||||||
toobig = true;
|
toobig = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,7 +107,7 @@ class UploadTheme extends Themelet
|
||||||
|
|
||||||
if (size){
|
if (size){
|
||||||
tracker.innerText = 'Total: ' + fileSize(size);
|
tracker.innerText = 'Total: ' + fileSize(size);
|
||||||
if (size > $max_total_size){
|
if ($max_total_size > 0 && size > $max_total_size){
|
||||||
lockbtn = true;
|
lockbtn = true;
|
||||||
tracker.style = 'color:red';
|
tracker.style = 'color:red';
|
||||||
}else{
|
}else{
|
||||||
|
@ -273,7 +271,7 @@ class UploadTheme extends Themelet
|
||||||
$thumbnail,
|
$thumbnail,
|
||||||
BR(),
|
BR(),
|
||||||
$form,
|
$form,
|
||||||
SMALL("(Max file size is $max_kb)"),
|
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||||
);
|
);
|
||||||
|
|
||||||
$page->set_title("Replace Post");
|
$page->set_title("Replace Post");
|
||||||
|
@ -282,37 +280,38 @@ class UploadTheme extends Themelet
|
||||||
$page->add_block(new Block("Upload Replacement Post", $html, "main", 20));
|
$page->add_block(new Block("Upload Replacement Post", $html, "main", 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_upload_status(Page $page, array $image_ids): void
|
/**
|
||||||
|
* @param int[] $image_ids
|
||||||
|
* @param UploadError[] $errors
|
||||||
|
*/
|
||||||
|
public function display_upload_status(Page $page, array $image_ids, array $errors): void
|
||||||
{
|
{
|
||||||
global $user;
|
global $user;
|
||||||
|
|
||||||
if ($this->has_errors) {
|
if (count($errors) > 0) {
|
||||||
$page->set_title("Upload Status");
|
$page->set_title("Upload Status");
|
||||||
$page->set_heading("Upload Status");
|
$page->set_heading("Upload Status");
|
||||||
$page->add_block(new NavBlock());
|
$page->add_block(new NavBlock());
|
||||||
} else {
|
foreach($errors as $error) {
|
||||||
if (count($image_ids) < 1) {
|
$message = $error->error;
|
||||||
$page->set_title("No images uploaded");
|
// this message has intentional HTML in it...
|
||||||
$page->set_heading("No images uploaded");
|
$message = str_contains($message, "already has hash") ? $message : html_escape($message);
|
||||||
$page->add_block(new NavBlock());
|
$page->add_block(new Block($error->name, $message));
|
||||||
} elseif (count($image_ids) == 1) {
|
|
||||||
$page->set_mode(PageMode::REDIRECT);
|
|
||||||
$page->set_redirect(make_link("post/view/{$image_ids[0]}"));
|
|
||||||
} else {
|
|
||||||
$page->set_mode(PageMode::REDIRECT);
|
|
||||||
$page->set_redirect(search_link(["poster={$user->name}"]));
|
|
||||||
}
|
}
|
||||||
|
} elseif (count($image_ids) == 0) {
|
||||||
|
$page->set_title("No images uploaded");
|
||||||
|
$page->set_heading("No images uploaded");
|
||||||
|
$page->add_block(new NavBlock());
|
||||||
|
$page->add_block(new Block("No images uploaded", "Upload attempted, but nothing succeeded and nothing failed?"));
|
||||||
|
} elseif (count($image_ids) == 1) {
|
||||||
|
$page->set_mode(PageMode::REDIRECT);
|
||||||
|
$page->set_redirect(make_link("post/view/{$image_ids[0]}"));
|
||||||
|
} else {
|
||||||
|
$page->set_mode(PageMode::REDIRECT);
|
||||||
|
$page->set_redirect(search_link(["poster={$user->name}"]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display_upload_error(Page $page, string $title, string $message): void
|
|
||||||
{
|
|
||||||
// this message has intentional HTML in it...
|
|
||||||
$message = str_contains($message, "already has hash") ? $message : html_escape($message);
|
|
||||||
$page->add_block(new Block($title, $message));
|
|
||||||
$this->has_errors = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function build_upload_block(): HTMLElement
|
protected function build_upload_block(): HTMLElement
|
||||||
{
|
{
|
||||||
global $config;
|
global $config;
|
||||||
|
@ -337,8 +336,8 @@ class UploadTheme extends Themelet
|
||||||
return DIV(
|
return DIV(
|
||||||
["class" => 'mini_upload'],
|
["class" => 'mini_upload'],
|
||||||
$form,
|
$form,
|
||||||
SMALL("(Max file size is $max_kb)"),
|
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||||
SMALL(BR(), "(Max total size is $max_total_kb)"),
|
$max_total_size > 0 ? SMALL(BR(), "(Max total size is $max_total_kb)") : null,
|
||||||
NOSCRIPT(BR(), A(["href" => make_link("upload")], "Larger Form"))
|
NOSCRIPT(BR(), A(["href" => make_link("upload")], "Larger Form"))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue