ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilRestFileStorage.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/FileSystem/classes/class.ilFileSystemStorage.php';
5
13{
15
19 private $logger = null;
20
21
25 public function __construct()
26 {
27 global $DIC;
28
29 $this->logger = $DIC->logger()->wsrv();
30 $this->logger->logStack();
31
32 parent::__construct(
34 false,
35 0
36 );
37 }
38
44 protected function checkWebserviceActivation(\Slim\Http\Request $request, \Slim\Http\Response $response)
45 {
46 global $DIC;
47
48 $settings = $DIC->settings();
49 if (!$settings->get('soap_user_administration', 0)) {
50 $this->logger->warning('Webservices disabled in administration.');
51
53 ->withHeader('Content-Type', 'text/html')
54 ->withStatus(\Slim\Http\StatusCode::HTTP_FORBIDDEN)
55 ->write('Webservice not enabled.');
56 return $response;
57 }
58 return null;
59 }
60
64 protected function getPathPrefix()
65 {
66 return 'ilRestFileStorage';
67 }
68
72 protected function getPathPostfix()
73 {
74 return 'files';
75 }
76
80 protected function init()
81 {
83 $this->create();
84 }
85
90 public function getFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
91 {
93 if ($failure instanceof \Slim\Http\Response) {
94 return $failure;
95 }
96
97
98 $file_id = $request->getParam('name');
99
100 $this->logger->debug('Original file name: ' . $file_id);
101
102 $real_path = realpath($this->getPath() . '/' . $file_id);
103 if (!$real_path) {
104 $this->logger->warning('No realpath found for ' . $this->getPath() . '/' . $file_id);
105 return $this->responeNotFound($response);
106 }
107 $file_name = basename($real_path);
108 $this->logger->debug('Translated name: ' . $this->getPath() . '/' . $file_name);
109 if (
110 $file_name &&
111 is_file($this->getPath() . '/' . $file_name) &&
112 file_exists($this->getPath() . '/' . $file_name)
113 ) {
114 $this->logger->info('Delivering file: ' . $this->getPath() . '/' . $file_name);
115 $return = file_get_contents($this->getPath() . '/' . $file_name);
116
117 $this->logger->dump($return);
118
120 ->withStatus(\Slim\Http\StatusCode::HTTP_OK)
121 ->withHeader('Content-Type', 'application/json')
122 ->write($return);
123 return $response;
124 }
125 $this->responeNotFound($response);
126 }
127
128
134 protected function responeNotFound(\Slim\Http\Response $response)
135 {
136 return $response
137 ->withHeader('Content-Type', 'text/html')
138 ->withStatus(\Slim\Http\StatusCode::HTTP_NOT_FOUND)
139 ->write('File not found');
140 }
141
142
143
150 public function createFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
151 {
152 $failure = $this->checkWebserviceActivation($request, $response);
153 if ($failure instanceof \Slim\Http\Response) {
154 return $failure;
155 }
156
157 $request_body = $request->getParam('content');
158
159 $tmpname = ilUtil::ilTempnam();
160 $path = $this->getPath() . '/' . basename($tmpname);
161
162 $this->writeToFile($request_body, $path);
163 $return = basename($tmpname);
164
166 ->withHeader('ContentType', 'application/json')
167 ->write($return);
168
169 return $response;
170 }
171
172 public function storeFileForRest($content)
173 {
174 $tmpname = ilUtil::ilTempnam();
175 $path = $this->getPath() . '/' . basename($tmpname);
176
177 $this->writeToFile($content, $path);
178 return basename($tmpname);
179 }
180
185 public function getStoredFilePath($tmpname)
186 {
187 return $this->getPath() . '/' . $tmpname;
188 }
189
193 public function deleteDeprecated()
194 {
195 $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
196 $ite = new DirectoryIterator($this->getPath());
197 foreach ($ite as $file) {
198 if ($file->getCTime() <= $max_age) {
199 try {
200 @unlink($file->getPathname());
201 } catch (Exception $e) {
202 $this->logger->warning($e->getMessage());
203 }
204 }
205 }
206 }
207}
$failure
$real_path
Definition: aliased.php:29
foreach($paths as $path) $request
Definition: asyncclient.php:32
while(count($oldTaskList) > 0) foreach(array_keys( $newTaskList) as $task) init()
Definition: build.php:77
An exception for terminatinating execution or to throw for unit testing.
writeToFile($a_data, $a_absolute_path)
Write data to file.
File storage handling.
checkWebserviceActivation(\Slim\Http\Request $request, \Slim\Http\Response $response)
getPathPrefix()
Get path prefix.
getFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
getPathPostfix()
Get path prefix.
init()
init and create directory
createFile(\Slim\Http\Request $request, \Slim\Http\Response $response)
Create new file from post.
deleteDeprecated()
Delete deprecated files.
responeNotFound(\Slim\Http\Response $response)
Send 403.
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Slim Framework (https://slimframework.com)
Definition: App.php:9
$response
global $DIC
Definition: saml.php:7