[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.
|
||||
* 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;
|
||||
|
||||
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}");
|
||||
$this->theme->display_upload_status($page, $image_ids);
|
||||
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||
} 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}");
|
||||
$this->theme->display_upload_status($page, $image_ids);
|
||||
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||
} 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}");
|
||||
$this->theme->display_upload_status($page, $image_ids);
|
||||
$this->theme->display_upload_status($page, $image_ids, $errors);
|
||||
} else {
|
||||
$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");
|
||||
return;
|
||||
}
|
||||
if(count($_POST) == 0 && empty($_GET['url'])) {
|
||||
$this->theme->display_page($page);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Regular Upload Image */
|
||||
if (count($_FILES) > 0 || count($_POST) > 0) {
|
||||
$image_ids = [];
|
||||
$all_image_ids = [];
|
||||
$all_errors = [];
|
||||
|
||||
foreach ($_FILES as $name => $file) {
|
||||
$tags = $this->tags_for_upload_slot(int_escape(substr($name, 4)));
|
||||
$source = $_POST['source'] ?? null;
|
||||
$image_ids += $this->try_upload($file, $tags, $source);
|
||||
}
|
||||
foreach ($_POST as $name => $value) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
$files = array_filter($_FILES, function ($file) {
|
||||
return !empty($file['name']);
|
||||
});
|
||||
$urls = array_filter($_POST, function ($value, $key) {
|
||||
return str_starts_with($key, "url") && strlen($value) > 0;
|
||||
}, ARRAY_FILTER_USE_BOTH);
|
||||
|
||||
$this->theme->display_upload_status($page, $image_ids);
|
||||
} elseif (!empty($_GET['url'])) {
|
||||
foreach ($files as $name => $file) {
|
||||
$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'];
|
||||
$source = $_GET['source'] ?? $url;
|
||||
$tags = ['tagme'];
|
||||
|
@ -259,11 +279,12 @@ class Upload extends Extension
|
|||
$tags = Tag::explode($_GET['tags']);
|
||||
}
|
||||
|
||||
$image_ids = $this->try_transload($url, $tags, $source);
|
||||
$this->theme->display_upload_status($page, $image_ids);
|
||||
} else {
|
||||
$this->theme->display_page($page);
|
||||
[$image_ids, $errors] = $this->try_transload($url, $tags, $source);
|
||||
$all_image_ids = array_merge($all_image_ids, $image_ids);
|
||||
$all_errors = array_merge($all_errors, $errors);
|
||||
}
|
||||
|
||||
$this->theme->display_upload_status($page, $all_image_ids, $all_errors);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -327,12 +348,13 @@ class Upload extends Extension
|
|||
}
|
||||
|
||||
$image_ids = [];
|
||||
$errors = [];
|
||||
|
||||
$num_files = count($file['name']);
|
||||
$limit = $config->get_int(UploadConfig::COUNT);
|
||||
try {
|
||||
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++) {
|
||||
|
@ -358,22 +380,14 @@ class Upload extends Extension
|
|||
$image_ids[] = $event->image_id;
|
||||
$page->add_http_header("X-Shimmie-Post-ID: " . $event->image_id);
|
||||
} catch (UploadException $ex) {
|
||||
$this->theme->display_upload_error(
|
||||
$page,
|
||||
"Error with " . html_escape($file['name'][$i]),
|
||||
$ex->getMessage()
|
||||
);
|
||||
$errors[] = new UploadError($file['name'][$i], $ex->getMessage());
|
||||
}
|
||||
}
|
||||
} catch (UploadException $ex) {
|
||||
$this->theme->display_upload_error(
|
||||
$page,
|
||||
"Error with upload",
|
||||
$ex->getMessage()
|
||||
);
|
||||
$errors[] = new UploadError('unknown', $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
|
||||
|
@ -381,6 +395,7 @@ class Upload extends Extension
|
|||
global $page, $config, $user;
|
||||
|
||||
$image_ids = [];
|
||||
$errors = [];
|
||||
$tmp_filename = tempnam(ini_get('upload_tmp_dir'), "shimmie_transload");
|
||||
|
||||
try {
|
||||
|
@ -419,17 +434,13 @@ class Upload extends Extension
|
|||
}
|
||||
$image_ids[] = $event->image_id;
|
||||
} catch (UploadException $ex) {
|
||||
$this->theme->display_upload_error(
|
||||
$page,
|
||||
"Error with " . html_escape($url),
|
||||
$ex->getMessage()
|
||||
);
|
||||
$errors[] = new UploadError($url, $ex->getMessage());
|
||||
} finally {
|
||||
if (file_exists($tmp_filename)) {
|
||||
unlink($tmp_filename);
|
||||
}
|
||||
}
|
||||
|
||||
return $image_ids;
|
||||
return [$image_ids, $errors];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,8 +22,6 @@ use function MicroHTML\P;
|
|||
|
||||
class UploadTheme extends Themelet
|
||||
{
|
||||
protected bool $has_errors = false;
|
||||
|
||||
public function display_block(Page $page): void
|
||||
{
|
||||
$b = new Block("Upload", (string)$this->build_upload_block(), "left", 20);
|
||||
|
@ -71,8 +69,8 @@ class UploadTheme extends Themelet
|
|||
);
|
||||
$html = emptyHTML(
|
||||
$form,
|
||||
SMALL("(Max file size is $max_kb)"),
|
||||
SMALL(BR(), "(Max total size is $max_total_kb)"),
|
||||
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||
$max_total_size > 0 ? SMALL(BR(), "(Max total size is $max_total_kb)") : null,
|
||||
rawHTML("<script>
|
||||
function fileSize(size){
|
||||
var i = Math.floor(Math.log(size) / Math.log(1024));
|
||||
|
@ -91,7 +89,7 @@ class UploadTheme extends Themelet
|
|||
cancelbtn.style.visibility = 'visible';
|
||||
for (var i = 0; i<n.files.length; i++){
|
||||
size += n.files[i].size;
|
||||
if (n.files[i].size > $max_size){
|
||||
if ($max_size > 0 && n.files[i].size > $max_size){
|
||||
toobig = true;
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +107,7 @@ class UploadTheme extends Themelet
|
|||
|
||||
if (size){
|
||||
tracker.innerText = 'Total: ' + fileSize(size);
|
||||
if (size > $max_total_size){
|
||||
if ($max_total_size > 0 && size > $max_total_size){
|
||||
lockbtn = true;
|
||||
tracker.style = 'color:red';
|
||||
}else{
|
||||
|
@ -273,7 +271,7 @@ class UploadTheme extends Themelet
|
|||
$thumbnail,
|
||||
BR(),
|
||||
$form,
|
||||
SMALL("(Max file size is $max_kb)"),
|
||||
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||
);
|
||||
|
||||
$page->set_title("Replace Post");
|
||||
|
@ -282,37 +280,38 @@ class UploadTheme extends Themelet
|
|||
$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;
|
||||
|
||||
if ($this->has_errors) {
|
||||
if (count($errors) > 0) {
|
||||
$page->set_title("Upload Status");
|
||||
$page->set_heading("Upload Status");
|
||||
$page->add_block(new NavBlock());
|
||||
} else {
|
||||
if (count($image_ids) < 1) {
|
||||
$page->set_title("No images uploaded");
|
||||
$page->set_heading("No images uploaded");
|
||||
$page->add_block(new NavBlock());
|
||||
} 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}"]));
|
||||
foreach($errors as $error) {
|
||||
$message = $error->error;
|
||||
// this message has intentional HTML in it...
|
||||
$message = str_contains($message, "already has hash") ? $message : html_escape($message);
|
||||
$page->add_block(new Block($error->name, $message));
|
||||
}
|
||||
} 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
|
||||
{
|
||||
global $config;
|
||||
|
@ -337,8 +336,8 @@ class UploadTheme extends Themelet
|
|||
return DIV(
|
||||
["class" => 'mini_upload'],
|
||||
$form,
|
||||
SMALL("(Max file size is $max_kb)"),
|
||||
SMALL(BR(), "(Max total size is $max_total_kb)"),
|
||||
$max_size > 0 ? SMALL("(Max file size is $max_kb)") : null,
|
||||
$max_total_size > 0 ? SMALL(BR(), "(Max total size is $max_total_kb)") : null,
|
||||
NOSCRIPT(BR(), A(["href" => make_link("upload")], "Larger Form"))
|
||||
);
|
||||
}
|
||||
|
|
Reference in a new issue