ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
StreamInfoFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
34 {
35  private string $storage_dir;
36  private string $data_dir;
37 
38  public function __construct(string $storage_base_dir)
39  {
40  $this->storage_dir = $storage_base_dir;
41  if (!is_dir($this->storage_dir)) {
42  throw new \InvalidArgumentException('Storage directory does not exist');
43  }
44  }
45 
46  public function fromAccessKey(string $access_key): StreamInfo
47  {
48  $path = $this->unhash($access_key);
49  $this->checkPath($path);
50  $stream = Streams::ofResource(fopen($path, 'rb'));
51 
52  return new StreamInfo($stream, $access_key);
53  }
54 
55  public function fromFileStream(FileStream $stream): StreamInfo
56  {
57  $path = $stream->getMetadata('uri');
58  $this->checkPath($path);
59  $access_key = $this->hash($path);
60 
61  return new StreamInfo($stream, $access_key);
62  }
63 
64  private function checkPath(string $path): void
65  {
66  if (
67  strpos($path, $this->storage_dir) === false
68  || !is_readable($path)
69  || !file_exists($path)
70  ) {
71  throw new \InvalidArgumentException("Invalid access key or path");
72  }
73  }
74 
75  private function hash(string $string): string
76  {
77  return bin2hex($string);
78  }
79 
80  private function unhash(string $string): string
81  {
82  return hex2bin($string);
83  }
84 }
$path
Definition: ltiservices.php:32
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:65
Interface FileStream.
Definition: FileStream.php:33