FileHelper.php in SVG Upload Sanitizer 8
File
src/Helper/FileHelper.php
View source
<?php
namespace Drupal\svg_upload_sanitizer\Helper;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
use Psr\Log\LoggerAwareTrait;
class FileHelper {
use LoggerAwareTrait;
private $fileSystem;
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
public function updateSize(FileInterface $file) {
$file_path = $this->fileSystem
->realpath($file
->getFileUri());
if (FALSE === $file_path) {
$this->logger
->error(sprintf('Could not resolve the path of the file (URI: "%s").', $file
->getFileUri()));
return FALSE;
}
$size = @filesize($file_path);
if (FALSE === $size) {
$this->logger
->error(sprintf('Could not get the file size (path: "%s").', $file_path));
return FALSE;
}
$file
->setSize($size);
try {
$file
->save();
} catch (EntityStorageException $e) {
$this->logger
->error(sprintf('Could not save the file (fid: "%s", path: "%s").', $file
->id(), $file_path));
}
return TRUE;
}
}