basePath example

// Generate ASCII text test files.       $lines = [16, 256, 1024, 2048, 20480];
      $count = 0;
      foreach ($lines as $line) {
        $this->generateFile('text-' . $count++, 64, $line, 'text');
      }

      // Copy other test files from fixtures.       $original = \Drupal::root() . '/core/tests/fixtures/files';
      $files = $file_system->scanDirectory($original, '/(html|image|javascript|php|sql)-.*/');
      foreach ($files as $file) {
        $file_system->copy($file->uri, PublicStream::basePath());
      }

      $this->generatedTestFiles = TRUE;
    }

    $files = [];
    // Make sure type is valid.     if (in_array($type['binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'])) {
      $files = $file_system->scanDirectory('public://', '/' . $type . '\-.*/');

      // If size is set then remove any files that are not of that size.
/** * {@inheritdoc} */
  public function getDescription() {
    return t('Private local files served by Drupal.');
  }

  /** * {@inheritdoc} */
  public function getDirectoryPath() {
    return static::basePath();
  }

  /** * {@inheritdoc} */
  public function getExternalUrl() {
    $path = str_replace('\\', '/', $this->getTarget());
    return Url::fromRoute('system.private_file_download', ['filepath' => $path]['absolute' => TRUE, 'path_processing' => FALSE])->toString();
  }

  /** * Returns the base path for private://. * * Note that this static method is used by \Drupal\system\Form\FileSystemForm * so you should alter that form or substitute a different form if you change * the class providing the stream_wrapper.private service. * * @return string|null * The base path for private://. NULL means the private directory is not * set. */
        // directory; stream wrappers are not end-user friendly.         $original_path = $element['#default_value'];
        $friendly_path = NULL;

        if (StreamWrapperManager::getScheme($original_path) == 'public') {
          $friendly_path = StreamWrapperManager::getTarget($original_path);
          $element['#default_value'] = $friendly_path;
        }

        // Prepare local file path for description.         if ($original_path && isset($friendly_path)) {
          $local_file = strtr($original_path['public:/' => PublicStream::basePath()]);
        }
        elseif ($theme) {
          $local_file = $this->themeHandler->getTheme($theme)->getPath() . '/' . $default;
        }
        else {
          $local_file = $this->themeManager->getActiveTheme()->getPath() . '/' . $default;
        }

        $element['#description'] = $this->t('Examples: <code>@implicit-public-file</code> (for a file in the public filesystem), <code>@explicit-file</code>, or <code>@local-file</code>.', [
          '@implicit-public-file' => $friendly_path ?? $default,
          '@explicit-file' => StreamWrapperManager::getScheme($original_path) !== FALSE ? $original_path : 'public://' . $default,
          

  protected $fileStorage;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create a directory.     $dir = PublicStream::basePath() . '/config';
    $this->fileStorage = new FileStorage($dir);
    $this->storage = new CachedStorage($this->fileStorage, \Drupal::service('cache.config'));
    $this->cache = \Drupal::service('cache_factory')->get('config');
  }

  /** * {@inheritdoc} */
  public function testInvalidStorage() {
    $this->markTestSkipped('No-op as this test does not make sense');
  }

  

  public function getDescription(): string {
    return $this->t('Public local optimized assets files served by the webserver.');
  }

  /** * {@inheritdoc} */
  public static function basePath($site_path = NULL): string {
    return Settings::get(
      'file_assets_path',
      parent::basePath($site_path)
    );
  }

  /** * {@inheritdoc} */
  public static function baseUrl(): string {
    $public_path = Settings::get('file_public_path', 'sites/default/files');
    $path = Settings::get('file_assets_path', $public_path);
    if ($path === $public_path) {
      $base_url = PublicStream::baseUrl();
    }
// Test that the name is used for the bin when it is NULL.     $this->setSettings('test', ['bin' => NULL]);
    $php = PhpStorageFactory::get('test');
    $this->assertInstanceOf(MockPhpStorage::class$php);
    $this->assertSame('test', $php->getConfigurationValue('bin'), 'Name value was used for bin.');

    // Test that a default directory is set if it's empty.     $this->setSettings('test', ['directory' => NULL]);
    $php = PhpStorageFactory::get('test');
    $this->assertInstanceOf(MockPhpStorage::class$php);
    $this->assertSame(PublicStream::basePath() . '/php', $php->getConfigurationValue('directory'), 'Default file directory was used.');

    // Test that a default storage class is set if it's empty.     $this->setSettings('test', ['class' => NULL]);
    $php = PhpStorageFactory::get('test');
    $this->assertInstanceOf(MTimeProtectedFileStorage::class$php);

    // Test that a default secret is not returned if it's set in the override.     $this->setSettings('test');
    $php = PhpStorageFactory::get('test');
    $this->assertNotEquals('mock hash salt', $php->getConfigurationValue('secret'), 'The default secret is not used if a secret is set in the overridden settings.');

    
return ['system.file'];
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('system.file');
    $form['file_public_path'] = [
      '#type' => 'item',
      '#title' => $this->t('Public file system path'),
      '#markup' => PublicStream::basePath(),
      '#description' => $this->t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web. This must be changed in settings.php'),
    ];

    $form['file_public_base_url'] = [
      '#type' => 'item',
      '#title' => $this->t('Public file base URL'),
      '#markup' => PublicStream::baseUrl(),
      '#description' => $this->t('The base URL that will be used for public file URLs. This can be changed in settings.php'),
    ];

    $form['file_assets_path'] = [
      
$this->assertSession()->statusCodeEquals(404);
    // Ensure a non existent theme settings form URL returns 404.     $this->drupalGet('admin/appearance/settings/' . $this->randomMachineName());
    $this->assertSession()->statusCodeEquals(404);
    // Ensure a hidden theme settings form URL returns 404.     $this->assertTrue(\Drupal::service('theme_installer')->install(['stable9']));
    $this->drupalGet('admin/appearance/settings/stable9');
    $this->assertSession()->statusCodeEquals(404);

    // Specify a filesystem path to be used for the logo.     $file = current($this->drupalGetTestFiles('image'));
    $file_relative = strtr($file->uri, ['public:/' => PublicStream::basePath()]);
    $default_theme_path = 'core/themes/starterkit_theme';

    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $supported_paths = [
      // Raw stream wrapper URI.       $file->uri => [
        'form' => StreamWrapperManager::getTarget($file->uri),
        'src' => $file_url_generator->generateString($file->uri),
      ],
      // Relative path within the public filesystem.
$instance = $stream_wrapper_manager->getViaUri('public://foo');
    $this->assertEquals('Drupal\Core\StreamWrapper\PublicStream', get_class($instance), 'Got correct class type for public URI.');

    // Test file_uri_target().     $this->assertEquals('foo/bar.txt', $stream_wrapper_manager::getTarget('public://foo/bar.txt'), 'Got a valid stream target from public://foo/bar.txt.');
    $this->assertEquals('image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', $stream_wrapper_manager::getTarget('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='));
    $this->assertFalse($stream_wrapper_manager::getTarget('foo/bar.txt'), 'foo/bar.txt is not a valid stream.');
    $this->assertSame($stream_wrapper_manager::getTarget('public://'), '');
    $this->assertSame($stream_wrapper_manager::getTarget('data:'), '');

    // Test Drupal\Core\StreamWrapper\LocalStream::getDirectoryPath().     $this->assertEquals(PublicStream::basePath()$stream_wrapper_manager->getViaScheme('public')->getDirectoryPath(), 'Expected default directory path was returned.');
    $file_system = \Drupal::service('file_system');
    assert($file_system instanceof FileSystemInterface);
    $this->assertEquals($file_system->getTempDirectory()$stream_wrapper_manager->getViaScheme('temporary')->getDirectoryPath(), 'Expected temporary directory path was returned.');

    // Test FileUrlGeneratorInterface::generateString()     // TemporaryStream::getExternalUrl() uses Url::fromRoute(), which needs     // route information to work.     $file_url_generator = $this->container->get('file_url_generator');
    assert($file_url_generator instanceof FileUrlGeneratorInterface);
    $this->assertStringContainsString('system/temporary?file=test.txt', $file_url_generator->generateString('temporary://test.txt'), 'Temporary external URL correctly built.');
    $this->assertStringContainsString(Settings::get('file_public_path') . '/test.txt', $file_url_generator->generateString('public://test.txt'), 'Public external URL correctly built.');
    

  protected $directory;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create a directory.     $this->directory = PublicStream::basePath() . '/config';
    $this->storage = new FileStorage($this->directory);
    $this->invalidStorage = new FileStorage($this->directory . '/nonexisting');

    // FileStorage::listAll() requires other configuration data to exist.     $this->storage->write('system.performance', $this->config('system.performance')->get());
    $this->storage->write('core.extension', ['module' => []]);
  }

  protected function read($name) {
    $data = file_get_contents($this->storage->getFilePath($name));
    return Yaml::decode($data);
  }
/** * {@inheritdoc} */
  public function getDescription() {
    return t('Public local files served by the webserver.');
  }

  /** * {@inheritdoc} */
  public function getDirectoryPath() {
    return static::basePath();
  }

  /** * {@inheritdoc} */
  public function getExternalUrl() {
    $path = str_replace('\\', '/', $this->getTarget());
    return static::baseUrl() . '/' . UrlHelper::encodePath($path);
  }

  /** * Finds and returns the base URL for public://. * * Defaults to the current site's base URL plus directory path. * * Note that this static method is used by \Drupal\system\Form\FileSystemForm * so you should alter that form or substitute a different form if you change * the class providing the stream_wrapper.public service. * * @return string * The external base URL for public:// */


    $this->logger->error("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <pre><code>@htaccess</code></pre>", ['%directory' => $directory, '@htaccess' => FileSecurity::htaccessLines($deny_public_access)]);
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function defaultProtectedDirs() {
    $protected_dirs[] = new ProtectedDirectory('Public files directory', 'public://');
    if (PrivateStream::basePath()) {
      $protected_dirs[] = new ProtectedDirectory('Private files directory', 'private://', TRUE);
    }
    $protected_dirs[] = new ProtectedDirectory('Temporary files directory', 'temporary://');

    // The assets path may be the same as the public file path, if so don't try     // to write the same .htaccess twice.     $public_path = Settings::get('file_public_path', 'sites/default/files');
    $assets_path = Settings::get('file_assets_path', $public_path);
    if ($assets_path !== $public_path) {
      $protected_dirs[] = new ProtectedDirectory('Optimized assets directory', $assets_path);
    }
    
    $gotit = FALSE;
    try {
      $this->testConnection->copyDirectory($sourcesys_get_temp_dir());
    }
    catch (FileTransferException $e) {
      $gotit = TRUE;
    }
    $this->assertTrue($gotit, 'Was not able to copy a directory outside of the jailed area.');

    $gotit = TRUE;
    try {
      $this->testConnection->copyDirectory($source$this->root . '/' . PublicStream::basePath());
    }
    catch (FileTransferException $e) {
      $gotit = FALSE;
    }
    $this->assertTrue($gotit, 'Was able to copy a directory inside of the jailed area');
  }

}
    $this->addDefaultCommentField('node', 'page');
  }

  /** * Tests removal of images having a non-local source. */
  public function testImageSource() {
    global $base_url;

    $node = $this->drupalCreateNode();

    $public_files_path = PublicStream::basePath();

    $http_base_url = preg_replace('/^https?/', 'http', $base_url);
    $https_base_url = preg_replace('/^https?/', 'https', $base_url);
    $files_path = base_path() . $public_files_path;
    $csrf_path = $public_files_path . '/' . implode('/', array_fill(0, substr_count($public_files_path, '/') + 1, '..'));

    $druplicon = 'core/misc/druplicon.png';
    $red_x_image = base_path() . 'core/misc/icons/e32700/error.svg';

    // Put a test image in the files directory.     $test_images = $this->getTestFiles('image');
    
// Use settings.     $temporary_directory = $this->settings->get('file_temp_path');
    if (!empty($temporary_directory)) {
      return $temporary_directory;
    }

    // Fallback to OS default.     $temporary_directory = FileSystemComponent::getOsTemporaryDirectory();

    if (empty($temporary_directory)) {
      // If no directory has been found default to 'files/tmp'.       $temporary_directory = PublicStream::basePath() . '/tmp';

      // Windows accepts paths with either slash (/) or backslash (\), but       // will not accept a path which contains both a slash and a backslash.       // Since the 'file_public_path' variable may have either format, we       // sanitize everything to use slash which is supported on all platforms.       $temporary_directory = str_replace('\\', '/', $temporary_directory);
    }
    return $temporary_directory;
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.