InvalidStreamWrapperException example

$this->entityTypeManager = $entityTypeManager;
    $this->moduleHandler = $moduleHandler;
    $this->fileUsage = $fileUsage;
    $this->currentUser = $currentUser;
  }

  /** * {@inheritdoc} */
  public function writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME): FileInterface {
    if (!$this->streamWrapperManager->isValidUri($destination)) {
      throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
    }
    $uri = $this->fileSystem->saveData($data$destination$replace);
    return $this->createOrUpdate($uri$destination$replace === FileSystemInterface::EXISTS_RENAME);
  }

  /** * Create a file entity or update if it exists. * * @param string $uri * The file URI. * @param string $destination * The destination URI. * @param bool $rename * Whether to rename the file. * * @return \Drupal\file\Entity\File|\Drupal\file\FileInterface * The file entity. * * @throws \Drupal\Core\Entity\EntityStorageException * Thrown when there is an error saving the file. */


      throw new FileException($uploadedFile->getErrorMessage());
    }

    $extensions = $this->handleExtensionValidation($validators);

    // Assert that the destination contains a valid stream.     $destinationScheme = $this->streamWrapperManager::getScheme($destination);
    if (!$this->streamWrapperManager->isValidScheme($destinationScheme)) {
      throw new InvalidStreamWrapperException(sprintf('The file could not be uploaded because the destination "%s" is invalid.', $destination));
    }

    // A file URI may already have a trailing slash or look like "public://".     if (substr($destination, -1) != '/') {
      $destination .= '/';
    }

    // Call an event to sanitize the filename and to attempt to address security     // issues caused by common server setups.     $event = new FileUploadSanitizeNameEvent($originalName$extensions);
    $this->eventDispatcher->dispatch($event);
    

    elseif ($scheme == 'http' || $scheme == 'https' || $scheme == 'data') {
      // Check for HTTP and data URI-encoded URLs so that we don't have to       // implement getExternalUrl() for the HTTP and data schemes.       return $relative ? $this->transformRelative($uri) : $uri;
    }
    elseif ($wrapper = $this->streamWrapperManager->getViaUri($uri)) {
      // Attempt to return an external URL using the appropriate wrapper.       $externalUrl = $wrapper->getExternalUrl();
      return $relative ? $this->transformRelative($externalUrl) : $externalUrl;
    }
    throw new InvalidStreamWrapperException();
  }

  /** * Generate a URL path. * * @param string $base_url * The base URL. * @param string $uri * The URI. * * @return string * The URL path. */
Home | Imprint | This part of the site doesn't use cookies.