/
StorageLocation.php
174 lines (148 loc) · 4.23 KB
/
StorageLocation.php
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
<?php
namespace Concrete\Core\Entity\File\StorageLocation;
use Concrete\Core\Cache\Level\ExpensiveCache;
use Concrete\Core\File\StorageLocation\StorageLocationInterface;
use Concrete\Core\Filesystem\FlysystemCache;
use Database;
use Doctrine\ORM\Mapping as ORM;
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\Storage\Psr6Cache;
/**
* @ORM\Entity
* @ORM\Table(name="FileStorageLocations")
*/
class StorageLocation implements StorageLocationInterface
{
/**
* @ORM\Column(type="string")
*/
protected $fslName;
/**
* @ORM\Column(type="object")
*/
protected $fslConfiguration;
/**
* @ORM\Id @ORM\Column(type="integer", options={"unsigned": true})
* @ORM\GeneratedValue
*/
protected $fslID;
/**
* @ORM\OneToMany(targetEntity="\Concrete\Core\Entity\File\File", mappedBy="storageLocation")
**/
protected $files;
/**
* @ORM\Column(type="boolean")
*/
protected $fslIsDefault = false;
/**
* @return int
*/
public function getID()
{
return $this->fslID;
}
/**
* @return string
*/
public function getName()
{
return $this->fslName;
}
/** Returns the display name for this storage location (localized and escaped accordingly to $format)
* @param string $format = 'html'
* Escape the result in html format (if $format is 'html').
* If $format is 'text' or any other value, the display name won't be escaped.
*
* @return string
*/
public function getDisplayName($format = 'html')
{
$value = tc('StorageLocationName', $this->getName());
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
/**
* @param string $fslName
*/
public function setName($fslName)
{
$this->fslName = $fslName;
}
/**
* @param bool $fslIsDefault
*/
public function setIsDefault($fslIsDefault)
{
$this->fslIsDefault = $fslIsDefault;
}
/**
* @return \Concrete\Core\File\StorageLocation\Configuration\ConfigurationInterface
*/
public function getConfigurationObject()
{
return $this->fslConfiguration;
}
/**
* @return bool
*/
public function isDefault()
{
return $this->fslIsDefault;
}
public function setConfigurationObject($configuration)
{
$this->fslConfiguration = $configuration;
}
public function getTypeObject()
{
$configuration = $this->getConfigurationObject();
$type = $configuration->getTypeObject();
return $type;
}
/**
* Returns the proper file system object for the current storage location, by mapping
* it through Flysystem.
*
* @return \League\Flysystem\Filesystem
*/
public function getFileSystemObject()
{
$adapter = $this->getConfigurationObject()->getAdapter();
$pool = \Core::make(ExpensiveCache::class)->pool;
$cache = new Psr6Cache($pool, 'flysystem-id-' . $this->getID());
$cachedAdapter = new FlysystemCache($adapter, $cache);
$filesystem = new \League\Flysystem\Filesystem($cachedAdapter);
return $filesystem;
}
public function delete()
{
$default = \Concrete\Core\File\StorageLocation\StorageLocation::getDefault();
$db = Database::get();
$fIDs = $db->GetCol('select fID from Files where fslID = ?', [$this->getID()]);
foreach ($fIDs as $fID) {
$file = \File::getByID($fID);
if (is_object($file)) {
$file->setFileStorageLocation($default);
}
}
$em = \ORM::entityManager();
$em->remove($this);
$em->flush();
}
public function save()
{
$default = \Concrete\Core\File\StorageLocation\StorageLocation::getDefault();
$em = \ORM::entityManager();
$em->persist($this);
if ($this->isDefault() && is_object($default) && $default->getID() != $this->getID()) {
$default->setIsDefault(false);
$em->persist($default);
}
$em->flush();
}
}