Moved base64 decoding to FormData

This commit is contained in:
Marcin Kurczewski 2014-09-15 17:09:42 +02:00
parent f6df009085
commit 9bd114e5b6
4 changed files with 28 additions and 24 deletions

View file

@ -22,7 +22,7 @@ class UserEditFormData implements \Szurubooru\IValidatable
$this->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankFromString($inputReader->accessRank); $this->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankFromString($inputReader->accessRank);
if ($inputReader->avatarStyle !== null) if ($inputReader->avatarStyle !== null)
$this->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleFromString($inputReader->avatarStyle); $this->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleFromString($inputReader->avatarStyle);
$this->avatarContent = $inputReader->avatarContent; $this->avatarContent = $inputReader->decodeBase64($inputReader->avatarContent);
$this->browsingSettings = $inputReader->browsingSettings; $this->browsingSettings = $inputReader->browsingSettings;
} }
} }

View file

@ -24,4 +24,12 @@ final class InputReader extends \ArrayObject
return null; return null;
return parent::offsetGet($index); return parent::offsetGet($index);
} }
public function decodeBase64($base64string)
{
$commaPosition = strpos($base64string, ',');
if ($commaPosition !== null)
$base64string = substr($base64string, $commaPosition + 1);
return base64_decode($base64string);
}
} }

View file

@ -12,17 +12,17 @@ class FileService
$this->httpHelper = $httpHelper; $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) $daysToLive = isset($options->daysToLive)
? $options->daysToLive ? $options->daysToLive
: 7; : 7;
$secondsToLive = $daysToLive * 24 * 60 * 60; $secondsToLive = $daysToLive * 24 * 60 * 60;
$lastModified = filemtime($finalSource); $lastModified = filemtime($fullPath);
$eTag = md5(file_get_contents($finalSource)); //todo: faster $eTag = md5(file_get_contents($fullPath)); //todo: faster
$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) $ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
? $_SERVER['HTTP_IF_MODIFIED_SINCE'] ? $_SERVER['HTTP_IF_MODIFIED_SINCE']
@ -45,7 +45,7 @@ class FileService
'Content-Type', 'Content-Type',
isset($options->mimeType) isset($options->mimeType)
? $options->mimeType ? $options->mimeType
: mime_content_type($finalSource)); : mime_content_type($fullPath));
if (strtotime($ifModifiedSince) === $lastModified or $eTagHeader === $eTag) if (strtotime($ifModifiedSince) === $lastModified or $eTagHeader === $eTag)
{ {
@ -54,38 +54,34 @@ class FileService
else else
{ {
$this->httpHelper->setResponseCode(200); $this->httpHelper->setResponseCode(200);
readfile($finalSource); readfile($fullPath);
} }
exit; exit;
} }
public function createFolders($target) public function createFolders($target)
{ {
$finalTarget = $this->getFullPath($target); $fullPath = $this->getFullPath($target);
if (!file_exists($finalTarget)) if (!file_exists($fullPath))
mkdir($finalTarget, 0777, true); mkdir($fullPath, 0777, true);
} }
public function exists($source) public function exists($target)
{ {
$finalSource = $this->getFullPath($source); $fullPath = $this->getFullPath($target);
return $source and file_exists($finalSource); return $target and file_exists($fullPath);
} }
public function delete($source) public function delete($target)
{ {
$finalSource = $this->getFullPath($source); $fullPath = $this->getFullPath($target);
if (file_exists($finalSource)) if (file_exists($fullPath))
unlink($finalSource); unlink($fullPath);
} }
public function saveFromBase64($base64string, $destination) public function save($destination, $data)
{ {
$finalDestination = $this->getFullPath($destination); $finalDestination = $this->getFullPath($destination);
$commaPosition = strpos($base64string, ',');
if ($commaPosition !== null)
$base64string = substr($base64string, $commaPosition + 1);
$data = base64_decode($base64string);
file_put_contents($finalDestination, $data); file_put_contents($finalDestination, $data);
} }

View file

@ -216,10 +216,10 @@ class UserService
$user->setAvatarStyle($newAvatarStyle); $user->setAvatarStyle($newAvatarStyle);
} }
private function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContentInBase64) private function updateUserAvatarContent(\Szurubooru\Entities\User $user, $newAvatarContent)
{ {
$target = $this->getCustomAvatarSourcePath($user); $target = $this->getCustomAvatarSourcePath($user);
$this->fileService->saveFromBase64($newAvatarContentInBase64, $target); $this->fileService->save($target, $newAvatarContent);
$this->thumbnailService->deleteUsedThumbnails($target); $this->thumbnailService->deleteUsedThumbnails($target);
} }