mirror of
https://github.com/standardebooks/web.git
synced 2025-07-07 07:10:29 -04:00
Add the HttpInput::File() and HttpInput::Date() method
This commit is contained in:
parent
f28378de37
commit
98f45ea4e0
4 changed files with 46 additions and 28 deletions
8
lib/Exceptions/InvalidFileUploadException.php
Normal file
8
lib/Exceptions/InvalidFileUploadException.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Exceptions;
|
||||||
|
|
||||||
|
class InvalidFileUploadException extends AppException{
|
||||||
|
/** @var string $message */
|
||||||
|
protected $message = 'Uploaded file is invalid.';
|
||||||
|
}
|
|
@ -107,6 +107,30 @@ class HttpInput{
|
||||||
return self::GetHttpVar($variable, HttpVariableType::Decimal, $set);
|
return self::GetHttpVar($variable, HttpVariableType::Decimal, $set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function Date(HttpVariableSource $set, string $variable): ?DateTimeImmutable{
|
||||||
|
/** @var ?DateTimeImmutable */
|
||||||
|
return self::GetHttpVar($variable, HttpVariableType::DateTime, $set);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the absolute path of the requested file upload, or `null` if there isn't one.
|
||||||
|
*
|
||||||
|
* @throws Exceptions\InvalidFileUploadException If there is a file upload present, but the upload somehow failed.
|
||||||
|
*/
|
||||||
|
public static function File(string $variable): ?string{
|
||||||
|
$filePath = null;
|
||||||
|
|
||||||
|
if(isset($_FILES[$variable]) && $_FILES[$variable]['size'] > 0){
|
||||||
|
if(!is_uploaded_file($_FILES[$variable]['tmp_name']) || $_FILES[$variable]['error'] > UPLOAD_ERR_OK){
|
||||||
|
throw new Exceptions\InvalidFileUploadException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$filePath = $_FILES[$variable]['tmp_name'] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filePath;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $variable
|
* @param string $variable
|
||||||
* @return array<string>
|
* @return array<string>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
enum HttpVariableType{
|
enum HttpVariableType{
|
||||||
case Array;
|
case Array;
|
||||||
case Boolean;
|
case Boolean;
|
||||||
|
case DateTime;
|
||||||
case Decimal;
|
case Decimal;
|
||||||
case Integer;
|
case Integer;
|
||||||
case String;
|
case String;
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
|
use Exceptions\InvalidImageUploadException;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
session_start();
|
session_start();
|
||||||
$httpMethod = HttpInput::ValidateRequestMethod([HttpMethod::Post, HttpMethod::Patch, HttpMethod::Put]);
|
$httpMethod = HttpInput::ValidateRequestMethod([HttpMethod::Post, HttpMethod::Patch, HttpMethod::Put]);
|
||||||
|
@ -32,17 +35,7 @@ try{
|
||||||
$artwork->ReviewerUserId = $GLOBALS['User']->UserId;
|
$artwork->ReviewerUserId = $GLOBALS['User']->UserId;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirm that we have an image and that it came from POST
|
$artwork->Create(HttpInput::File('artwork-image'));
|
||||||
$imagePath = null;
|
|
||||||
if(isset($_FILES['artwork-image']) && $_FILES['artwork-image']['size'] > 0){
|
|
||||||
if(!is_uploaded_file($_FILES['artwork-image']['tmp_name']) || $_FILES['artwork-image']['error'] > UPLOAD_ERR_OK){
|
|
||||||
throw new Exceptions\InvalidImageUploadException();
|
|
||||||
}
|
|
||||||
|
|
||||||
$imagePath = $_FILES['artwork-image']['tmp_name'] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$artwork->Create($imagePath);
|
|
||||||
|
|
||||||
$_SESSION['artwork'] = $artwork;
|
$_SESSION['artwork'] = $artwork;
|
||||||
$_SESSION['artwork-created'] = true;
|
$_SESSION['artwork-created'] = true;
|
||||||
|
@ -77,21 +70,7 @@ try{
|
||||||
$artwork->Status = $newStatus;
|
$artwork->Status = $newStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Confirm that we have an image and that it came from POST
|
$artwork->Save(HttpInput::File('artwork-image'));
|
||||||
$imagePath = null;
|
|
||||||
if(isset($_FILES['artwork-image']) && $_FILES['artwork-image']['size'] > 0){
|
|
||||||
if(!is_uploaded_file($_FILES['artwork-image']['tmp_name']) || $_FILES['artwork-image']['error'] > UPLOAD_ERR_OK){
|
|
||||||
throw new Exceptions\InvalidImageUploadException();
|
|
||||||
}
|
|
||||||
|
|
||||||
$imagePath = $_FILES['artwork-image']['tmp_name'] ?? null;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
// No uploaded file as part of this edit, so retain the MimeType of the original submission.
|
|
||||||
$artwork->MimeType = $originalArtwork->MimeType;
|
|
||||||
}
|
|
||||||
|
|
||||||
$artwork->Save($imagePath);
|
|
||||||
|
|
||||||
$_SESSION['artwork'] = $artwork;
|
$_SESSION['artwork'] = $artwork;
|
||||||
$_SESSION['artwork-saved'] = true;
|
$_SESSION['artwork-saved'] = true;
|
||||||
|
@ -100,7 +79,7 @@ try{
|
||||||
header('Location: ' . $artwork->Url);
|
header('Location: ' . $artwork->Url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PATCHing a new artwork
|
// PATCHing an artwork
|
||||||
if($httpMethod == HttpMethod::Patch){
|
if($httpMethod == HttpMethod::Patch){
|
||||||
$artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name'));
|
$artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name'));
|
||||||
|
|
||||||
|
@ -147,7 +126,13 @@ catch(Exceptions\InvalidPermissionsException){
|
||||||
catch(Exceptions\ArtworkNotFoundException){
|
catch(Exceptions\ArtworkNotFoundException){
|
||||||
Template::Emit404();
|
Template::Emit404();
|
||||||
}
|
}
|
||||||
catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException | Exceptions\InvalidArtistException | Exceptions\InvalidImageUploadException | Exceptions\ArtworkNotFoundException $ex){
|
catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException | Exceptions\InvalidArtistException | Exceptions\InvalidFileUploadException | Exceptions\ArtworkNotFoundException $ex){
|
||||||
|
|
||||||
|
// If we were passed a more generic file upload exception from `HttpInput`, swap it for a more specific exception to show to the user.
|
||||||
|
if($ex instanceof Exceptions\InvalidFileUploadException){
|
||||||
|
$ex = new InvalidImageUploadException();
|
||||||
|
}
|
||||||
|
|
||||||
$_SESSION['artwork'] = $artwork;
|
$_SESSION['artwork'] = $artwork;
|
||||||
$_SESSION['exception'] = $ex;
|
$_SESSION['exception'] = $ex;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue