ILIAS  release_8 Revision v8.23
class.ilDAVFile.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
27 use Sabre\DAV\IFile;
29 
33 class ilDAVFile implements IFile
34 {
35  use ilObjFileNews;
38 
39  protected ilObjFile $obj;
45 
46  protected bool $needs_size_check = true;
47  protected bool $versioning_enabled;
48 
49  public function __construct(
50  ilObjFile $obj,
51  ilWebDAVRepositoryHelper $repo_helper,
52  Services $resource_storage,
53  RequestInterface $request,
54  ilWebDAVObjFactory $dav_factory,
55  bool $versioning_enabled
56  ) {
57  $this->obj = $obj;
58  $this->repo_helper = $repo_helper;
59  $this->resource_manager = $resource_storage->manage();
60  $this->resource_consumer = $resource_storage->consume();
61  $this->request = $request;
62  $this->dav_factory = $dav_factory;
63  $this->versioning_enabled = $versioning_enabled;
64  }
65 
69  public function put($data): ?string
70  {
71  if (!$this->repo_helper->checkAccess('write', $this->obj->getRefId())) {
72  throw new Forbidden("Permission denied. No write access for this file");
73  }
74 
75  $size = 0;
76 
77  if ($this->request->hasHeader("Content-Length")) {
78  $size = (int) $this->request->getHeader("Content-Length")[0];
79  }
80  if ($size === 0 && $this->request->hasHeader('X-Expected-Entity-Length')) {
81  $size = (int) $this->request->getHeader('X-Expected-Entity-Length')[0];
82  }
83 
85  throw new Forbidden('File is too big');
86  }
87 
88  if ($this->needs_size_check && $this->getSize() === 0) {
89  $parent_ref_id = $this->repo_helper->getParentOfRefId($this->obj->getRefId());
90  $obj_id = $this->obj->getId();
91  $this->repo_helper->deleteObject($this->obj->getRefId());
92  $file_obj = new ilObjFile();
93  $file_obj->setTitle($this->getName());
94  $file_obj->setFileName($this->getName());
95 
96  $file_dav = $this->dav_factory->createDAVObject($file_obj, $parent_ref_id);
97  $file_dav->noSizeCheckNeeded();
98  $this->repo_helper->updateLocksAfterResettingObject($obj_id, $file_obj->getId());
99  return $file_dav->put($data);
100  }
101 
102  $stream = Streams::ofResource($data);
103 
104  if ($this->versioning_enabled === true ||
105  $this->obj->getVersion() === 0 && $this->obj->getMaxVersion() === 0) {
106  $this->obj->appendStream($stream, $this->obj->getTitle());
107  } else {
108  $this->obj->replaceWithStream($stream, $this->obj->getTitle());
109  }
110 
111  $stream->close();
112 
113  return $this->getETag();
114  }
115 
119  public function get()
120  {
121  if (!$this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
122  throw new Forbidden("Permission denied. No read access for this file");
123  }
124 
125  if (($r_id = $this->obj->getResourceId()) &&
126  ($identification = $this->resource_manager->find($r_id))) {
127  return $this->resource_consumer->stream($identification)->getStream()->getContents();
128  }
129 
130  throw new NotFound("File not found");
131  }
132 
133  public function getName(): string
134  {
135  return ilFileUtils::getValidFilename($this->obj->getTitle());
136  }
137 
138  public function getContentType(): ?string
139  {
140  return $this->obj->getFileType();
141  }
142 
143  public function getETag(): ?string
144  {
145  if ($this->getSize() > 0) {
146  return '"' . sha1(
147  $this->getSize() .
148  $this->getName() .
149  $this->obj->getCreateDate()
150  ) . '"';
151  }
152 
153  return null;
154  }
155 
156  public function getSize(): int
157  {
158  try {
159  return $this->obj->getFileSize();
160  } catch (Error $e) {
161  return -1;
162  }
163  }
164 
165  public function noSizeCheckNeeded(): void
166  {
167  $this->needs_size_check = false;
168  }
169 
170  public function setName($name): void
171  {
172  if (!$this->repo_helper->checkAccess("write", $this->obj->getRefId())) {
173  throw new Forbidden('Permission denied');
174  }
175 
176  if ($this->isDAVableObjTitle($name) &&
177  $name === $this->obj->checkFileExtension($this->getName(), $name)) {
178  $this->obj->setTitle($name);
179  $this->obj->update();
180  } else {
182  }
183  }
184 
185  public function delete(): void
186  {
187  $this->repo_helper->deleteObject($this->obj->getRefId());
188  }
189 
190  public function getLastModified(): ?int
191  {
192  return $this->retrieveLastModifiedAsIntFromObjectLastUpdateString($this->obj->getLastUpdateDate());
193  }
194 }
bool $needs_size_check
RequestInterface $request
static getValidFilename(string $a_filename)
__construct(ilObjFile $obj, ilWebDAVRepositoryHelper $repo_helper, Services $resource_storage, RequestInterface $request, ilWebDAVObjFactory $dav_factory, bool $versioning_enabled)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getUploadSizeLimitBytes()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilWebDAVRepositoryHelper $repo_helper
Class ilObjFile.
ilObjFile $obj
bool $versioning_enabled
Manager $resource_manager
Consumers $resource_consumer
ilWebDAVObjFactory $dav_factory
Class StorageManager.
Definition: Manager.php:39
trait ilObjFileNews
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...