Removed JsonSerializable implementations.

This commit is contained in:
flash 2023-11-07 22:05:14 +00:00
parent cd1de984d4
commit d669221869
4 changed files with 42 additions and 60 deletions

View file

@ -34,6 +34,26 @@ function eepromOriginAllowed(string $origin): bool {
return in_array($origin, $allowed); return in_array($origin, $allowed);
} }
function eepromUploadInfo(Upload $uploadInfo): array {
return [
'id' => $uploadInfo->getId(),
'url' => $uploadInfo->getPublicUrl(),
'urlf' => $uploadInfo->getPublicUrl(true),
'thumb' => $uploadInfo->getPublicThumbUrl(),
'name' => $uploadInfo->getName(),
'type' => $uploadInfo->getType(),
'size' => $uploadInfo->getSize(),
'user' => $uploadInfo->getUserId(),
'appl' => $uploadInfo->getApplicationId(),
'hash' => $uploadInfo->getHash(),
'created' => date('c', $uploadInfo->getCreated()),
'accessed' => $uploadInfo->hasBeenAccessed() ? date('c', $uploadInfo->getLastAccessed()) : null,
'expires' => $uploadInfo->hasExpired() ? date('c', $uploadInfo->getExpires()) : null,
'deleted' => $uploadInfo->isDeleted() ? date('c', $uploadInfo->getDeleted()) : null,
'dmca' => $uploadInfo->isDMCA() ? date('c', $uploadInfo->getDMCA()) : null,
];
}
$isApiDomain = $_SERVER['HTTP_HOST'] === $cfg->getString('domain:api'); $isApiDomain = $_SERVER['HTTP_HOST'] === $cfg->getString('domain:api');
$router = new HttpFx; $router = new HttpFx;
@ -174,23 +194,23 @@ if($isApiDomain) {
$hash = hash_file('sha256', $localFile); $hash = hash_file('sha256', $localFile);
// this is stupid: dmca status is stored as a file record rather than in a separate table requiring this hack ass garbage // this is stupid: dmca status is stored as a file record rather than in a separate table requiring this hack ass garbage
$fileInfo = Upload::byUserHash($db, $userInfo, $hash) ?? Upload::byHash($db, $hash); $uploadInfo = Upload::byUserHash($db, $userInfo, $hash) ?? Upload::byHash($db, $hash);
if($fileInfo !== null) { if($uploadInfo !== null) {
if($fileInfo->isDMCA()) if($uploadInfo->isDMCA())
return 451; return 451;
if($fileInfo->getUserId() !== $userInfo->getId() if($uploadInfo->getUserId() !== $userInfo->getId()
|| $fileInfo->getApplicationId() !== $appInfo->getId()) || $uploadInfo->getApplicationId() !== $appInfo->getId())
unset($fileInfo); unset($uploadInfo);
} }
if(!empty($fileInfo)) { if(!empty($uploadInfo)) {
if($fileInfo->isDeleted()) if($uploadInfo->isDeleted())
$fileInfo->restore($db); $uploadInfo->restore($db);
$fileInfo->bumpExpiry($db); $uploadInfo->bumpExpiry($db);
} else { } else {
$fileInfo = Upload::create( $uploadInfo = Upload::create(
$db, $appInfo, $userInfo, $db, $appInfo, $userInfo,
$file->getSuggestedFileName(), $file->getSuggestedFileName(),
mime_content_type($localFile), mime_content_type($localFile),
@ -198,12 +218,13 @@ if($isApiDomain) {
$appInfo->getExpiry(), true $appInfo->getExpiry(), true
); );
$file->moveTo($fileInfo->getPath()); $file->moveTo($uploadInfo->getPath());
} }
$response->setStatusCode(201); $response->setStatusCode(201);
$response->setHeader('Content-Type', 'application/json; charset=utf-8'); $response->setHeader('Content-Type', 'application/json; charset=utf-8');
return $fileInfo;
return eepromUploadInfo($uploadInfo);
}); });
$router->delete('/uploads/:fileid', function($response, $request, $fileId) use ($db) { $router->delete('/uploads/:fileid', function($response, $request, $fileId) use ($db) {
@ -252,7 +273,7 @@ if($isApiDomain) {
} }
if($isJson) if($isJson)
return $uploadInfo; return eepromUploadInfo($uploadInfo);
if($uploadInfo->isDMCA()) { if($uploadInfo->isDMCA()) {
$response->setContent('File is unavailable for copyright reasons.'); $response->setContent('File is unavailable for copyright reasons.');

View file

@ -2,10 +2,9 @@
namespace EEPROM; namespace EEPROM;
use RuntimeException; use RuntimeException;
use JsonSerializable;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
final class Application implements JsonSerializable { final class Application {
public function __construct( public function __construct(
private int $id = 0, private int $id = 0,
private string $name = '', private string $name = '',
@ -39,17 +38,6 @@ final class Application implements JsonSerializable {
return $this->expiry; return $this->expiry;
} }
public function jsonSerialize(): mixed {
return [
'id' => $this->id,
'name' => $this->name,
'size_limit' => $this->sizeLimit,
'size_multi' => $this->allowSizeMultiplier,
'expiry' => $this->expiry,
'created' => date('c', $this->created),
];
}
public static function byId(IDbConnection $conn, int $appId): self { public static function byId(IDbConnection $conn, int $appId): self {
$get = $conn->prepare( $get = $conn->prepare(
'SELECT `app_id`, `app_name`, `app_size_limit`, `app_expiry`, `app_allow_size_multiplier`,' 'SELECT `app_id`, `app_name`, `app_size_limit`, `app_expiry`, `app_allow_size_multiplier`,'

View file

@ -5,12 +5,11 @@ use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Imagick; use Imagick;
use JsonSerializable;
use Index\XString; use Index\XString;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
use Index\Data\IDbResult; use Index\Data\IDbResult;
final class Upload implements JsonSerializable { final class Upload {
public function __construct( public function __construct(
private string $id = '', private string $id = '',
private int $userId = 0, private int $userId = 0,
@ -89,6 +88,10 @@ final class Upload implements JsonSerializable {
return $this->created; return $this->created;
} }
public function hasBeenAccessed(): bool {
return $this->accessed < 1;
}
public function getLastAccessed(): int { public function getLastAccessed(): int {
return $this->accessed; return $this->accessed;
} }
@ -175,26 +178,6 @@ final class Upload implements JsonSerializable {
} }
} }
public function jsonSerialize(): mixed {
return [
'id' => $this->id,
'url' => $this->getPublicUrl(),
'urlf' => $this->getPublicUrl(true),
'thumb' => $this->getPublicThumbUrl(),
'name' => $this->name,
'type' => $this->type,
'size' => $this->size,
'user' => $this->userId,
'appl' => $this->appId,
'hash' => $this->hash,
'created' => date('c', $this->created),
'accessed' => $this->accessed < 1 ? null : date('c', $this->accessed),
'expires' => $this->expires < 1 ? null : date('c', $this->expires),
'deleted' => $this->deleted < 1 ? null : date('c', $this->deleted),
'dmca' => $this->dmca < 1 ? null : date('c', $this->dmca),
];
}
public static function create( public static function create(
IDbConnection $conn, IDbConnection $conn,
Application $app, User $user, Application $app, User $user,

View file

@ -2,10 +2,9 @@
namespace EEPROM; namespace EEPROM;
use RuntimeException; use RuntimeException;
use JsonSerializable;
use Index\Data\IDbConnection; use Index\Data\IDbConnection;
class User implements JsonSerializable { class User {
private static $active; private static $active;
public static function hasActive(): bool { public static function hasActive(): bool {
@ -50,15 +49,6 @@ class User implements JsonSerializable {
return $this->restricted > 0; return $this->restricted > 0;
} }
public function jsonSerialize(): mixed {
return [
'id' => $this->id,
'size_multi' => $this->sizeMultiplier,
'created' => date('c', $this->created),
'restricted' => $this->restricted < 1 ? null : date('c', $this->restricted),
];
}
public static function byId(IDbConnection $conn, int $userId): self { public static function byId(IDbConnection $conn, int $userId): self {
$create = $conn->prepare('INSERT IGNORE INTO `prm_users` (`user_id`) VALUES (?)'); $create = $conn->prepare('INSERT IGNORE INTO `prm_users` (`user_id`) VALUES (?)');
$create->addParameter(1, $userId); $create->addParameter(1, $userId);