loadByUri example

$target = $this->createFile();
    $fileRepository->writeData('asdf', $target->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
  }

  /** * Tests loading a file by URI. * * @covers ::loadByUri */
  public function testLoadByUri() {
    $source = $this->createFile();
    $result = $this->fileRepository->loadByUri($source->getFileUri());
    $this->assertSameFile($source$result);
  }

  /** * Tests loading a file by case-sensitive URI. * * @covers ::loadByUri */
  public function testLoadByUriCaseSensitive() {
    $source = $this->createFile('FooBar.txt');
    $result = $this->fileRepository->loadByUri('public://FooBar.txt');
    

  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();

    
$file->setFileUri($destinationFilename);

    if (!$this->moveUploadedFile($uploadedFile$file->getFileUri())) {
      throw new FileWriteException('File upload error. Could not move uploaded file.');
    }

    // Update the filename with any changes as a result of security or renaming     // due to an existing file.     $file->setFilename($this->fileSystem->basename($file->getFileUri()));

    if ($replace === FileSystemInterface::EXISTS_REPLACE) {
      $existingFile = $this->loadByUri($file->getFileUri());
      if ($existingFile) {
        $file->fid = $existingFile->id();
        $file->setOriginalId($existingFile->id());
      }
    }

    $result = (new FileUploadResult())
      ->setOriginalFilename($originalName)
      ->setSanitizedFilename($filename)
      ->setFile($file);

    
Home | Imprint | This part of the site doesn't use cookies.