ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
16
20 public function __construct()
21 {
22 parent::__construct(
24 false,
25 0
26 );
27 }
28
32 protected function getPathPrefix()
33 {
34 return 'ilRestFileStorage';
35 }
36
40 protected function getPathPostfix()
41 {
42 return 'files';
43 }
44
48 protected function init()
49 {
50 parent::init();
51 $this->create();
52 }
53
58 public function getFile($name)
59 {
60 $GLOBALS['ilLog']->write(__METHOD__.' original name: '.$this->getPath().'/'.$name);
61
62 $real_path = realpath($this->getPath().'/'.$name);
63 if(!$real_path)
64 {
65 $GLOBALS['ilLog']->write(__METHOD__.' no realpath found for: '.$this->getPath().'/'.$name);
66 $this->responeNotFound();
67 return;
68 }
69 $file_name = basename($real_path);
70 $GLOBALS['ilLog']->write(__METHOD__.' translated name: '.$this->getPath().'/'.$file_name);
71 if(
72 $file_name &&
73 is_file($this->getPath().'/'.$file_name) &&
74 file_exists($this->getPath().'/'.$file_name)
75 )
76 {
77 $GLOBALS['ilLog']->write(__METHOD__.' delivering file: ' . $this->getPath().'/'.$file_name);
78 $return = file_get_contents($this->getPath().'/'.$file_name);
79 // Response header
80 Slim::getInstance()->response()->header('Content-Type', 'application/json');
81 Slim::getInstance()->response()->body($return);
82 return;
83 }
84
85 $this->responeNotFound();
86 }
87
88
92 protected function responeNotFound()
93 {
94 $GLOBALS['ilLog']->write(__METHOD__.' file not found.');
95 Slim::getInstance()->response()->header('Content-Type','text/html');
96 Slim::getInstance()->response()->status(404);
97 Slim::getInstance()->response()->body('Not found');
98 }
99
104 public function createFile()
105 {
106 $request = Slim::getInstance()->request();
107 $body = $request->post("content");
108
109 $tmpname = ilUtil::ilTempnam();
110 $path = $this->getPath().'/'.basename($tmpname);
111
112 $this->writeToFile($body, $path);
113 $return = basename($tmpname);
114
115 $GLOBALS['ilLog']->write(__METHOD__.' Writing to path '.$path);
116
117 Slim::getInstance()->response()->header('Content-Type', 'application/json');
118 Slim::getInstance()->response()->body($return);
119 }
120
121 public function storeFileForRest($content)
122 {
123 $tmpname = ilUtil::ilTempnam();
124 $path = $this->getPath().'/'.basename($tmpname);
125
126 $this->writeToFile($content, $path);
127 return basename($tmpname);
128 }
129
130 public function getStoredFilePath($tmpname)
131 {
132 return $this->getPath().'/'.$tmpname;
133 }
134
138 public function deleteDeprecated()
139 {
140 $max_age = time() - self::AVAILABILITY_IN_DAYS * 24 * 60 * 60;
141 $ite = new DirectoryIterator($this->getPath());
142 foreach($ite as $file)
143 {
144 if($file->getCTime() <= $max_age)
145 {
146 try {
147 @unlink($file->getPathname());
148 }
149 catch(Exception $e) {
150 $GLOBALS['ilLog']->write(__METHOD__.' '. $e->getMessage());
151 }
152 }
153 }
154 }
155}
156?>
print $file
static getInstance( $name='default')
Get Slim application with name.
Definition: Slim.php:254
writeToFile($a_data, $a_absolute_path)
Write data to file.
File storage handling.
getPathPrefix()
Get path prefix.
getPathPostfix()
Get path prefix.
responeNotFound()
Send not found response.
init()
init and create directory
deleteDeprecated()
Delete deprecated files.
createFile()
Get file by md5 hash.
getFile($name)
Get file by md5 hash.
static ilTempnam()
Create a temporary file in an ILIAS writable directory.
$GLOBALS['ct_recipient']