forked from dmstr/yii2-filefly-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
221 lines (188 loc) · 5.72 KB
/
Module.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
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
<?php
/**
* @link http://www.herzogkommunikation.de/
* @copyright Copyright (c) 2017 herzog kommunikation GmbH, Stuttgart
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace hrzg\filefly;
use creocoder\flysystem\Filesystem;
use dmstr\web\traits\AccessBehaviorTrait;
use hrzg\filefly\helpers\FsManager;
use yii\web\HttpException;
/**
* Class Module
* @package hrzg\filefly
* @author Christopher Stebe <[email protected]>
*
* @property-read \hrzg\filefly\helpers\FsManager $manager
* @property-read callable $thumbnailCallback
*/
class Module extends \yii\base\Module
{
use AccessBehaviorTrait;
const NAME = 'filefly';
/**
* Access fields
*/
const ACCESS_OWNER = 'access_owner';
const ACCESS_READ = 'access_read';
const ACCESS_UPDATE = 'access_update';
const ACCESS_DELETE = 'access_delete';
/**
* Module roles
*/
const ACCESS_ROLE_ADMIN = 'FileflyAdmin';
const ACCESS_ROLE_DEFAULT = 'FileflyDefault';
const ACCESS_ROLE_API = 'FileflyApi';
const ACCESS_ROLE_PERMISSIONS = 'FileflyPermissions';
/**
* Module permissions
*/
const ACCESS_PERMISSION_ADMIN = 'filefly';
const ACCESS_PERMISSION_DEFAULT = 'filefly_default_index';
const ACCESS_PERMISSION_API = 'filefly_api_index';
/**
* @var array
*/
public $accessFields = [self::ACCESS_OWNER, self::ACCESS_READ, self::ACCESS_UPDATE, self::ACCESS_DELETE];
/**
* @var array
*/
public $accessRoles = [
self::ACCESS_ROLE_ADMIN => [self::ACCESS_PERMISSION_ADMIN],
self::ACCESS_ROLE_DEFAULT => [self::ACCESS_PERMISSION_DEFAULT],
self::ACCESS_ROLE_API => [self::ACCESS_PERMISSION_API],
self::ACCESS_ROLE_PERMISSIONS => [],
];
/**
* The name of the filesystem component
* @var string
*/
public $filesystem;
/**
* @var \creocoder\flysystem\Filesystem default filesystem
*/
public $filesystemComponent;
/**
* @var array mapping for filesystems 'scheme' => 'component'
*/
public $filesystemComponents = [];
/**
* @inheritdoc
*/
public $controllerNamespace = 'hrzg\filefly\controllers';
/**
* use \yii\helpers\Inflector::slug() for file names on create/upload
* @var bool
*/
public $slugNames = true;
/**
* Active / deactivate the filesystem and hashmap self healing plugins
* @var bool
*/
public $repair = true;
/**
* Active / deactivate the filesystem recursive folder delete
* @var bool
*/
public $deleteRecursive = false;
/**
* Used as default permissions on active \hrzg\filefly\plugins\RepairKit
* if $this->repair = true
*
* @var array
*/
public $defaultPermissions = [
self::ACCESS_OWNER => 1,
self::ACCESS_READ => '*',
self::ACCESS_UPDATE => '*',
self::ACCESS_DELETE => '*',
];
/**
* @var null role for accessing and managing items on the root folder, `null` means anyone
*/
public $rootFolderManageRole = null;
/**
* Offset (in seconds) for Expires Header in stream action, default: 1 week
* @var int
*/
public $streamExpireOffset = 604800;
private $_manager;
/**
* @var callable $thumbnailCallback
*
* Example:
*
* ```php
* function ($path) {
* return dmstr\willnorrisImageproxy\Url::image($path, '50x50q50');
* }
* ```
*/
public $thumbnailCallback;
/**
* optional callback that can define a list of urls that will be displayed in filemanager
* backend in the item 'copy urls' overlay.
*
* Return value of the callback has to be an array!
*
* @var null|callable
*/
public $urlCallback;
/**
* optional callback that can define the preview URL for an item
* if not set the default filemanager downloadURL will be used
*
* @var
*/
public $previewCallback;
/**
* additional options for the angular fileManagerApp
* @see: dmstr/yii2-filemanager-widgets
*
* @var array
*/
public $fileManagerWidgetOptions = [];
/**
* @inheritdoc
*
* @throws HttpException
*/
public function beforeAction($action)
{
parent::beforeAction($action);
if (empty($this->filesystem)) {
\Yii::$app->session->addFlash(
'warning',
'No filesystem configured for <code>filefly</code> module'
);
\Yii::warning('Filesystem not configured.', __METHOD__);
} else {
// set the yii component name of the filesystem
$fsComponentName = $this->filesystem;
// get the component object
$this->filesystemComponent = \Yii::$app->{$fsComponentName};
if ( ! $this->filesystemComponent instanceof Filesystem) {
\Yii::$app->session->addFlash(
'error',
'Filesystem component is no instance of creocoder\flysystem\Filesystem'
);
\Yii::error('Invalid filesystem component.', __METHOD__);
}
}
// breadcrumbs
\Yii::$app->controller->view->params['breadcrumbs'][] = ['label' => 'Filefly module', 'url' => ['/'.$this->id]];
// Set the allowed MIME types to all files as default
\Yii::$app->settings->getOrSet('mime-whitelist', '', 'filefly', 'string');
return true;
}
public function getManager(){
if (!$this->_manager) {
$this->_manager = new FsManager();
$this->_manager->setModule($this);
}
return $this->_manager;
}
}