instances added god may help
This commit is contained in:
@@ -12,38 +12,38 @@ class UserAvatarService
|
||||
private const SIZES = [64, 128, 256];
|
||||
private const DEFAULT_SIZE = 128;
|
||||
|
||||
public static function isValidUuid(string $uuid): bool
|
||||
public function isValidUuid(string $uuid): bool
|
||||
{
|
||||
return self::imageIsValidUuid($uuid);
|
||||
}
|
||||
|
||||
public static function storageBase(): string
|
||||
public function storageBase(): string
|
||||
{
|
||||
return self::imageStorageBase();
|
||||
}
|
||||
|
||||
public static function userDir(string $uuid): string
|
||||
public function userDir(string $uuid): string
|
||||
{
|
||||
return self::storageBase() . '/users/' . $uuid;
|
||||
return $this->storageBase() . '/users/' . $uuid;
|
||||
}
|
||||
|
||||
public static function findAvatarPath(string $uuid, ?int $size = null): ?string
|
||||
public function findAvatarPath(string $uuid, ?int $size = null): ?string
|
||||
{
|
||||
if (!self::isValidUuid($uuid)) {
|
||||
if (!$this->isValidUuid($uuid)) {
|
||||
return null;
|
||||
}
|
||||
$dir = self::userDir($uuid);
|
||||
$dir = $this->userDir($uuid);
|
||||
if (!is_dir($dir)) {
|
||||
return null;
|
||||
}
|
||||
if ($size) {
|
||||
$size = self::normalizeSize($size);
|
||||
$variant = self::findVariantPath($dir, $size);
|
||||
$size = $this->normalizeSize($size);
|
||||
$variant = $this->findVariantPath($dir, $size);
|
||||
if ($variant) {
|
||||
return $variant;
|
||||
}
|
||||
}
|
||||
$defaultVariant = self::findVariantPath($dir, self::DEFAULT_SIZE);
|
||||
$defaultVariant = $this->findVariantPath($dir, self::DEFAULT_SIZE);
|
||||
if ($defaultVariant) {
|
||||
return $defaultVariant;
|
||||
}
|
||||
@@ -51,18 +51,18 @@ class UserAvatarService
|
||||
return $original ?: null;
|
||||
}
|
||||
|
||||
public static function hasAvatar(string $uuid): bool
|
||||
public function hasAvatar(string $uuid): bool
|
||||
{
|
||||
$path = self::findAvatarPath($uuid);
|
||||
$path = $this->findAvatarPath($uuid);
|
||||
return $path ? is_file($path) : false;
|
||||
}
|
||||
|
||||
public static function delete(string $uuid): bool
|
||||
public function delete(string $uuid): bool
|
||||
{
|
||||
if (!self::isValidUuid($uuid)) {
|
||||
if (!$this->isValidUuid($uuid)) {
|
||||
return false;
|
||||
}
|
||||
$dir = self::userDir($uuid);
|
||||
$dir = $this->userDir($uuid);
|
||||
if (!is_dir($dir)) {
|
||||
return true;
|
||||
}
|
||||
@@ -79,9 +79,9 @@ class UserAvatarService
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function saveUpload(string $uuid, array $file): array
|
||||
public function saveUpload(string $uuid, array $file): array
|
||||
{
|
||||
if (!self::isValidUuid($uuid)) {
|
||||
if (!$this->isValidUuid($uuid)) {
|
||||
return ['ok' => false, 'error' => t('User not found')];
|
||||
}
|
||||
if (empty($file) || !isset($file['tmp_name'])) {
|
||||
@@ -95,7 +95,7 @@ class UserAvatarService
|
||||
}
|
||||
|
||||
$tmpPath = $file['tmp_name'];
|
||||
$mime = self::detectMime($tmpPath);
|
||||
$mime = $this->detectMime($tmpPath);
|
||||
$isSvg = self::imageIsSvgUpload($mime, $tmpPath);
|
||||
$ext = self::imageExtensionForMime($mime, $isSvg);
|
||||
if (!$ext) {
|
||||
@@ -105,12 +105,12 @@ class UserAvatarService
|
||||
return ['ok' => false, 'error' => t('Invalid image file')];
|
||||
}
|
||||
|
||||
$dir = self::userDir($uuid);
|
||||
$dir = $this->userDir($uuid);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
|
||||
return ['ok' => false, 'error' => t('Upload failed')];
|
||||
}
|
||||
|
||||
self::delete($uuid);
|
||||
$this->delete($uuid);
|
||||
$originalPath = $dir . '/original.' . $ext;
|
||||
if (!move_uploaded_file($tmpPath, $originalPath)) {
|
||||
return ['ok' => false, 'error' => t('Upload failed')];
|
||||
@@ -128,9 +128,9 @@ class UserAvatarService
|
||||
return ['ok' => true, 'path' => $originalPath, 'mime' => $mime];
|
||||
}
|
||||
|
||||
public static function saveBinary(string $uuid, string $contents, string $mime = ''): array
|
||||
public function saveBinary(string $uuid, string $contents, string $mime = ''): array
|
||||
{
|
||||
if (!self::isValidUuid($uuid)) {
|
||||
if (!$this->isValidUuid($uuid)) {
|
||||
return ['ok' => false, 'error' => t('User not found')];
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ class UserAvatarService
|
||||
return ['ok' => false, 'error' => t('Upload failed')];
|
||||
}
|
||||
|
||||
$detectedMime = self::detectMime($tmpPath);
|
||||
$detectedMime = $this->detectMime($tmpPath);
|
||||
if ($detectedMime === 'application/octet-stream') {
|
||||
$headerMime = strtolower(trim(explode(';', $mime, 2)[0]));
|
||||
if (in_array($headerMime, ['image/jpeg', 'image/png', 'image/webp'], true)) {
|
||||
@@ -164,13 +164,13 @@ class UserAvatarService
|
||||
return ['ok' => false, 'error' => t('Invalid image file')];
|
||||
}
|
||||
|
||||
$dir = self::userDir($uuid);
|
||||
$dir = $this->userDir($uuid);
|
||||
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
|
||||
@unlink($tmpPath);
|
||||
return ['ok' => false, 'error' => t('Upload failed')];
|
||||
}
|
||||
|
||||
self::delete($uuid);
|
||||
$this->delete($uuid);
|
||||
$originalPath = $dir . '/original.' . $ext;
|
||||
if (!@rename($tmpPath, $originalPath)) {
|
||||
if (!@copy($tmpPath, $originalPath)) {
|
||||
@@ -192,12 +192,12 @@ class UserAvatarService
|
||||
return ['ok' => true, 'path' => $originalPath, 'mime' => $detectedMime];
|
||||
}
|
||||
|
||||
public static function detectMime(string $path): string
|
||||
public function detectMime(string $path): string
|
||||
{
|
||||
return self::imageDetectMime($path);
|
||||
}
|
||||
|
||||
private static function normalizeSize(int $size): int
|
||||
private function normalizeSize(int $size): int
|
||||
{
|
||||
if (in_array($size, self::SIZES, true)) {
|
||||
return $size;
|
||||
@@ -205,7 +205,7 @@ class UserAvatarService
|
||||
return self::DEFAULT_SIZE;
|
||||
}
|
||||
|
||||
private static function findVariantPath(string $dir, int $size): ?string
|
||||
private function findVariantPath(string $dir, int $size): ?string
|
||||
{
|
||||
$matches = glob($dir . '/avatar-' . $size . '.*');
|
||||
if (!$matches) {
|
||||
|
||||
Reference in New Issue
Block a user