ILIAS  release_8 Revision v8.23
StreamInfoResolver.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
33 {
34  protected string $path;
35  protected ?string $file_name = null;
36  protected string $suffix;
37  protected string $mime_type;
38  protected ?\DateTimeImmutable $creation_date = null;
39  protected int $size = 0;
41 
42  public function __construct(
43  FileStream $file_stream,
46  string $revision_title,
47  ?string $file_name = null
48  ) {
49  $this->file_stream = $file_stream;
50  parent::__construct($next_version_number, $revision_owner_id, $revision_title);
51  $this->path = $file_stream->getMetadata('uri');
52  $this->initFileName($file_name);
53  $this->suffix = pathinfo($this->file_name, PATHINFO_EXTENSION);
54  $this->initSize();
55  $this->initMimeType();
56  $this->initCreationDate();
57  }
58 
59  protected function initMimeType(): void
60  {
61  $this->mime_type = 'unknown';
62  if (function_exists('mime_content_type') && file_exists($this->path)) {
63  $this->mime_type = mime_content_type($this->path);
64  return;
65  }
67  if (class_exists('finfo')) {
68  $finfo = finfo_open(FILEINFO_MIME_TYPE);
69  //We only need the first few bytes to determine the mime-type this helps to reduce RAM-Usage
70  $this->mime_type = finfo_buffer($finfo, $this->file_stream->read(255));
71  if ($this->file_stream->isSeekable()) {
72  $this->file_stream->rewind();
73  }
74  //All MS-Types are 'application/zip' we need to look at the extension to determine the type.
75  if ($this->mime_type === 'application/zip' && $this->suffix !== 'zip') {
76  $this->mime_type = $this->getMSFileTypeFromSuffix();
77  }
78  }
79  }
80 
81  protected function initSize(): void
82  {
83  $this->size = 0;
84  try {
85  $this->size = $this->file_stream->getSize();
86  } catch (\Throwable $exception) {
87  $mb_strlen_exists = function_exists('mb_strlen');
88  //We only read one MB at a time as this radically reduces RAM-Usage
89  while ($content = $this->file_stream->read(1_048_576)) {
90  if ($mb_strlen_exists) {
91  $this->size += mb_strlen($content, '8bit');
92  } else {
93  $this->size += strlen($content);
94  }
95  }
96 
97  if ($this->file_stream->isSeekable()) {
98  $this->file_stream->rewind();
99  }
100  }
101  }
102 
103  protected function initCreationDate(): void
104  {
105  $filectime = file_exists($this->path) ? filectime($this->path) : false;
106  $this->creation_date = $filectime ? (new \DateTimeImmutable())->setTimestamp(
107  $filectime
108  ) : new \DateTimeImmutable();
109  }
110 
111  protected function initFileName(?string $file_name = null): void
112  {
113  if ($file_name !== null) {
114  $this->file_name = $file_name;
115  return;
116  }
117  $this->file_name = basename($this->path);
118  if ($this->file_name === 'memory' || $this->file_name === 'input') { // in case the stream is ofString or of php://input
119  $this->file_name = $this->getRevisionTitle();
120  }
121  }
122 
123  public function getFileName(): string
124  {
125  return $this->file_name;
126  }
127 
128  public function getMimeType(): string
129  {
130  return $this->mime_type;
131  }
132 
133  public function getSuffix(): string
134  {
135  return $this->suffix;
136  }
137 
139  {
140  return $this->creation_date;
141  }
142 
143  public function getSize(): int
144  {
145  return $this->size;
146  }
147 
148  protected function getMSFileTypeFromSuffix(): string
149  {
150  $mime_types_array = MimeType::getExt2MimeMap();
151  $suffix_with_dot = '.' . $this->getSuffix();
152  if (array_key_exists($suffix_with_dot, $mime_types_array)) {
153  return $mime_types_array[$suffix_with_dot];
154  }
155  return 'application/zip';
156  }
157 }
__construct(FileStream $file_stream, int $next_version_number, int $revision_owner_id, string $revision_title, ?string $file_name=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
Interface FileStream.
Definition: FileStream.php:33