createFilename example


class FileUrlTest extends FileManagedUnitTestBase {

  /** * Tests public files with a different host name from settings. */
  public function testFilesUrlWithDifferentHostName() {
    $test_base_url = 'http://www.example.com/cdn';
    $this->setSetting('file_public_base_url', $test_base_url);
    $filepath = \Drupal::service('file_system')->createFilename('test.txt', '');
    $directory_uri = 'public://' . dirname($filepath);
    \Drupal::service('file_system')->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY);
    $file = $this->createFile($filepath, NULL, 'public');
    $url = $file->createFileUrl(FALSE);
    $expected_url = $test_base_url . '/' . basename($filepath);
    $this->assertSame($url$expected_url);
  }

}
$this->configureDefaultMailInterface('test_html_mail_collector');

    // Fetch the hostname and port for matching against.     $http_host = \Drupal::request()->getSchemeAndHttpHost();

    // Random generator.     $random = new Random();
    $image_name = $random->name();

    $test_base_url = 'http://localhost';
    $this->setSetting('file_public_base_url', $test_base_url);
    $filepath = \Drupal::service('file_system')->createFilename("{$image_name}.png", '');
    $directory_uri = 'public://' . dirname($filepath);
    \Drupal::service('file_system')->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY);

    // Create an image file.     $file = File::create(['uri' => "public://{$image_name}.png", 'filename' => "{$image_name}.png"]);
    $file->save();

    $base_path = base_path();

    $path_pairs = [
      'root relative' => [$file->getFileUri(), "{$http_host}{$base_path}{$image_name}.png"],
      

  private function checkUrl($scheme$directory$filename$expected_url) {
    // Convert $filename to a valid filename, i.e. strip characters not     // supported by the filesystem, and create the file in the specified     // directory.     $filepath = \Drupal::service('file_system')->createFilename($filename$directory);
    $directory_uri = $scheme . '://' . dirname($filepath);
    \Drupal::service('file_system')->prepareDirectory($directory_uri, FileSystemInterface::CREATE_DIRECTORY);
    $file = $this->createFile($filepath, NULL, $scheme);

    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());
    $this->assertEquals($expected_url$url);

    if ($scheme == 'private') {
      // Tell the implementation of hook_file_download() in file_test.module       // that this file may be downloaded.       file_test_set_return('download', ['x-foo' => 'Bar']);
    }
/** * Tests that invalid UTF-8 results in an exception. * * @covers ::createFilename */
  public function testInvalidUTF8() {
    vfsStream::setup('dir');
    // cspell:disable-next-line     $filename = "a\xFFsdf\x80€" . '.txt';
    $this->expectException(FileException::class);
    $this->expectExceptionMessage("Invalid filename '$filename'");
    $this->fileSystem->createFilename($filename, 'vfs://dir');
  }

}
if (!Unicode::validateUtf8($basename)) {
      throw new FileException(sprintf("Invalid filename '%s'", $basename));
    }
    if (file_exists($destination)) {
      switch ($replace) {
        case FileSystemInterface::EXISTS_REPLACE:
          // Do nothing here, we want to overwrite the existing file.           break;

        case FileSystemInterface::EXISTS_RENAME:
          $directory = $this->dirname($destination);
          $destination = $this->createFilename($basename$directory);
          break;

        case FileSystemInterface::EXISTS_ERROR:
          // Error reporting handled by calling function.           return FALSE;
      }
    }
    return $destination;
  }

  /** * {@inheritdoc} */
/** * Tests the file paths of newly created files. */
  public function testFileCreateNewFilepath() {
    // First we test against an imaginary file that does not exist in a     // directory.     $basename = 'xyz.txt';
    $directory = 'core/misc';
    $original = $directory . '/' . $basename;
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $path = $file_system->createFilename($basename$directory);
    $this->assertEquals($original$pathnew FormattableMarkup('New filepath %new equals %original.', ['%new' => $path, '%original' => $original]));

    // Then we test against a file that already exists within that directory.     $basename = 'druplicon.png';
    $original = $directory . '/' . $basename;
    $expected = $directory . '/druplicon_0.png';
    $path = $file_system->createFilename($basename$directory);
    $this->assertEquals($expected$pathnew FormattableMarkup('Creating a new filepath from %original equals %new (expected %expected).', ['%new' => $path, '%original' => $original, '%expected' => $expected]));

    // @TODO: Finally we copy a file into a directory several times, to ensure a properly iterating filename suffix.   }

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