@@ -31,10 +31,10 @@ |
||
31 | 31 | |
32 | 32 | public function install() |
33 | 33 | { |
34 | - Models\File::migrate(); |
|
35 | - Models\FileAccessLog::migrate(); |
|
36 | - Models\FileContent::migrate(); |
|
37 | - Models\FileRemoteAccess::migrate(); |
|
34 | + Models\File::migrate(); |
|
35 | + Models\FileAccessLog::migrate(); |
|
36 | + Models\FileContent::migrate(); |
|
37 | + Models\FileRemoteAccess::migrate(); |
|
38 | 38 | |
39 | 39 | $downloadFiles = Permission::create(['name' => 'download files']); |
40 | 40 |
@@ -13,228 +13,228 @@ |
||
13 | 13 | |
14 | 14 | class File extends Model |
15 | 15 | { |
16 | - use HasEpesiConnection; |
|
16 | + use HasEpesiConnection; |
|
17 | 17 | |
18 | 18 | public $table = 'filestorage_files'; |
19 | 19 | |
20 | - function init() { |
|
21 | - parent::init(); |
|
22 | - |
|
23 | - $this->addFields([ |
|
24 | - 'created_at' => ['caption' => __('Stored At')], |
|
25 | - 'name' => ['caption' => __('File Name')], |
|
26 | - 'link' => ['caption' => __('Link')], |
|
27 | - 'backref' |
|
28 | - ]); |
|
20 | + function init() { |
|
21 | + parent::init(); |
|
22 | + |
|
23 | + $this->addFields([ |
|
24 | + 'created_at' => ['caption' => __('Stored At')], |
|
25 | + 'name' => ['caption' => __('File Name')], |
|
26 | + 'link' => ['caption' => __('Link')], |
|
27 | + 'backref' |
|
28 | + ]); |
|
29 | 29 | |
30 | - $this->hasOne('created_by', [User::class, 'our_field' => 'created_by'])->addTitle(['field' => 'created_by_user', 'caption' => __('Stored By')]); |
|
30 | + $this->hasOne('created_by', [User::class, 'our_field' => 'created_by'])->addTitle(['field' => 'created_by_user', 'caption' => __('Stored By')]); |
|
31 | 31 | |
32 | - $this->hasOne('content', [FileContent::class, 'our_field' => 'content_id']); |
|
32 | + $this->hasOne('content', [FileContent::class, 'our_field' => 'content_id']); |
|
33 | 33 | |
34 | - $this->hasMany('links', [FileRemoteAccess::class, 'their_field' => 'file_id']); |
|
34 | + $this->hasMany('links', [FileRemoteAccess::class, 'their_field' => 'file_id']); |
|
35 | 35 | |
36 | - $this->addCalculatedField('thumbnail', [[__CLASS__, 'getThumbnailField']]); |
|
37 | - } |
|
36 | + $this->addCalculatedField('thumbnail', [[__CLASS__, 'getThumbnailField']]); |
|
37 | + } |
|
38 | 38 | |
39 | - public function userActiveLinks() |
|
40 | - { |
|
41 | - return $this->ref('links')->addCrits([ |
|
42 | - ['created_by', Auth::id()], |
|
43 | - ['expires_at', '>', date('Y-m-d H:i:s')] |
|
44 | - ]); |
|
45 | - } |
|
39 | + public function userActiveLinks() |
|
40 | + { |
|
41 | + return $this->ref('links')->addCrits([ |
|
42 | + ['created_by', Auth::id()], |
|
43 | + ['expires_at', '>', date('Y-m-d H:i:s')] |
|
44 | + ]); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Retrieve the file |
|
49 | - * |
|
50 | - * @param int|string $idOrLink Filestorage ID or unique link string |
|
51 | - * @param bool $useCache Use cache or not |
|
52 | - * |
|
53 | - * @return static |
|
54 | - * |
|
55 | - * @throws FileNotFound |
|
56 | - */ |
|
57 | - public static function retrieve($idOrLink) |
|
58 | - { |
|
59 | - $id = self::getIdByLink($idOrLink, true, true); |
|
47 | + /** |
|
48 | + * Retrieve the file |
|
49 | + * |
|
50 | + * @param int|string $idOrLink Filestorage ID or unique link string |
|
51 | + * @param bool $useCache Use cache or not |
|
52 | + * |
|
53 | + * @return static |
|
54 | + * |
|
55 | + * @throws FileNotFound |
|
56 | + */ |
|
57 | + public static function retrieve($idOrLink) |
|
58 | + { |
|
59 | + $id = self::getIdByLink($idOrLink, true, true); |
|
60 | 60 | |
61 | - $file = self::create()->tryLoad($id); |
|
61 | + $file = self::create()->tryLoad($id); |
|
62 | 62 | |
63 | - if (! $file->ref('content')['hash']) { |
|
64 | - throw new FileNotFound('File object does not have corresponding content'); |
|
65 | - } |
|
63 | + if (! $file->ref('content')['hash']) { |
|
64 | + throw new FileNotFound('File object does not have corresponding content'); |
|
65 | + } |
|
66 | 66 | |
67 | - return $file; |
|
68 | - } |
|
67 | + return $file; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get Filestorage ID by link |
|
72 | - * |
|
73 | - * @param string $link Unique link |
|
74 | - * @param bool $useCache Use cache or not |
|
75 | - * @param bool $throwException Throw exception if link is not found |
|
76 | - * |
|
77 | - * @return int Filestorage ID |
|
78 | - * @throws LinkNotFound |
|
79 | - */ |
|
80 | - public static function getIdByLink($link, $useCache = true, $throwException = false) |
|
81 | - { |
|
82 | - static $cache = []; |
|
83 | - |
|
84 | - if (is_numeric($link) || is_null($link)) return $link; |
|
85 | - |
|
86 | - if (is_object($link)) return $link['id']; |
|
87 | - |
|
88 | - if (!$useCache || !isset($cache[$link])) { |
|
89 | - $file = self::create()->tryLoadBy('link', $link); |
|
70 | + /** |
|
71 | + * Get Filestorage ID by link |
|
72 | + * |
|
73 | + * @param string $link Unique link |
|
74 | + * @param bool $useCache Use cache or not |
|
75 | + * @param bool $throwException Throw exception if link is not found |
|
76 | + * |
|
77 | + * @return int Filestorage ID |
|
78 | + * @throws LinkNotFound |
|
79 | + */ |
|
80 | + public static function getIdByLink($link, $useCache = true, $throwException = false) |
|
81 | + { |
|
82 | + static $cache = []; |
|
83 | + |
|
84 | + if (is_numeric($link) || is_null($link)) return $link; |
|
85 | + |
|
86 | + if (is_object($link)) return $link['id']; |
|
87 | + |
|
88 | + if (!$useCache || !isset($cache[$link])) { |
|
89 | + $file = self::create()->tryLoadBy('link', $link); |
|
90 | 90 | |
91 | - $cache[$link] = $file? $file->get('id'): null; |
|
91 | + $cache[$link] = $file? $file->get('id'): null; |
|
92 | 92 | |
93 | - if (!$cache[$link] && $throwException) { |
|
94 | - throw new LinkNotFound($link); |
|
95 | - } |
|
96 | - } |
|
93 | + if (!$cache[$link] && $throwException) { |
|
94 | + throw new LinkNotFound($link); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - return $cache[$link]; |
|
99 | - } |
|
98 | + return $cache[$link]; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Mark file as deleted. Does not remove any content! |
|
103 | - * |
|
104 | - * @param int|string $idOrLink Filestorage ID or unique link |
|
105 | - */ |
|
106 | - public static function unlink($idOrLink) |
|
107 | - { |
|
101 | + /** |
|
102 | + * Mark file as deleted. Does not remove any content! |
|
103 | + * |
|
104 | + * @param int|string $idOrLink Filestorage ID or unique link |
|
105 | + */ |
|
106 | + public static function unlink($idOrLink) |
|
107 | + { |
|
108 | 108 | if ($id = self::getIdByLink($idOrLink, false)) { |
109 | 109 | self::create()->delete($id); |
110 | - } |
|
111 | - } |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Check if file exists |
|
115 | - * |
|
116 | - * @param int|static $idOrMeta Filestorage ID or file object |
|
117 | - * @param bool $throwException Throw exception on missing file or return false |
|
118 | - * |
|
119 | - * @return bool True if file exists, false otherwise |
|
120 | - * @throws FileNotFound May be thrown if $throwException set to true |
|
121 | - */ |
|
122 | - public static function exists($idOrMeta, $throwException = false) |
|
123 | - { |
|
124 | - try { |
|
125 | - $file = is_numeric($idOrMeta) ? self::retrieve($idOrMeta) : $idOrMeta; |
|
113 | + /** |
|
114 | + * Check if file exists |
|
115 | + * |
|
116 | + * @param int|static $idOrMeta Filestorage ID or file object |
|
117 | + * @param bool $throwException Throw exception on missing file or return false |
|
118 | + * |
|
119 | + * @return bool True if file exists, false otherwise |
|
120 | + * @throws FileNotFound May be thrown if $throwException set to true |
|
121 | + */ |
|
122 | + public static function exists($idOrMeta, $throwException = false) |
|
123 | + { |
|
124 | + try { |
|
125 | + $file = is_numeric($idOrMeta) ? self::retrieve($idOrMeta) : $idOrMeta; |
|
126 | 126 | |
127 | - if (! file_exists($file->ref('content')['path'])) { |
|
128 | - throw new FileNotFound('Exception - file not found: ' . $file->ref('content')['path']); |
|
129 | - } |
|
130 | - } catch (\Exception $exception) { |
|
131 | - if ($throwException) |
|
132 | - throw $exception; |
|
133 | - else |
|
134 | - return false; |
|
135 | - } |
|
136 | - |
|
137 | - return true; |
|
138 | - } |
|
127 | + if (! file_exists($file->ref('content')['path'])) { |
|
128 | + throw new FileNotFound('Exception - file not found: ' . $file->ref('content')['path']); |
|
129 | + } |
|
130 | + } catch (\Exception $exception) { |
|
131 | + if ($throwException) |
|
132 | + throw $exception; |
|
133 | + else |
|
134 | + return false; |
|
135 | + } |
|
136 | + |
|
137 | + return true; |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Add multiple files, clone file if file id is provided. |
|
142 | - * May be used to update backref for all files. |
|
143 | - * |
|
144 | - * @param array $files array of existing filestorage ids or array with values for the new file |
|
145 | - * @param string|null $backref Backref for all files |
|
146 | - * @return array Newly created Meta Ids sorted in ascending order |
|
147 | - */ |
|
148 | - public static function storeMany($files, $backref = null) |
|
149 | - { |
|
150 | - $ids = []; |
|
151 | - foreach ((array) $files as $filePath) { |
|
152 | - if (! is_numeric($filePath)) { |
|
153 | - $ids[] = self::store($filePath); |
|
140 | + /** |
|
141 | + * Add multiple files, clone file if file id is provided. |
|
142 | + * May be used to update backref for all files. |
|
143 | + * |
|
144 | + * @param array $files array of existing filestorage ids or array with values for the new file |
|
145 | + * @param string|null $backref Backref for all files |
|
146 | + * @return array Newly created Meta Ids sorted in ascending order |
|
147 | + */ |
|
148 | + public static function storeMany($files, $backref = null) |
|
149 | + { |
|
150 | + $ids = []; |
|
151 | + foreach ((array) $files as $filePath) { |
|
152 | + if (! is_numeric($filePath)) { |
|
153 | + $ids[] = self::store($filePath); |
|
154 | 154 | |
155 | - continue; |
|
156 | - } |
|
155 | + continue; |
|
156 | + } |
|
157 | 157 | |
158 | - $file = self::retrieve($filePath, false); |
|
158 | + $file = self::retrieve($filePath, false); |
|
159 | 159 | |
160 | - if ($backref && $file['backref'] != $backref) { |
|
161 | - $file->save(compact('backref')); |
|
162 | - } |
|
160 | + if ($backref && $file['backref'] != $backref) { |
|
161 | + $file->save(compact('backref')); |
|
162 | + } |
|
163 | 163 | |
164 | - $ids[] = $file['id']; |
|
165 | - } |
|
164 | + $ids[] = $file['id']; |
|
165 | + } |
|
166 | 166 | |
167 | - sort($ids); |
|
167 | + sort($ids); |
|
168 | 168 | |
169 | - return $ids; |
|
170 | - } |
|
169 | + return $ids; |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * @param string|array $fileOrPath |
|
174 | - * @param string $content Content of the file |
|
175 | - * |
|
176 | - * @return int Filestorage ID |
|
177 | - * @throws LinkDuplicate |
|
178 | - */ |
|
179 | - public static function store($fileOrPath, $content = null) |
|
180 | - { |
|
181 | - $file = $fileOrPath; |
|
182 | - |
|
183 | - if (is_object($file)) { |
|
184 | - $file->action('update', [ |
|
185 | - 'content_id' => FileContent::store($content) |
|
186 | - ]); |
|
172 | + /** |
|
173 | + * @param string|array $fileOrPath |
|
174 | + * @param string $content Content of the file |
|
175 | + * |
|
176 | + * @return int Filestorage ID |
|
177 | + * @throws LinkDuplicate |
|
178 | + */ |
|
179 | + public static function store($fileOrPath, $content = null) |
|
180 | + { |
|
181 | + $file = $fileOrPath; |
|
182 | + |
|
183 | + if (is_object($file)) { |
|
184 | + $file->action('update', [ |
|
185 | + 'content_id' => FileContent::store($content) |
|
186 | + ]); |
|
187 | 187 | |
188 | - return $file['id']; |
|
189 | - } |
|
188 | + return $file['id']; |
|
189 | + } |
|
190 | 190 | |
191 | - if (! $content && is_string($fileOrPath)) { |
|
192 | - $content = file_get_contents($fileOrPath); |
|
191 | + if (! $content && is_string($fileOrPath)) { |
|
192 | + $content = file_get_contents($fileOrPath); |
|
193 | 193 | |
194 | - $file = [ |
|
195 | - 'name' => basename($fileOrPath) |
|
196 | - ]; |
|
197 | - } |
|
194 | + $file = [ |
|
195 | + 'name' => basename($fileOrPath) |
|
196 | + ]; |
|
197 | + } |
|
198 | 198 | |
199 | - if (! empty($file['link']) && self::getIdByLink($file['link'], false)) { |
|
200 | - throw new LinkDuplicate($file['link']); |
|
201 | - } |
|
199 | + if (! empty($file['link']) && self::getIdByLink($file['link'], false)) { |
|
200 | + throw new LinkDuplicate($file['link']); |
|
201 | + } |
|
202 | 202 | |
203 | - $content = $file['content']?? $content; |
|
203 | + $content = $file['content']?? $content; |
|
204 | 204 | |
205 | - if (is_array($content)) { |
|
206 | - $path = $content['path']; |
|
205 | + if (is_array($content)) { |
|
206 | + $path = $content['path']; |
|
207 | 207 | |
208 | - $file['name'] = $file['name']?? basename($path); |
|
208 | + $file['name'] = $file['name']?? basename($path); |
|
209 | 209 | |
210 | - $content = file_get_contents($path); |
|
211 | - } |
|
210 | + $content = file_get_contents($path); |
|
211 | + } |
|
212 | 212 | |
213 | - unset($file['content']); |
|
213 | + unset($file['content']); |
|
214 | 214 | |
215 | 215 | return self::create()->insert(array_merge([ |
216 | 216 | 'created_at' => date('Y-m-d H:i:s'), |
217 | 217 | 'created_by' => Auth::id(), |
218 | 218 | 'content_id' => FileContent::store($content) |
219 | 219 | ], $file)); |
220 | - } |
|
220 | + } |
|
221 | 221 | |
222 | - public static function getThumbnailField($model) |
|
223 | - { |
|
224 | - if (! $model->thumbnailPossible()) return false; |
|
222 | + public static function getThumbnailField($model) |
|
223 | + { |
|
224 | + if (! $model->thumbnailPossible()) return false; |
|
225 | 225 | |
226 | - $image = new \Imagick($model->ref('content')['path'] . '[0]'); |
|
226 | + $image = new \Imagick($model->ref('content')['path'] . '[0]'); |
|
227 | 227 | |
228 | - $image->setImageFormat('jpg'); |
|
228 | + $image->setImageFormat('jpg'); |
|
229 | 229 | |
230 | - return collect([ |
|
231 | - 'mime' => 'image/jpeg', |
|
232 | - 'name' => 'preview.jpeg', |
|
233 | - 'contents' => $image . '' |
|
234 | - ]); |
|
235 | - } |
|
230 | + return collect([ |
|
231 | + 'mime' => 'image/jpeg', |
|
232 | + 'name' => 'preview.jpeg', |
|
233 | + 'contents' => $image . '' |
|
234 | + ]); |
|
235 | + } |
|
236 | 236 | |
237 | - public function thumbnailPossible() { |
|
238 | - return $this->ref('content')['type'] == 'application/pdf' && class_exists('Imagick'); |
|
239 | - } |
|
237 | + public function thumbnailPossible() { |
|
238 | + return $this->ref('content')['type'] == 'application/pdf' && class_exists('Imagick'); |
|
239 | + } |
|
240 | 240 | } |
241 | 241 | \ No newline at end of file |
@@ -8,21 +8,21 @@ |
||
8 | 8 | |
9 | 9 | class FileAccessLog extends Model |
10 | 10 | { |
11 | - use HasEpesiConnection; |
|
11 | + use HasEpesiConnection; |
|
12 | 12 | |
13 | - public $table = 'filestorage_access_log'; |
|
13 | + public $table = 'filestorage_access_log'; |
|
14 | 14 | |
15 | - function init() { |
|
16 | - parent::init(); |
|
15 | + function init() { |
|
16 | + parent::init(); |
|
17 | 17 | |
18 | - $this->addFields([ |
|
19 | - 'file_id', |
|
20 | - 'accessed_at' => ['caption' => __('Accessed At'), 'type' => 'datetime'], |
|
21 | - 'action' => ['caption' => __('Action'), 'type' => 'enum', 'values' => ['download' => __('Download'), 'preview' => __('Preview'), 'inline' => __('Inline')]], |
|
22 | - 'ip_address' => ['caption' => __('IP Address')], |
|
23 | - 'host_name' => ['caption' => __('Host Name')] |
|
24 | - ]); |
|
18 | + $this->addFields([ |
|
19 | + 'file_id', |
|
20 | + 'accessed_at' => ['caption' => __('Accessed At'), 'type' => 'datetime'], |
|
21 | + 'action' => ['caption' => __('Action'), 'type' => 'enum', 'values' => ['download' => __('Download'), 'preview' => __('Preview'), 'inline' => __('Inline')]], |
|
22 | + 'ip_address' => ['caption' => __('IP Address')], |
|
23 | + 'host_name' => ['caption' => __('Host Name')] |
|
24 | + ]); |
|
25 | 25 | |
26 | - $this->hasOne('accessed_by', [User::class, 'our_field' => 'accessed_by'])->addTitle(['field' => 'accessed_by_user', 'caption' => __('Accessed By')]); |
|
27 | - } |
|
26 | + $this->hasOne('accessed_by', [User::class, 'our_field' => 'accessed_by'])->addTitle(['field' => 'accessed_by_user', 'caption' => __('Accessed By')]); |
|
27 | + } |
|
28 | 28 | } |
29 | 29 | \ No newline at end of file |
@@ -9,50 +9,50 @@ |
||
9 | 9 | |
10 | 10 | class FileRemoteAccess extends Model |
11 | 11 | { |
12 | - use HasEpesiConnection; |
|
12 | + use HasEpesiConnection; |
|
13 | 13 | |
14 | - const DEFAULT_PERIOD = '1 week'; |
|
14 | + const DEFAULT_PERIOD = '1 week'; |
|
15 | 15 | |
16 | - public $table = 'filestorage_remote_access'; |
|
16 | + public $table = 'filestorage_remote_access'; |
|
17 | 17 | |
18 | - public function init() |
|
19 | - { |
|
20 | - parent::init(); |
|
18 | + public function init() |
|
19 | + { |
|
20 | + parent::init(); |
|
21 | 21 | |
22 | - $this->addFields([ |
|
23 | - 'token', |
|
24 | - 'expires_at' => ['type' => 'datetime'], |
|
25 | - ]); |
|
22 | + $this->addFields([ |
|
23 | + 'token', |
|
24 | + 'expires_at' => ['type' => 'datetime'], |
|
25 | + ]); |
|
26 | 26 | |
27 | - $this->hasOne('file_id', File::class)->addTitle(['field' => 'file', 'caption' => __('File Name')]); |
|
28 | - $this->hasOne('created_by', User::class)->addTitle(['field' => 'created_by_user', 'caption' => __('Created By')]); |
|
27 | + $this->hasOne('file_id', File::class)->addTitle(['field' => 'file', 'caption' => __('File Name')]); |
|
28 | + $this->hasOne('created_by', User::class)->addTitle(['field' => 'created_by_user', 'caption' => __('Created By')]); |
|
29 | 29 | |
30 | - $this->addCalculatedField('href', [[__CLASS__, 'getHrefField']]); |
|
31 | - } |
|
30 | + $this->addCalculatedField('href', [[__CLASS__, 'getHrefField']]); |
|
31 | + } |
|
32 | 32 | |
33 | - public static function check($fileId, $token) |
|
34 | - { |
|
35 | - return (bool) self::create() |
|
36 | - ->addCrits([ |
|
37 | - ['file_id', $fileId], |
|
38 | - ['token', $token], |
|
39 | - ['expires_at', '>', date('Y-m-d H:i:s')] |
|
40 | - ]) |
|
41 | - ->action('count')->getOne(); |
|
42 | - } |
|
33 | + public static function check($fileId, $token) |
|
34 | + { |
|
35 | + return (bool) self::create() |
|
36 | + ->addCrits([ |
|
37 | + ['file_id', $fileId], |
|
38 | + ['token', $token], |
|
39 | + ['expires_at', '>', date('Y-m-d H:i:s')] |
|
40 | + ]) |
|
41 | + ->action('count')->getOne(); |
|
42 | + } |
|
43 | 43 | |
44 | - public static function grant($file, $expires = self::DEFAULT_PERIOD) |
|
45 | - { |
|
44 | + public static function grant($file, $expires = self::DEFAULT_PERIOD) |
|
45 | + { |
|
46 | 46 | return self::create()->insert([ |
47 | 47 | 'file_id' => is_numeric($file)? $file: $file->id, |
48 | 48 | 'token' => md5(uniqid(rand(), true)), |
49 | 49 | 'created_by' => Auth::id()?: 0, |
50 | 50 | 'expires_at' => date('Y-m-d H:i:s', strtotime($expires)), |
51 | 51 | ]); |
52 | - } |
|
52 | + } |
|
53 | 53 | |
54 | - public static function getHrefField($model) |
|
55 | - { |
|
56 | - return url('file') . '?' . http_build_query(['id' => $model['file_id'], 'token' => $model['token']]); |
|
57 | - } |
|
54 | + public static function getHrefField($model) |
|
55 | + { |
|
56 | + return url('file') . '?' . http_build_query(['id' => $model['file_id'], 'token' => $model['token']]); |
|
57 | + } |
|
58 | 58 | } |
59 | 59 | \ No newline at end of file |
@@ -10,135 +10,135 @@ |
||
10 | 10 | |
11 | 11 | class FileContent extends Model |
12 | 12 | { |
13 | - use HasEpesiConnection; |
|
13 | + use HasEpesiConnection; |
|
14 | 14 | |
15 | 15 | const HASH_METHOD = 'sha512'; |
16 | 16 | |
17 | 17 | public $table = 'filestorage_contents'; |
18 | 18 | |
19 | - protected $appends = ['path']; |
|
19 | + protected $appends = ['path']; |
|
20 | 20 | |
21 | - function init(){ |
|
22 | - parent::init(); |
|
21 | + function init(){ |
|
22 | + parent::init(); |
|
23 | 23 | |
24 | - $this->addFields([ |
|
25 | - 'hash', |
|
26 | - 'size', |
|
27 | - 'type' |
|
28 | - ]); |
|
24 | + $this->addFields([ |
|
25 | + 'hash', |
|
26 | + 'size', |
|
27 | + 'type' |
|
28 | + ]); |
|
29 | 29 | |
30 | - $this->hasMany('files', [File::class, 'their_field' => 'content_id']); |
|
31 | - $this->addCalculatedField('storage_path', [[__CLASS__, 'getStoragePathField']]); |
|
32 | - $this->addCalculatedField('path', [[__CLASS__, 'getPathField']]); |
|
33 | - $this->addCalculatedField('data', [[__CLASS__, 'getDataField']]); |
|
34 | - } |
|
30 | + $this->hasMany('files', [File::class, 'their_field' => 'content_id']); |
|
31 | + $this->addCalculatedField('storage_path', [[__CLASS__, 'getStoragePathField']]); |
|
32 | + $this->addCalculatedField('path', [[__CLASS__, 'getPathField']]); |
|
33 | + $this->addCalculatedField('data', [[__CLASS__, 'getDataField']]); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * One content can have many files associated with |
|
38 | - * The actual content is stored only once based on the content hash |
|
39 | - * |
|
40 | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
41 | - */ |
|
42 | - public function files() |
|
43 | - { |
|
44 | - return $this->ref('files'); |
|
45 | - } |
|
36 | + /** |
|
37 | + * One content can have many files associated with |
|
38 | + * The actual content is stored only once based on the content hash |
|
39 | + * |
|
40 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
41 | + */ |
|
42 | + public function files() |
|
43 | + { |
|
44 | + return $this->ref('files'); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Accessor method for retrieving of file content path when using the model |
|
49 | - * Having the $appends property in the File model listing the 'path' makes sure the value is also exported to arrays |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public static function getPathField($model) |
|
54 | - { |
|
55 | - return self::storage()->path($model->getStoragePath($model->get('hash'))); |
|
56 | - } |
|
47 | + /** |
|
48 | + * Accessor method for retrieving of file content path when using the model |
|
49 | + * Having the $appends property in the File model listing the 'path' makes sure the value is also exported to arrays |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public static function getPathField($model) |
|
54 | + { |
|
55 | + return self::storage()->path($model->getStoragePath($model->get('hash'))); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Accessor method for retrieving of file contents |
|
60 | - * |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public static function getDataField($model) |
|
64 | - { |
|
65 | - return self::storage()->get($model->get('storage_path')); |
|
66 | - } |
|
58 | + /** |
|
59 | + * Accessor method for retrieving of file contents |
|
60 | + * |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public static function getDataField($model) |
|
64 | + { |
|
65 | + return self::storage()->get($model->get('storage_path')); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Accessor method for file relative storage path |
|
70 | - * |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public static function getStoragePathField($model) |
|
74 | - { |
|
75 | - return $model->getStoragePath($model->get('hash')); |
|
76 | - } |
|
68 | + /** |
|
69 | + * Accessor method for file relative storage path |
|
70 | + * |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public static function getStoragePathField($model) |
|
74 | + { |
|
75 | + return $model->getStoragePath($model->get('hash')); |
|
76 | + } |
|
77 | 77 | |
78 | - protected static function getStoragePath($hash) |
|
79 | - { |
|
80 | - return implode(DIRECTORY_SEPARATOR, array_merge(str_split(substr($hash, 0, 5)), [substr($hash, 5)])); |
|
81 | - } |
|
78 | + protected static function getStoragePath($hash) |
|
79 | + { |
|
80 | + return implode(DIRECTORY_SEPARATOR, array_merge(str_split(substr($hash, 0, 5)), [substr($hash, 5)])); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Returns the storage where file contents are saved based on config settings |
|
85 | - * |
|
86 | - * @return \Illuminate\Contracts\Filesystem\Filesystem |
|
87 | - */ |
|
88 | - public static function storage() |
|
89 | - { |
|
90 | - return Storage::disk(config('epesi.filestorage', 'local')); |
|
91 | - } |
|
83 | + /** |
|
84 | + * Returns the storage where file contents are saved based on config settings |
|
85 | + * |
|
86 | + * @return \Illuminate\Contracts\Filesystem\Filesystem |
|
87 | + */ |
|
88 | + public static function storage() |
|
89 | + { |
|
90 | + return Storage::disk(config('epesi.filestorage', 'local')); |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Add file to the filestorage |
|
95 | - * |
|
96 | - * @param string $file File path to save |
|
97 | - * |
|
98 | - * @return int File id in the database |
|
99 | - */ |
|
100 | - public static function storeFromFile($file) |
|
101 | - { |
|
102 | - return self::store(file_get_contents($file)); |
|
103 | - } |
|
93 | + /** |
|
94 | + * Add file to the filestorage |
|
95 | + * |
|
96 | + * @param string $file File path to save |
|
97 | + * |
|
98 | + * @return int File id in the database |
|
99 | + */ |
|
100 | + public static function storeFromFile($file) |
|
101 | + { |
|
102 | + return self::store(file_get_contents($file)); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Add content to the filestorage |
|
107 | - * |
|
108 | - * @param string $content Content to save |
|
109 | - * |
|
110 | - * @return int File id in the database |
|
111 | - */ |
|
112 | - public static function store($content) |
|
113 | - { |
|
114 | - $hash = self::hash($content); |
|
105 | + /** |
|
106 | + * Add content to the filestorage |
|
107 | + * |
|
108 | + * @param string $content Content to save |
|
109 | + * |
|
110 | + * @return int File id in the database |
|
111 | + */ |
|
112 | + public static function store($content) |
|
113 | + { |
|
114 | + $hash = self::hash($content); |
|
115 | 115 | |
116 | - $path = self::getStoragePath($hash); |
|
116 | + $path = self::getStoragePath($hash); |
|
117 | 117 | |
118 | - if (! self::storage()->exists($path)) { |
|
119 | - self::storage()->put($path, $content); |
|
120 | - } |
|
118 | + if (! self::storage()->exists($path)) { |
|
119 | + self::storage()->put($path, $content); |
|
120 | + } |
|
121 | 121 | |
122 | - $content = self::create()->addCondition('hash', $hash); |
|
122 | + $content = self::create()->addCondition('hash', $hash); |
|
123 | 123 | |
124 | - if (! $content->action('count')->getOne()) { |
|
125 | - return $content->insert([ |
|
126 | - 'size' => self::storage()->size($path), |
|
127 | - 'type' => self::storage()->mimeType($path) |
|
128 | - ]); |
|
129 | - } |
|
124 | + if (! $content->action('count')->getOne()) { |
|
125 | + return $content->insert([ |
|
126 | + 'size' => self::storage()->size($path), |
|
127 | + 'type' => self::storage()->mimeType($path) |
|
128 | + ]); |
|
129 | + } |
|
130 | 130 | |
131 | - return $content->loadAny()->get('id'); |
|
132 | - } |
|
131 | + return $content->loadAny()->get('id'); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * Get the hash of the content using hash method defined as constant to the class |
|
136 | - * |
|
137 | - * @param string $content |
|
138 | - * @return string |
|
139 | - */ |
|
140 | - public static function hash($content) |
|
141 | - { |
|
142 | - return hash(self::HASH_METHOD, $content); |
|
143 | - } |
|
134 | + /** |
|
135 | + * Get the hash of the content using hash method defined as constant to the class |
|
136 | + * |
|
137 | + * @param string $content |
|
138 | + * @return string |
|
139 | + */ |
|
140 | + public static function hash($content) |
|
141 | + { |
|
142 | + return hash(self::HASH_METHOD, $content); |
|
143 | + } |
|
144 | 144 | } |
145 | 145 | \ No newline at end of file |
@@ -6,30 +6,30 @@ |
||
6 | 6 | |
7 | 7 | class FileStorageAccess |
8 | 8 | { |
9 | - public function handle($request, \Closure $next) |
|
10 | - { |
|
11 | - foreach (Integration\Joints\FileStorageAccessJoint::collect() as $joint) { |
|
12 | - if ($joint->accessGranted($request)) { |
|
13 | - $this->logFileAccess($request->get('id'), $request->get('action', 'download')); |
|
9 | + public function handle($request, \Closure $next) |
|
10 | + { |
|
11 | + foreach (Integration\Joints\FileStorageAccessJoint::collect() as $joint) { |
|
12 | + if ($joint->accessGranted($request)) { |
|
13 | + $this->logFileAccess($request->get('id'), $request->get('action', 'download')); |
|
14 | 14 | |
15 | - return $next($request); |
|
16 | - } |
|
17 | - } |
|
15 | + return $next($request); |
|
16 | + } |
|
17 | + } |
|
18 | 18 | |
19 | - return response('No access to file', 401); |
|
20 | - } |
|
19 | + return response('No access to file', 401); |
|
20 | + } |
|
21 | 21 | |
22 | - protected function logFileAccess($fileId, $action, $time = null) |
|
23 | - { |
|
24 | - $ip_address = request()->ip(); |
|
22 | + protected function logFileAccess($fileId, $action, $time = null) |
|
23 | + { |
|
24 | + $ip_address = request()->ip(); |
|
25 | 25 | |
26 | - Models\FileAccessLog::create()->insert([ |
|
27 | - 'file_id' => $fileId, |
|
28 | - 'accessed_at' => date('Y-m-d H:i:s', $time ?: time()), |
|
29 | - 'accessed_by' => Auth::id() ?: 0, |
|
30 | - 'action' => $action, |
|
31 | - 'ip_address' => $ip_address, |
|
32 | - 'host_name' => gethostbyaddr($ip_address) |
|
33 | - ]); |
|
34 | - } |
|
26 | + Models\FileAccessLog::create()->insert([ |
|
27 | + 'file_id' => $fileId, |
|
28 | + 'accessed_at' => date('Y-m-d H:i:s', $time ?: time()), |
|
29 | + 'accessed_by' => Auth::id() ?: 0, |
|
30 | + 'action' => $action, |
|
31 | + 'ip_address' => $ip_address, |
|
32 | + 'host_name' => gethostbyaddr($ip_address) |
|
33 | + ]); |
|
34 | + } |
|
35 | 35 | } |
@@ -7,52 +7,52 @@ |
||
7 | 7 | |
8 | 8 | class FileStorageController extends Controller |
9 | 9 | { |
10 | - public function get(Request $request) |
|
11 | - { |
|
12 | - try { |
|
13 | - $file = Models\File::retrieve($request->get('id')); |
|
14 | - } catch (\Exception $e) { |
|
15 | - abort(404); |
|
16 | - } |
|
10 | + public function get(Request $request) |
|
11 | + { |
|
12 | + try { |
|
13 | + $file = Models\File::retrieve($request->get('id')); |
|
14 | + } catch (\Exception $e) { |
|
15 | + abort(404); |
|
16 | + } |
|
17 | 17 | |
18 | - $useThumbnail = false; |
|
19 | - $disposition = 'attachment'; |
|
20 | - switch ($request->get('action')) { |
|
21 | - case 'preview': |
|
22 | - $useThumbnail = true; |
|
23 | - // intended fallthrough |
|
24 | - case 'inline': |
|
25 | - $disposition = 'inline'; |
|
26 | - break; |
|
18 | + $useThumbnail = false; |
|
19 | + $disposition = 'attachment'; |
|
20 | + switch ($request->get('action')) { |
|
21 | + case 'preview': |
|
22 | + $useThumbnail = true; |
|
23 | + // intended fallthrough |
|
24 | + case 'inline': |
|
25 | + $disposition = 'inline'; |
|
26 | + break; |
|
27 | 27 | |
28 | - default: |
|
29 | - break; |
|
30 | - } |
|
28 | + default: |
|
29 | + break; |
|
30 | + } |
|
31 | 31 | |
32 | - if ($useThumbnail && $request->get('thumbnail', 1) && ($thumbnail = $file['thumbnail'])) { |
|
33 | - $mime = $thumbnail['mime']; |
|
34 | - $filename = $thumbnail['name']; |
|
35 | - $contents = $thumbnail['contents']; |
|
36 | - } |
|
37 | - else { |
|
38 | - $content = $file->ref('content'); |
|
32 | + if ($useThumbnail && $request->get('thumbnail', 1) && ($thumbnail = $file['thumbnail'])) { |
|
33 | + $mime = $thumbnail['mime']; |
|
34 | + $filename = $thumbnail['name']; |
|
35 | + $contents = $thumbnail['contents']; |
|
36 | + } |
|
37 | + else { |
|
38 | + $content = $file->ref('content'); |
|
39 | 39 | |
40 | - $mime = $content['type']; |
|
41 | - $filename = $file['name']; |
|
42 | - $contents = $content['data']; |
|
43 | - } |
|
40 | + $mime = $content['type']; |
|
41 | + $filename = $file['name']; |
|
42 | + $contents = $content['data']; |
|
43 | + } |
|
44 | 44 | |
45 | - $headers = [ |
|
46 | - 'Content-Type' => $mime, |
|
47 | - 'Content-Length' => strlen($contents), |
|
48 | - 'Content-Disposition' => "$disposition; filename=\"$filename\"", |
|
49 | - ]; |
|
45 | + $headers = [ |
|
46 | + 'Content-Type' => $mime, |
|
47 | + 'Content-Length' => strlen($contents), |
|
48 | + 'Content-Disposition' => "$disposition; filename=\"$filename\"", |
|
49 | + ]; |
|
50 | 50 | |
51 | - if ($request->get('nocache')) { |
|
52 | - $headers['Pragma'] = 'no-cache'; |
|
53 | - $headers['Expires'] = '0'; |
|
54 | - } |
|
51 | + if ($request->get('nocache')) { |
|
52 | + $headers['Pragma'] = 'no-cache'; |
|
53 | + $headers['Expires'] = '0'; |
|
54 | + } |
|
55 | 55 | |
56 | - return response($contents, 200, $headers); |
|
57 | - } |
|
56 | + return response($contents, 200, $headers); |
|
57 | + } |
|
58 | 58 | } |