saveData example

$hash = Crypt::hashBase64($remote_thumbnail_url);
    $files = $this->fileSystem->scanDirectory($directory, "/^$hash\..*/");
    if (count($files) > 0) {
      return reset($files)->uri;
    }

    // The local thumbnail doesn't exist yet, so we need to download it.     try {
      $response = $this->httpClient->request('GET', $remote_thumbnail_url);
      if ($response->getStatusCode() === 200) {
        $local_thumbnail_uri = $directory . DIRECTORY_SEPARATOR . $hash . '.' . $this->getThumbnailFileExtensionFromUrl($remote_thumbnail_url$response);
        $this->fileSystem->saveData((string) $response->getBody()$local_thumbnail_uri, FileSystemInterface::EXISTS_REPLACE);
        return $local_thumbnail_uri;
      }
    }
    catch (TransferException $e) {
      $this->logger->warning('Failed to download remote thumbnail file due to "%error".', [
        '%error' => $e->getMessage(),
      ]);
    }
    catch (FileException $e) {
      $this->logger->warning('Could not download remote thumbnail from {url}.', [
        'url' => $remote_thumbnail_url,
      ]);
return $this->dumpToUri($data$file_extension$uri);
  }

  /** * {@inheritdoc} */
  public function dumpToUri(string $data, string $file_extension, string $uri): string {
    $path = 'assets://' . $file_extension;
    // Create the CSS or JS file.     $this->fileSystem->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
    try {
      if (!file_exists($uri) && !$this->fileSystem->saveData($data$uri, FileSystemInterface::EXISTS_REPLACE)) {
        return FALSE;
      }
    }
    catch (FileException $e) {
      return FALSE;
    }
    // If CSS/JS gzip compression is enabled and the zlib extension is available     // then create a gzipped version of this file. This file is served     // conditionally to browsers that accept gzip using .htaccess rules.     // It's possible that the rewrite rules in .htaccess aren't working on this     // server, but there's no harm (other than the time spent generating the
$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. */
protected static $modules = [
    'system',
    'update',
  ];

  /** * Tests the deletion of stale files. */
  public function testUpdateDeleteFileIfStale() {
    $file_system = $this->container->get('file_system');

    $file_name = $file_system->saveData($this->randomMachineName(), 'public://');
    $this->assertNotNull($file_name);
    $file_path = $file_system->realpath($file_name);

    // During testing, the file change and the stale checking occurs in the same     // request, so the beginning of request will be before the file changes and     // REQUEST_TIME - $filectime is negative or zero. Set the maximum age to a     // number greater than that.     $this->config('system.file')
      ->set('temporary_maximum_age', 100000)
      ->save();

    

  public function testFileSaveData() {
    $contents = $this->randomMachineName(8);
    $this->setSetting('file_chmod_file', 0777);

    // No filename.     /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');

    // Provide a filename.     $filepath = $file_system->saveData($contents, 'public://asdf.txt', FileSystemInterface::EXISTS_REPLACE);
    $this->assertNotFalse($filepath, 'Unnamed file saved correctly.');
    $this->assertEquals('asdf.txt', \Drupal::service('file_system')->basename($filepath), 'File was named correctly.');
    $this->assertEquals($contentsfile_get_contents($filepath), 'Contents of the file are correct.');
    $this->assertFilePermissions($filepath, 0777);
  }

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