ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDAVFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use Sabre\DAV\Exception\Forbidden;
26use Sabre\DAV\IFile;
27use Psr\Http\Message\RequestInterface;
28
32class ilDAVFile implements IFile
33{
35 use ilObjFileNews;
36 use ilWebDAVCheckValidTitleTrait;
37 use ilWebDAVCommonINodeFunctionsTrait;
38
41
42 protected bool $needs_size_check = true;
43
44 protected ?int $temporary_size = null;
45
46 public function __construct(
47 protected ilObjFile $obj,
48 protected ilWebDAVRepositoryHelper $repo_helper,
49 Services $resource_storage,
50 protected RequestInterface $request,
51 protected ilWebDAVObjFactory $dav_factory,
52 protected bool $versioning_enabled
53 ) {
54 $this->resource_manager = $resource_storage->manage();
55 $this->resource_consumer = $resource_storage->consume();
56 }
57
58 protected function clearLocks(): void
59 {
60 $this->repo_helper->locks()->purgeExpiredLocksFromDB();
61 $lock = $this->repo_helper->locks()->getLockObjectWithObjIdFromDB($this->obj->getId());
62 if ($lock !== null) {
63 $this->repo_helper->locks()->removeLockWithTokenFromDB($lock->getToken());
64 }
65 }
66
70 public function put($data, ?string $name = null): ?string
71 {
72 if (!$this->repo_helper->checkAccess('write', $this->obj->getRefId())) {
73 throw new Forbidden("Permission denied. No write access for this file");
74 }
75 if ($name === null) {
76 $name = $this->getName();
77 }
78 $size = 0;
79 $name ??= $this->getName();
80 $name = $this->ensureSuffix($name, $this->extractSuffixFromFilename($name));
81
82 $stream = is_resource($data) ? Streams::ofResource($data) : Streams::ofString($data);
83 $stream_size = $stream->getSize();
84
85 if ($this->request->hasHeader("Content-Length")) {
86 $size = (int) $this->request->getHeader("Content-Length")[0];
87 }
88 if ($size === 0 && $this->request->hasHeader('X-Expected-Entity-Length')) {
89 $size = (int) $this->request->getHeader('X-Expected-Entity-Length')[0];
90 }
91
93 // remove already created file?
94 throw new Forbidden('File is too big');
95 }
96
97 if ($this->needs_size_check && $this->getSize() === 0) {
98 $parent_ref_id = $this->repo_helper->getParentOfRefId($this->obj->getRefId());
99 $file_dav = $this->dav_factory->createDAVObject($this->obj, $parent_ref_id);
100 $file_dav->noSizeCheckNeeded();
101
102 return $file_dav->put($data);
103 }
104
105 $resource = $stream->detach();
106 if ($resource === null) {
107 return null;
108 }
109 $stream = Streams::ofResource($resource);
110
111 if ($this->versioning_enabled) {
112 $version = $this->obj->getVersion(true);
113 if (($stream_content = (string) $stream) !== '') {
114 $version = $this->obj->appendStream(
115 Streams::ofString($stream_content),
116 $name
117 );
118 }
119 } else {
120 $version = $this->obj->replaceWithStream($stream, $name);
121 }
122
123 $stream->close();
124 $this->clearLocks();
125
126 if ($version > 0) {
127 // $this->obj->publish();
128 return $this->getETag();
129 }
130 return null;
131 }
132
136 public function get()
137 {
138 if (!$this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
139 throw new Forbidden("Permission denied. No read access for this file");
140 }
141
142 if (($r_id = $this->obj->getResourceId()) &&
143 ($identification = $this->resource_manager->find($r_id))) {
144 return $this->resource_consumer->stream($identification)->getStream()->getContents();
145 }
146 return '';
147 }
148
149 public function getName(): string
150 {
151 $title = $this->obj->getTitle();
152 $suffix = empty($this->obj->getFileExtension())
153 ? $this->extractSuffixFromFilename($title)
154 : $this->obj->getFileExtension();
155
156 $return_title = $this->ensureSuffix(
157 $title,
158 $suffix
159 );
160
161 return $return_title;
162 }
163
164 public function getContentType(): ?string
165 {
166 return $this->obj->getFileType();
167 }
168
169 public function getETag(): ?string
170 {
171 if ($this->getSize() > 0) {
172 return '"'
173 . sha1(
174 (string) $this->getSize() .
175 $this->getName() .
176 $this->obj->getCreateDate()
177 )
178 . '"';
179 }
180
181 return null;
182 }
183
184 public function getSize(): int
185 {
186 try {
187 return $this->obj->getFileSize();
188 } catch (Throwable) {
189 return -1;
190 }
191 }
192
193 public function noSizeCheckNeeded(): void
194 {
195 $this->needs_size_check = false;
196 }
197
198 public function setName($name): void
199 {
200 if (!$this->repo_helper->checkAccess("write", $this->obj->getRefId())) {
201 throw new Forbidden('Permission denied');
202 }
203
204 if ($this->isDAVableObjTitle($name) &&
205 $name === $this->obj->checkFileExtension($this->getName(), $name)) {
206 $this->obj->setTitle($this->ensureSuffix($name, $this->extractSuffixFromFilename($name)));
207 $this->obj->update();
208 } else {
210 }
211 }
212
213 public function delete(): void
214 {
215 $this->repo_helper->deleteObject($this->obj->getRefId());
216 }
217
218 public function getLastModified(): ?int
219 {
220 return $this->retrieveLastModifiedAsIntFromObjectLastUpdateString($this->obj->getLastUpdateDate());
221 }
222}
$version
Definition: plugin.php:24
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Consumers $resource_consumer
put($data, ?string $name=null)
bool $needs_size_check
Manager $resource_manager
__construct(protected ilObjFile $obj, protected ilWebDAVRepositoryHelper $repo_helper, Services $resource_storage, protected RequestInterface $request, protected ilWebDAVObjFactory $dav_factory, protected bool $versioning_enabled)
static getPhpUploadSizeLimitInBytes()
Class ilObjFile.
trait ilObjFileNews
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait ilObjFileSecureString
Trait ilObjFileSecureString.
ensureSuffix(string $title, ?string $suffix=null)
extractSuffixFromFilename(string $filename)