/
FsController.php
179 lines (152 loc) · 5.36 KB
/
FsController.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\base\Fs;
use craft\base\FsInterface;
use craft\helpers\ArrayHelper;
use craft\web\Controller;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\Response as YiiResponse;
/**
* The FsController class is a controller that handles various actions related to asset filesystems, such as
* creating, editing, renaming and deleting them.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 4.0.0
*/
class FsController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
// All asset volume actions require an admin
$this->requireAdmin();
return true;
}
/**
* Shows the filesystem list.
*
* @return Response
*/
public function actionIndex(): Response
{
$variables = [];
$variables['filesystems'] = Craft::$app->getFs()->getAllFilesystems();
return $this->renderTemplate('settings/filesystems/_index.twig', $variables);
}
/**
* Edit a filesystem.
*
* @param string|null $handle The filesystem’s handle, if editing an existing filesystem.
* @param Fs|null $filesystem The filesystem being edited, if there were any validation errors.
* @return Response
* @throws ForbiddenHttpException if the user is not an admin
* @throws NotFoundHttpException if the requested volume cannot be found
*/
public function actionEdit(?string $handle = null, ?Fs $filesystem = null): Response
{
$this->requireAdmin();
$fsService = Craft::$app->getFs();
if ($filesystem === null) {
if ($handle !== null) {
$filesystem = $fsService->getFilesystemByHandle($handle);
if ($filesystem === null) {
throw new NotFoundHttpException('Filesystem not found');
}
}
}
$allFsTypes = Craft::$app->getFs()->getAllFilesystemTypes();
$fsInstances = [];
$fsOptions = [];
foreach ($allFsTypes as $fsType) {
/** @var Fs $fsInstance */
$fsInstance = Craft::createObject($fsType);
if ($filesystem === null) {
$filesystem = $fsInstance;
}
$fsInstances[$fsType] = $fsInstance;
$fsOptions[] = [
'value' => $fsType,
'label' => $fsInstance::displayName(),
];
}
// Sort them by name
ArrayHelper::multisort($fsOptions, 'label');
if ($handle && $fsService->getFilesystemByHandle($handle)) {
$title = trim($filesystem->name ?: Craft::t('app', 'Edit Filesystem'));
} else {
$title = Craft::t('app', 'Create a new filesystem');
}
return $this->asCpScreen()
->title($title)
->addCrumb(Craft::t('app', 'Settings'), 'settings')
->addCrumb(Craft::t('app', 'Filesystems'), 'settings/filesystems')
->action('fs/save')
->redirectUrl('settings/filesystems')
->addAltAction(Craft::t('app', 'Save and continue editing'), [
'redirect' => 'settings/filesystems/{handle}',
'shortcut' => true,
'retainScroll' => true,
])
->contentTemplate('settings/filesystems/_edit.twig', [
'oldHandle' => $handle,
'filesystem' => $filesystem,
'fsOptions' => $fsOptions,
'fsInstances' => $fsInstances,
'fsTypes' => $allFsTypes,
]);
}
/**
* Saves a filesystem.
*
* @return Response|null
* @throws BadRequestHttpException
*/
public function actionSave(): ?YiiResponse
{
$this->requirePostRequest();
$fsService = Craft::$app->getFs();
$type = $this->request->getBodyParam('type');
/** @var FsInterface|Fs $fs */
$fs = $fsService->createFilesystem([
'type' => $type,
'name' => $this->request->getBodyParam('name'),
'handle' => $this->request->getBodyParam('handle'),
'oldHandle' => $this->request->getBodyParam('oldHandle'),
'settings' => $this->request->getBodyParam("types.$type"),
]);
if (!$fsService->saveFilesystem($fs)) {
return $this->asModelFailure($fs, Craft::t('app', 'Couldn’t save filesystem.'), 'filesystem');
}
return $this->asModelSuccess($fs, Craft::t('app', 'Filesystem saved.'), 'filesystem');
}
/**
* Removes a filesystem.
*/
public function actionRemove(): Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();
$handle = $this->request->getRequiredBodyParam('id');
$fsService = Craft::$app->getFs();
$fs = $fsService->getFilesystemByHandle($handle);
if ($fs) {
$fsService->removeFilesystem($fs);
}
return $this->asSuccess();
}
}