AvatarKitDownloadUtility.php in Avatar Kit 8.2
File
src/AvatarKitDownloadUtility.php
View source
<?php
namespace Drupal\avatars;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
use Drupal\file\FileUsage\FileUsageInterface;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
class AvatarKitDownloadUtility implements AvatarKitDownloadUtilityInterface {
protected $fileStorage;
protected $fileSystem;
protected $fileUsage;
protected $httpClient;
protected $logger;
public function __construct(EntityTypeManagerInterface $entityTypeManager, FileSystemInterface $fileSystem, FileUsageInterface $fileUsage, Client $httpClient, LoggerInterface $logger) {
$this->fileStorage = $entityTypeManager
->getStorage('file');
$this->fileSystem = $fileSystem;
$this->fileUsage = $fileUsage;
$this->httpClient = $httpClient;
$this->logger = $logger;
}
public function get(string $uri) : ResponseInterface {
$valid_url = !empty($uri) && parse_url($uri) !== FALSE;
if (!$valid_url) {
throw new \InvalidArgumentException('Malformed Url');
}
$response = $this->httpClient
->get($uri);
return $response;
}
public function createFile(ResponseInterface $response, string $default_filename) : ?FileInterface {
$stream = $response
->getBody();
$temp_filepath = $this->fileSystem
->tempnam('temporary://', 'temp');
if (\file_put_contents($temp_filepath, $stream) === FALSE) {
$this->logger
->notice('Unable to create temporary file: %filename.', [
'%filename' => $temp_filepath,
]);
return NULL;
}
[
'dirname' => $directory,
'filename' => $filename,
] = pathinfo($default_filename);
$final_filename = $directory . DIRECTORY_SEPARATOR . $filename . '.' . $this
->getExtension($response);
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY) !== TRUE) {
$this->logger
->notice('Unable to create directory: %directory.', [
'%directory' => $directory,
]);
return NULL;
}
try {
$final_filepath = $this
->moveFile($temp_filepath, $final_filename, FILE_EXISTS_REPLACE);
} catch (\Exception $e) {
return NULL;
}
$file = $this->fileStorage
->create();
$file
->setFileUri($final_filepath);
$file
->setPermanent();
try {
$file
->save();
} catch (\Exception $e) {
$this->logger
->notice('Error saving file: %message.', [
'%message' => $e
->getMessage(),
]);
}
return $file;
}
protected function moveFile(...$args) : string {
$result = \file_unmanaged_move(...$args);
if ($result === FALSE) {
throw new \Exception('Failed to move file.');
}
return $result;
}
protected function getExtension(ResponseInterface $response) : string {
$guesser = ExtensionGuesser::getInstance();
$mime = $response
->getHeaderLine('Content-Type');
return $guesser
->guess($mime) ?? 'file';
}
protected function logViolations(FileInterface $entity) : int {
$violations = $entity
->validate();
if ($violations) {
foreach ($violations as $violation) {
$message = $violation
->getMessage();
$this->logger
->notice('Unable to save file %file: %message.', [
'%file' => $entity
->getFileUri(),
'%message' => $message,
]);
}
}
return $violations
->count();
}
}