ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDAVFile.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 use Sabre\DAV\IFile;
28 
32 class ilDAVFile implements IFile
33 {
35  use ilObjFileNews;
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  $this->delete();
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  $version = $this->obj->getVersion(true);
112  if ($this->versioning_enabled || $version === 0) {
113  // stream may be a temp-file (due to chunked upload). we must impoort it directly
114  $uri = $stream->getMetadata('uri');
115  if ($uri === 'php://temp') {
116  $version = $this->obj->appendStream($stream, $name);
117  } elseif (($stream_content = (string) $stream) !== '') { // this may be a problem with large files
118  $version = $this->obj->appendStream(
119  Streams::ofString($stream_content),
120  $name
121  );
122  }
123  } else {
124  $version = $this->obj->replaceWithStream($stream, $name);
125  }
126 
127  $stream->close();
128  $this->clearLocks();
129 
130  if ($version > 0) {
131  // $this->obj->publish();
132  return $this->getETag();
133  }
134  return null;
135  }
136 
140  public function get()
141  {
142  if (!$this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
143  throw new Forbidden("Permission denied. No read access for this file");
144  }
145 
146  if (($r_id = $this->obj->getResourceId()) &&
147  ($identification = $this->resource_manager->find($r_id))) {
148  return $this->resource_consumer->stream($identification)->getStream()->detach();
149  }
150  return '';
151  }
152 
153  public function getName(): string
154  {
155  $title = $this->obj->getTitle();
156  $suffix = empty($this->obj->getFileExtension())
157  ? $this->extractSuffixFromFilename($title)
158  : $this->obj->getFileExtension();
159 
160  $return_title = $this->ensureSuffix(
161  $title,
162  $suffix
163  );
164 
165  return $return_title;
166  }
167 
168  public function getContentType(): ?string
169  {
170  return $this->obj->getFileType();
171  }
172 
173  public function getETag(): ?string
174  {
175  if ($this->getSize() > 0) {
176  return '"'
177  . sha1(
178  (string) $this->getSize() .
179  $this->getName() .
180  $this->obj->getCreateDate()
181  )
182  . '"';
183  }
184 
185  return null;
186  }
187 
188  public function getSize(): int
189  {
190  try {
191  return $this->obj->getFileSize();
192  } catch (Throwable) {
193  return -1;
194  }
195  }
196 
197  public function noSizeCheckNeeded(): void
198  {
199  $this->needs_size_check = false;
200  }
201 
202  public function setName($name): void
203  {
204  if (!$this->repo_helper->checkAccess("write", $this->obj->getRefId())) {
205  throw new Forbidden('Permission denied');
206  }
207 
208  if ($this->isDAVableObjTitle($name) &&
209  $name === $this->obj->checkFileExtension($this->getName(), $name)) {
210  $this->obj->setTitle($this->ensureSuffix($name, $this->extractSuffixFromFilename($name)));
211  $this->obj->update();
212  } else {
214  }
215  }
216 
217  public function delete(): void
218  {
219  $this->repo_helper->deleteObject($this->obj->getRefId());
220  }
221 
222  public function getLastModified(): ?int
223  {
224  return $this->retrieveLastModifiedAsIntFromObjectLastUpdateString($this->obj->getLastUpdateDate());
225  }
226 }
bool $needs_size_check
$version
Definition: plugin.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFile.
extractSuffixFromFilename(string $filename)
__construct(protected ilObjFile $obj, protected ilWebDAVRepositoryHelper $repo_helper, Services $resource_storage, protected RequestInterface $request, protected ilWebDAVObjFactory $dav_factory, protected bool $versioning_enabled)
trait ilObjFileSecureString
Trait ilObjFileSecureString.
Manager $resource_manager
static getPhpUploadSizeLimitInBytes()
Consumers $resource_consumer
put($data, ?string $name=null)
ensureSuffix(string $title, ?string $suffix=null)
trait ilObjFileNews
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...