From 9bd114e5b65e08bf9ff7105f53ae33971f09a1e0 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 15 Sep 2014 17:09:42 +0200 Subject: [PATCH] Moved base64 decoding to FormData --- src/FormData/UserEditFormData.php | 2 +- src/Helpers/InputReader.php | 8 +++++++ src/Services/FileService.php | 38 ++++++++++++++----------------- src/Services/UserService.php | 4 ++-- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/FormData/UserEditFormData.php b/src/FormData/UserEditFormData.php index db23b832..241c8fd0 100644 --- a/src/FormData/UserEditFormData.php +++ b/src/FormData/UserEditFormData.php @@ -22,7 +22,7 @@ class UserEditFormData implements \Szurubooru\IValidatable $this->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankFromString($inputReader->accessRank); if ($inputReader->avatarStyle !== null) $this->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleFromString($inputReader->avatarStyle); - $this->avatarContent = $inputReader->avatarContent; + $this->avatarContent = $inputReader->decodeBase64($inputReader->avatarContent); $this->browsingSettings = $inputReader->browsingSettings; } } diff --git a/src/Helpers/InputReader.php b/src/Helpers/InputReader.php index d3e5d568..f7ff3d44 100644 --- a/src/Helpers/InputReader.php +++ b/src/Helpers/InputReader.php @@ -24,4 +24,12 @@ final class InputReader extends \ArrayObject return null; return parent::offsetGet($index); } + + public function decodeBase64($base64string) + { + $commaPosition = strpos($base64string, ','); + if ($commaPosition !== null) + $base64string = substr($base64string, $commaPosition + 1); + return base64_decode($base64string); + } } diff --git a/src/Services/FileService.php b/src/Services/FileService.php index 3094a238..71d1be48 100644 --- a/src/Services/FileService.php +++ b/src/Services/FileService.php @@ -12,17 +12,17 @@ class FileService $this->httpHelper = $httpHelper; } - public function serve($source, $options = []) + public function serve($target, $options = []) { - $finalSource = $this->getFullPath($source); + $fullPath = $this->getFullPath($target); $daysToLive = isset($options->daysToLive) ? $options->daysToLive : 7; $secondsToLive = $daysToLive * 24 * 60 * 60; - $lastModified = filemtime($finalSource); - $eTag = md5(file_get_contents($finalSource)); //todo: faster + $lastModified = filemtime($fullPath); + $eTag = md5(file_get_contents($fullPath)); //todo: faster $ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] @@ -45,7 +45,7 @@ class FileService 'Content-Type', isset($options->mimeType) ? $options->mimeType - : mime_content_type($finalSource)); + : mime_content_type($fullPath)); if (strtotime($ifModifiedSince) === $lastModified or $eTagHeader === $eTag) { @@ -54,38 +54,34 @@ class FileService else { $this->httpHelper->setResponseCode(200); - readfile($finalSource); + readfile($fullPath); } exit; } public function createFolders($target) { - $finalTarget = $this->getFullPath($target); - if (!file_exists($finalTarget)) - mkdir($finalTarget, 0777, true); + $fullPath = $this->getFullPath($target); + if (!file_exists($fullPath)) + mkdir($fullPath, 0777, true); } - public function exists($source) + public function exists($target) { - $finalSource = $this->getFullPath($source); - return $source and file_exists($finalSource); + $fullPath = $this->getFullPath($target); + return $target and file_exists($fullPath); } - public function delete($source) + public function delete($target) { - $finalSource = $this->getFullPath($source); - if (file_exists($finalSource)) - unlink($finalSource); + $fullPath = $this->getFullPath($target); + if (file_exists($fullPath)) + unlink($fullPath); } - public function saveFromBase64($base64string, $destination) + public function save($destination, $data) { $finalDestination = $this->getFullPath($destination); - $commaPosition = strpos($base64string, ','); - if ($commaPosition !== null) - $base64string = substr($base64string, $commaPosition + 1); - $data = base64_decode($base64string); file_put_contents($finalDestination, $data); } diff --git a/src/Services/UserService.php b/src/Services/UserService.php index 1697b0ae..9cd3ebfa 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -216,10 +216,10 @@ class UserService $user->setAvatarStyle($newAvatarStyle); } - private function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContentInBase64) + private function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContent) { $target = $this->getCustomAvatarSourcePath($user); - $this->fileService->saveFromBase64($newAvatarContentInBase64, $target); + $this->fileService->save($target, $newAvatarContent); $this->thumbnailService->deleteUsedThumbnails($target); }