PrivateStream.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\Core\StreamWrapper;
  3. use Drupal\Core\Site\Settings;
  4. use Drupal\Core\Url;
  5. /**
  6. * Drupal private (private://) stream wrapper class.
  7. *
  8. * Provides support for storing privately accessible files with the Drupal file
  9. * interface.
  10. */
  11. class PrivateStream extends LocalStream {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getType() {
  16. return StreamWrapperInterface::LOCAL_NORMAL;
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getName() {
  22. return t('Private files');
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getDescription() {
  28. return t('Private local files served by Drupal.');
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getDirectoryPath() {
  34. return static::basePath();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getExternalUrl() {
  40. $path = str_replace('\\', '/', $this->getTarget());
  41. return Url::fromRoute('system.private_file_download', ['filepath' => $path], ['absolute' => TRUE, 'path_processing' => FALSE])->toString();
  42. }
  43. /**
  44. * Returns the base path for private://.
  45. *
  46. * Note that this static method is used by \Drupal\system\Form\FileSystemForm
  47. * so you should alter that form or substitute a different form if you change
  48. * the class providing the stream_wrapper.private service.
  49. *
  50. * @return string
  51. * The base path for private://.
  52. */
  53. public static function basePath() {
  54. return Settings::get('file_private_path');
  55. }
  56. }