Additional Ebook validation

This commit is contained in:
Mike Colagrosso 2024-04-26 23:52:28 -06:00 committed by Alex Cabal
parent 3ef1af1237
commit 032032b920
22 changed files with 262 additions and 2 deletions

View file

@ -32,7 +32,9 @@ const DATABASE_DEFAULT_HOST = 'localhost';
const EBOOKS_PER_PAGE = 12; const EBOOKS_PER_PAGE = 12;
const EBOOKS_MAX_STRING_LENGTH = 250; const EBOOKS_MAX_STRING_LENGTH = 250;
const EBOOKS_MAX_LONG_STRING_LENGTH = 500;
const EBOOK_SINGLE_PAGE_SIZE_WARNING = 3 *1024 * 1024; // 3145728 const EBOOK_SINGLE_PAGE_SIZE_WARNING = 3 *1024 * 1024; // 3145728
const EBOOK_EARLIEST_CREATION_DATE = new DateTimeImmutable('2014-01-01');
const ARTWORK_THUMBNAIL_HEIGHT = 350; const ARTWORK_THUMBNAIL_HEIGHT = 350;
const ARTWORK_THUMBNAIL_WIDTH = 350; const ARTWORK_THUMBNAIL_WIDTH = 350;

View file

@ -581,16 +581,87 @@ class Ebook{
// ******* // *******
public function Validate(): void{ public function Validate(): void{
$now = new DateTimeImmutable();
$error = new Exceptions\ValidationException(); $error = new Exceptions\ValidationException();
if($this->Identifier == ''){ if($this->Identifier == ''){
$error->Add(new Exceptions\EbookIdentifierRequiredException()); $error->Add(new Exceptions\EbookIdentifierRequiredException());
} }
if(strlen($this->Identifier) > EBOOKS_MAX_STRING_LENGTH){ if(strlen($this->Identifier) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Identifier')); $error->Add(new Exceptions\StringTooLongException('Ebook Identifier'));
} }
if(!is_readable($this->WwwFilesystemPath)){
$error->Add(new Exceptions\InvalidEbookWwwFilesystemPathException($this->WwwFilesystemPath));
}
if(strlen($this->WwwFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook WwwFilesystemPath'));
}
if(!is_readable($this->RepoFilesystemPath)){
$error->Add(new Exceptions\InvalidEbookRepoFilesystemPathException($this->RepoFilesystemPath));
}
if(strlen($this->RepoFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook RepoFilesystemPath'));
}
if($this->KindleCoverUrl !== null && !preg_match('|/*_EBOK_portrait.jpg|ius', $this->KindleCoverUrl)){
$error->Add(new Exceptions\InvalidEbookKindleCoverUrlException('Invalid Ebook KindleCoverUrl: ' . $this->KindleCoverUrl));
}
if($this->KindleCoverUrl !== null && strlen($this->KindleCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KindleCoverUrl'));
}
if($this->EpubUrl !== null && !preg_match('|/*.epub|ius', $this->EpubUrl)){
$error->Add(new Exceptions\InvalidEbookEpubUrlException('Invalid Ebook EpubUrl: ' . $this->EpubUrl));
}
if($this->EpubUrl !== null && strlen($this->EpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook EpubUrl'));
}
if($this->AdvancedEpubUrl !== null && !preg_match('|/*_advanced.epub|ius', $this->AdvancedEpubUrl)){
$error->Add(new Exceptions\InvalidEbookAdvancedEpubUrlException('Invalid Ebook AdvancedEpubUrl: ' . $this->AdvancedEpubUrl));
}
if($this->AdvancedEpubUrl !== null && strlen($this->AdvancedEpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook AdvancedEpubUrl'));
}
if($this->KepubUrl !== null && !preg_match('|/*.kepub.epub|ius', $this->KepubUrl)){
$error->Add(new Exceptions\InvalidEbookKepubUrlException('Invalid Ebook KepubUrl: ' . $this->KepubUrl));
}
if($this->KepubUrl !== null && strlen($this->KepubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KepubUrl'));
}
if($this->Azw3Url !== null && !preg_match('|/*.azw3|ius', $this->Azw3Url)){
$error->Add(new Exceptions\InvalidEbookAzw3UrlException('Invalid Ebook Azw3Url: ' . $this->Azw3Url));
}
if($this->Azw3Url !== null && strlen($this->Azw3Url) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Azw3Url'));
}
if($this->DistCoverUrl !== null && !preg_match('|/*cover.jpg|ius', $this->DistCoverUrl)){
$error->Add(new Exceptions\InvalidEbookDistCoverUrlException('Invalid Ebook DistCoverUrl: ' . $this->DistCoverUrl));
}
if($this->DistCoverUrl !== null && strlen($this->DistCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook DistCoverUrl'));
}
if($this->Title === null || $this->Title == ''){ if($this->Title === null || $this->Title == ''){
$error->Add(new Exceptions\EbookTitleRequiredException()); $error->Add(new Exceptions\EbookTitleRequiredException());
} }
@ -599,7 +670,69 @@ class Ebook{
$error->Add(new Exceptions\StringTooLongException('Ebook Title')); $error->Add(new Exceptions\StringTooLongException('Ebook Title'));
} }
// TODO: Add more validation. if($this->FullTitle !== null && strlen($this->FullTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook FullTitle'));
}
if($this->AlternateTitle !== null && strlen($this->AlternateTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook AlternateTitle'));
}
if($this->Description === null || $this->Description == ''){
$error->Add(new Exceptions\EbookDescriptionRequiredException());
}
if($this->LongDescription === null || $this->LongDescription == ''){
$error->Add(new Exceptions\EbookLongDescriptionRequiredException());
}
if($this->Language !== null && strlen($this->Language) > 10){
$error->Add(new Exceptions\StringTooLongException('Ebook Language: ' . $this->Language));
}
if($this->WordCount <= 0){
$error->Add(new Exceptions\InvalidEbookWordCountException('Invalid Ebook WordCount: ' . $this->WordCount));
}
// In theory, Flesch reading ease can be negative, but in practice it's positive.
if($this->ReadingEase <= 0){
$error->Add(new Exceptions\InvalidEbookReadingEaseException('Invalid Ebook ReadingEase: ' . $this->ReadingEase));
}
if($this->GitHubUrl !== null && !preg_match('|https://github.com/standardebooks/\w+|ius', $this->GitHubUrl)){
$error->Add(new Exceptions\InvalidEbookGitHubUrlException('Invalid Ebook GitHubUrl: ' . $this->GitHubUrl));
}
if($this->GitHubUrl !== null && strlen($this->GitHubUrl) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook GitHubUrl'));
}
if($this->WikipediaUrl !== null && !preg_match('|https://.*wiki.*|ius', $this->WikipediaUrl)){
$error->Add(new Exceptions\InvalidEbookWikipediaUrlException('Invalid Ebook WikipediaUrl: ' . $this->WikipediaUrl));
}
if($this->WikipediaUrl !== null && strlen($this->WikipediaUrl) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook WikipediaUrl'));
}
if($this->Created > $now || $this->Created < EBOOK_EARLIEST_CREATION_DATE){
$error->Add(new Exceptions\InvalidEbookCreatedDatetimeException($this->Created));
}
if($this->Updated > $now || $this->Updated < EBOOK_EARLIEST_CREATION_DATE){
$error->Add(new Exceptions\InvalidEbookUpdatedDatetimeException($this->Updated));
}
if($this->TextSinglePageByteCount === null || $this->TextSinglePageByteCount <= 0){
$error->Add(new Exceptions\InvalidEbookTextSinglePageByteCountException('Invalid Ebook TextSinglePageByteCount: ' . $this->TextSinglePageByteCount));
}
if($this->IndexableText === null || $this->IndexableText == ''){
$error->Add(new Exceptions\EbookIndexableTextRequiredException());
}
if($error->HasExceptions){ if($error->HasExceptions){
throw $error; throw $error;

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class EbookDescriptionRequiredException extends AppException{
protected $message = 'Ebook Description required.';
}

View file

@ -2,4 +2,5 @@
namespace Exceptions; namespace Exceptions;
class EbookIdentifierRequiredException extends AppException{ class EbookIdentifierRequiredException extends AppException{
protected $message = 'Ebook Identifier required.';
} }

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class EbookIndexableTextRequiredException extends AppException{
protected $message = 'Ebook IndexableText required.';
}

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class EbookLongDescriptionRequiredException extends AppException{
protected $message = 'Ebook LongDescription required.';
}

View file

@ -2,4 +2,5 @@
namespace Exceptions; namespace Exceptions;
class EbookTitleRequiredException extends AppException{ class EbookTitleRequiredException extends AppException{
protected $message = 'Ebook Title required.';
} }

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookAdvancedEpubUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookAzw3UrlException extends AppException{
}

View file

@ -0,0 +1,13 @@
<?
namespace Exceptions;
use Safe\DateTimeImmutable;
class InvalidEbookCreatedDatetimeException extends AppException{
protected $message = 'Invalid EbookCreated datetime.';
public function __construct(DateTimeImmutable $createdDatetime){
$now = new DateTimeImmutable();
$this->message = 'Invalid EbookCreated datetime. ' . $createdDatetime->format('Y-m-d') . ' is not between ' . EBOOK_EARLIEST_CREATION_DATE->format('Y-m-d') . ' and ' . $now->format('Y-m-d') . '.';
}
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookDistCoverUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookEpubUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookGitHubUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookKepubUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookKindleCoverUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookReadingEaseException extends AppException{
}

View file

@ -0,0 +1,12 @@
<?
namespace Exceptions;
use Safe\DateTimeImmutable;
class InvalidEbookRepoFilesystemPathException extends AppException{
protected $message = 'Invalid RepoFilesystemPath.';
public function __construct(?string $path){
$this->message = 'Invalid RepoFilesystemPath. Not readable: ' . $path;
}
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookTextSinglePageByteCountException extends AppException{
}

View file

@ -0,0 +1,13 @@
<?
namespace Exceptions;
use Safe\DateTimeImmutable;
class InvalidEbookUpdatedDatetimeException extends AppException{
protected $message = 'Invalid EbookUpdated datetime.';
public function __construct(DateTimeImmutable $updatedDatetime){
$now = new DateTimeImmutable();
$this->message = 'Invalid EbookUpdated datetime. ' . $updatedDatetime->format('Y-m-d') . ' is not between ' . EBOOK_EARLIEST_CREATION_DATE->format('Y-m-d') . ' and ' . $now->format('Y-m-d') . '.';
}
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookWikipediaUrlException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class InvalidEbookWordCountException extends AppException{
}

View file

@ -0,0 +1,12 @@
<?
namespace Exceptions;
use Safe\DateTimeImmutable;
class InvalidEbookWwwFilesystemPathException extends AppException{
protected $message = 'Invalid WwwFilesystemPath.';
public function __construct(?string $path){
$this->message = 'Invalid WwwFilesystemPath. Not readable: ' . $path;
}
}