View source
<?php
namespace Drupal\file;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileUsage\FileUsageInterface;
class FileRepository implements FileRepositoryInterface {
protected $fileSystem;
protected $streamWrapperManager;
protected $entityTypeManager;
protected $moduleHandler;
protected $fileUsage;
protected $currentUser;
public function __construct(FileSystemInterface $fileSystem, StreamWrapperManagerInterface $streamWrapperManager, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, FileUsageInterface $fileUsage, AccountInterface $currentUser) {
$this->fileSystem = $fileSystem;
$this->streamWrapperManager = $streamWrapperManager;
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->fileUsage = $fileUsage;
$this->currentUser = $currentUser;
}
public function writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface {
if (!$this->streamWrapperManager
->isValidUri($destination)) {
throw new InvalidStreamWrapperException(sprintf('Invalid stream wrapper: %destination', [
'%destination' => $destination,
]));
}
$uri = $this->fileSystem
->saveData($data, $destination, $replace);
return $this
->createOrUpdate($uri, $destination, $replace === FileSystemInterface::EXISTS_RENAME);
}
protected function createOrUpdate(string $uri, string $destination, bool $rename) : FileInterface {
$file = $this
->loadByUri($uri);
if ($file === NULL) {
$file = File::create([
'uri' => $uri,
]);
$file
->setOwnerId($this->currentUser
->id());
}
if ($rename && is_file($destination)) {
$file
->setFilename($this->fileSystem
->basename($destination));
}
$file
->setPermanent();
$file
->save();
return $file;
}
public function copy(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface {
if (!$this->streamWrapperManager
->isValidUri($destination)) {
throw new InvalidStreamWrapperException(sprintf('Invalid stream wrapper: %destination', [
'%destination' => $destination,
]));
}
$uri = $this->fileSystem
->copy($source
->getFileUri(), $destination, $replace);
if ($replace === FileSystemInterface::EXISTS_REPLACE && ($existing = $this
->loadByUri($uri))) {
$file = $existing;
}
else {
$file = $source
->createDuplicate();
$file
->setFileUri($uri);
if ($replace === FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
$file
->setFilename($this->fileSystem
->basename($destination));
}
else {
$file
->setFilename($this->fileSystem
->basename($uri));
}
}
$file
->save();
$this->moduleHandler
->invokeAll('file_copy', [
$file,
$source,
]);
return $file;
}
public function move(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME) : FileInterface {
if (!$this->streamWrapperManager
->isValidUri($destination)) {
throw new InvalidStreamWrapperException(sprintf('Invalid stream wrapper: %destination', [
'%destination' => $destination,
]));
}
$uri = $this->fileSystem
->move($source
->getFileUri(), $destination, $replace);
$delete_source = FALSE;
$file = clone $source;
$file
->setFileUri($uri);
if ($replace === FileSystemInterface::EXISTS_REPLACE) {
if ($existing = $this
->loadByUri($uri)) {
$delete_source = TRUE;
$file->fid = $existing
->id();
$file->uuid = $existing
->uuid();
}
}
elseif ($replace === FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
$file
->setFilename($this->fileSystem
->basename($destination));
}
$file
->save();
$this->moduleHandler
->invokeAll('file_move', [
$file,
$source,
]);
if ($delete_source && !$this->fileUsage
->listUsage($source)) {
$source
->delete();
}
return $file;
}
public function loadByUri(string $uri) : ?FileInterface {
$fileStorage = $this->entityTypeManager
->getStorage('file');
$files = $fileStorage
->loadByProperties([
'uri' => $uri,
]);
if (count($files)) {
foreach ($files as $item) {
if ($item
->getFileUri() === $uri) {
return $item;
}
}
}
return NULL;
}
}