ILIAS  release_7 Revision v7.30-3-g800a261c036
StreamInfoResolver.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
6use DateTimeImmutable;
7
14{
18 protected $file_stream;
22 protected $path;
26 protected $file_name;
30 protected $suffix;
34 protected $mime_type;
38 protected $creation_date;
39 protected $size = 0;
40
41 public function __construct(
42 FileStream $stream,
45 string $revision_title
46 ) {
48 $this->file_stream = $stream;
49 $this->path = $stream->getMetadata('uri');
50 $this->initFileName();
51 $this->suffix = pathinfo($this->file_name, PATHINFO_EXTENSION);
52 $this->initSize();
53 $this->initMimeType();
54 $this->initCreationDate();
55 }
56
57 protected function initMimeType() : void
58 {
59 $this->mime_type = 'unknown';
60 if (function_exists('mime_content_type')) {
61 if (file_exists($this->path)) {
62 $this->mime_type = mime_content_type($this->path);
63 return;
64 }
65 }
66 if (class_exists('finfo')) {
67 $finfo = finfo_open(FILEINFO_MIME_TYPE);
68 //We only need the first few bytes to determine the mime-type this helps to reduce RAM-Usage
69 $this->mime_type = finfo_buffer($finfo, $this->file_stream->read(255));
70 if ($this->file_stream->isSeekable()) {
71 $this->file_stream->rewind();
72 }
73 //All MS-Types are 'application/zip' we need to look at the extension to determine the type.
74 if ($this->mime_type === 'application/zip' && $this->suffix !== 'zip') {
75 $this->mime_type = $this->getMSFileTypeFromSuffix();
76 }
77 return;
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 $t) {
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(1048576)) {
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($filectime) : new \DateTimeImmutable();
107 }
108
109 protected function initFileName() : void
110 {
111 $this->file_name = basename($this->path);
112 if ($this->file_name === 'memory' || $this->file_name === 'input') { // in case the stream is ofString or of php://input
113 $this->file_name = $this->getRevisionTitle();
114 }
115 }
116
117 public function getFileName() : string
118 {
119 return $this->file_name;
120 }
121
122 public function getMimeType() : string
123 {
124 return $this->mime_type;
125 }
126
127 public function getSuffix() : string
128 {
129 return $this->suffix;
130 }
131
132 public function getCreationDate() : DateTimeImmutable
133 {
135 }
136
137 public function getSize() : int
138 {
139 return $this->size;
140 }
141
142 protected function getMSFileTypeFromSuffix() : string
143 {
144 $mime_types_array = \ilMimeTypeUtil::getExt2MimeMap();
145 $suffix_with_dot = '.' . $this->getSuffix();
146 if (array_key_exists($suffix_with_dot, $mime_types_array)) {
147 return $mime_types_array[$suffix_with_dot];
148 }
149 return 'application/zip';
150 }
151}
An exception for terminatinating execution or to throw for unit testing.
__construct(FileStream $stream, int $next_version_number, int $revision_owner_id, string $revision_title)
Interface FileStream The base interface for all filesystem streams.
Definition: FileStream.php:18
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc