-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFile.php
More file actions
344 lines (302 loc) · 7.33 KB
/
File.php
File metadata and controls
344 lines (302 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php namespace models\main;
/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\ORM\Mapping AS ORM;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use models\utils\SilverstripeBaseModel;
use Illuminate\Support\Facades\Config;
/**
* @ORM\Entity(repositoryClass="repositories\main\DoctrineFolderRepository")
* @ORM\Table(name="File")
* Class File
* @package models\main
*/
class File extends SilverstripeBaseModel
{
/**
* @ORM\Column(name="Name", type="string")
*/
private $name;
/**
* @ORM\Column(name="Title", type="string")
*/
private $title;
/**
* @ORM\Column(name="ClassName", type="string")
*/
private $class_name;
/**
* @ORM\Column(name="Content", type="string")
*/
private $content;
/**
* @ORM\Column(name="Filename", type="string")
*/
private $filename;
/**
* @ORM\Column(name="ShowInSearch", type="boolean")
* @var bool
*/
private $show_in_search;
/**
* @ORM\Column(name="CloudStatus", type="string")
*/
private $cloud_status;
/**
* @ORM\Column(name="CloudSize", type="integer")
*/
private $cloud_size;
/**
* @ORM\Column(name="CloudMetaJson", type="string")
*/
private $cloud_metajson;
/**
* @ORM\ManyToOne(targetEntity="models\main\File")
* @ORM\JoinColumn(name="ParentID", referencedColumnName="ID")
* @var File
*/
private $parent;
/**
* @ORM\ManyToOne(targetEntity="models\main\Member")
* @ORM\JoinColumn(name="OwnerID", referencedColumnName="ID")
* @var Member
*/
private $owner;
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @param mixed $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* @return mixed
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param mixed $filename
*/
public function setFilename($filename)
{
$this->filename = $filename;
}
/**
* @return bool
*/
public function isShowInSearch()
{
return $this->show_in_search;
}
/**
* @param bool $show_in_search
*/
public function setShowInSearch($show_in_search)
{
$this->show_in_search = $show_in_search;
}
/**
* @return File
*/
public function getParent()
{
return $this->parent;
}
/**
* @param File $parent
*/
public function setParent($parent)
{
$this->parent = $parent;
}
/**
* @return Member
*/
public function getOwner()
{
return $this->owner;
}
/**
* @param Member $owner
*/
public function setOwner($owner)
{
$this->owner = $owner;
}
public function __construct()
{
parent::__construct();
$this->class_name = 'CloudFile';
$this->show_in_search = true;
$this->cloud_metajson = "";
$this->cloud_status = "Local";
$this->cloud_size = 0;
}
public function setImage(){
$this->class_name = 'CloudImage';
}
public function setFolder(){
$this->class_name = 'Folder';
}
/**
* @return string
*/
public function getCloudStatus()
{
return $this->cloud_status;
}
/**
* @param string $cloud_status
*/
public function setCloudStatus($cloud_status): void
{
$this->cloud_status = $cloud_status;
}
/**
* @return int
*/
public function getCloudSize()
{
return $this->cloud_size;
}
/**
* @param int $cloud_size
*/
public function setCloudSize($cloud_size): void
{
$this->cloud_size = $cloud_size;
}
/**
* @param string $cloud_metajson
*/
public function setCloudMetaJSON($cloud_metajson): void
{
$this->cloud_metajson = $cloud_metajson;
}
/**
* @return string
*/
public function getUrl()
{
$local = Config::get("server.assets_base_url", 'https://www.openstack.org/').$this->getFilename();
return $this->cloud_status == 'Live' ? $this->getCloudLink() : $local;
}
/**
* @return string
*/
public function getRelativeLinkFor()
{
$fn = $this->getFilename();
return trim(str_replace("assets", '', $fn), '/');
}
/**
* @return string
*/
public function getCloudLink()
{
try {
$relativeLink = ltrim($this->getRelativeLinkFor(), '/');
$disk = Storage::disk(Config::get('filesystems.assets_disk', 'assets'));
if ($disk == null) return null;
return $disk->url($relativeLink);
} catch (\Exception $ex) {
Log::warning($ex);
return null;
}
}
/**
* @param string $imageRelativePath
* @return string|null
*/
public static function getCloudLinkForImages(string $imageRelativePath):?string {
try {
$imageRelativePath = ltrim($imageRelativePath, '/');
$disk = Storage::disk(Config::get('filesystems.static_images_disk', 'static_images'));
if ($disk == null) return null;
return $disk->url($imageRelativePath);
} catch (\Exception $ex) {
Log::warning($ex);
return null;
}
}
/**
* @param string|array $key - passing an array as the first argument replaces the meta data entirely
* @param mixed $val
* @return File - chainable
*/
public function setCloudMeta($key, $val = null)
{
if (is_array($key)) {
$data = $key;
} else {
$data = $this->getCloudMetaJSON();
$data[$key] = $val;
}
$this->cloud_metajson = json_encode($data);
return $this;
}
/**
* @param string $key [optional] - if not present returns the whole array
* @return array
*/
public function getCloudMetaJSON($key = null)
{
$data = json_decode($this->cloud_metajson, true);
if (empty($data) || !is_array($data)) {
$data = array();
}
if (!empty($key)) {
return isset($data[$key]) ? $data[$key] : null;
} else {
return $data;
}
}
}