/
FileController.php
200 lines (171 loc) · 6.66 KB
/
FileController.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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\file\controllers;
use Yii;
use yii\web\HttpException;
use yii\web\UploadedFile;
use yii\helpers\FileHelper;
use humhub\models\Setting;
use humhub\modules\file\models\File;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
/**
* UploadController provides uploading functions for files
*
* @since 0.5
*/
class FileController extends \humhub\components\Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'acl' => [
'class' => \humhub\components\behaviors\AccessControl::className(),
'guestAllowedActions' => ['download']
]
];
}
/**
* Action which handles file uploads
*
* The result is an json array of all uploaded files.
*/
public function actionUpload()
{
Yii::$app->response->format = 'json';
// Object which the uploaded file(s) belongs to (optional)
$object = null;
$objectModel = Yii::$app->request->get('objectModel');
$objectId = Yii::$app->request->get('objectId');
if ($objectModel != "" && $objectId != "" && \humhub\libs\Helpers::CheckClassType($objectModel, \yii\db\ActiveRecord::className())) {
$givenObject = $objectModel::findOne(['id' => $objectId]);
// Check if given object is HActiveRecordContent or HActiveRecordContentAddon and can be written by the current user
if ($givenObject !== null && ($givenObject instanceof ContentActiveRecord || $givenObject instanceof ContentAddonActiveRecord) && $givenObject->content->canWrite()) {
$object = $givenObject;
}
}
$files = array();
foreach (UploadedFile::getInstancesByName('files') as $cFile) {
$files[] = $this->handleFileUpload($cFile, $object);
}
return ['files' => $files];
}
/**
* Handles a single upload by given CUploadedFile and returns an array
* of informations.
*
* The 'error' attribute of the array, indicates there was an error.
*
* Informations on error:
* - error: true
* - errorMessage: some message
* - name: name of the file
* - size: file size
*
* Informations on success:
* - error: false
* - name: name of the uploaded file
* - size: file size
* - guid: of the file
* - url: url to the file
* - thumbnailUrl: url to the thumbnail if exists
*
* @param type $cFile
* @return Array Informations about the uploaded file
*/
protected function handleFileUpload($cFile, $object = null)
{
$output = array();
$file = new File();
$file->setUploadedFile($cFile);
if ($object != null) {
$file->object_id = $object->getPrimaryKey();
$file->object_model = $object->className();
}
if ($file->validate() && $file->save()) {
$output['error'] = false;
$output['guid'] = $file->guid;
$output['name'] = $file->file_name;
$output['title'] = $file->title;
$output['size'] = $file->size;
$output['mimeIcon'] = \humhub\libs\MimeHelper::getMimeIconClassByExtension($file->getExtension());
$output['mimeBaseType'] = $file->getMimeBaseType();
$output['mimeSubType'] = $file->getMimeSubType();
$output['url'] = $file->getUrl("", false);
$output['thumbnailUrl'] = $file->getPreviewImageUrl(200, 200);
} else {
$output['error'] = true;
$output['errors'] = $file->getErrors();
}
$output['name'] = $file->file_name;
$output['size'] = $file->size;
$output['deleteUrl'] = "";
$output['deleteType'] = "";
$output['thumbnailUrl'] = "";
return $output;
}
/**
* Downloads a file
*/
public function actionDownload()
{
// GUID of file
$guid = Yii::$app->request->get('guid');
// Force Download Flag
$download = Yii::$app->request->get('download', 0);
// Optional suffix of file (e.g. scaled variant of image)
$suffix = Yii::$app->request->get('suffix');
$file = File::findOne(['guid' => $guid]);
if ($file == null) {
throw new HttpException(404, Yii::t('FileModule.controllers_FileController', 'Could not find requested file!'));
}
if (!$file->canRead()) {
throw new HttpException(401, Yii::t('FileModule.controllers_FileController', 'Insufficient permissions!'));
}
if (!file_exists($file->getStoredFilePath($suffix))) {
throw new HttpException(404, Yii::t('FileModule.controllers_FileController', 'Could not find requested file!'));
}
$options = [
'inline' => false,
'mimeType' => FileHelper::getMimeTypeByExtension($file->getFilename($suffix))
];
if ($download != 1 && in_array($options['mimeType'], Yii::$app->getModule('file')->inlineMimeTypes)) {
$options['inline'] = true;
}
if (!Setting::Get('useXSendfile', 'file')) {
Yii::$app->response->sendFile($file->getStoredFilePath($suffix), $file->getFilename($suffix), $options);
} else {
$filePath = $file->getStoredFilePath($suffix);
if (strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') === 0) {
// set nginx specific X-Sendfile header name
$options['xHeader'] = 'X-Accel-Redirect';
// make path relative to docroot
$docroot = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR);
if (substr($filePath, 0, strlen($docroot)) == $docroot) {
$filePath = substr($file, strlen($docroot));
}
}
Yii::$app->response->xSendFile($filePath, $file->getFilename($suffix), $options);
}
}
public function actionDelete()
{
$this->forcePostRequest();
$guid = Yii::$app->request->post('guid');
$file = File::findOne(['guid' => $guid]);
if ($file == null) {
throw new HttpException(404, Yii::t('FileModule.controllers_FileController', 'Could not find requested file!'));
}
if (!$file->canDelete()) {
throw new HttpException(401, Yii::t('FileModule.controllers_FileController', 'Insufficient permissions!'));
}
$file->delete();
}
}