-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFile.php
500 lines (427 loc) · 13.6 KB
/
File.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
namespace humhub\modules\cfiles\models;
use humhub\modules\cfiles\libs\FileUtils;
use humhub\modules\comment\models\Comment;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\content\widgets\richtext\RichText;
use humhub\modules\file\libs\FileHelper;
use humhub\modules\file\models\File as BaseFile;
use humhub\modules\file\models\FileUpload;
use humhub\modules\post\models\Post;
use humhub\modules\search\events\SearchAddEvent;
use humhub\modules\topic\models\Topic;
use humhub\modules\user\models\User;
use Yii;
use yii\db\ActiveQuery;
use yii\web\UploadedFile;
/**
* This is the model class for table "cfiles_file".
*
* @property int $id
* @property int $parent_folder_id
* @property string $description
* @property int $download_count
*
* @property Folder $parentFolder
* @property BaseFile $baseFile
*/
class File extends FileSystemItem
{
/**
* @inheritdoc
*/
public $wallEntryClass = "humhub\modules\cfiles\widgets\WallEntryFile";
/**
* @var File
*/
protected $_setFileContent = null;
/**
* @var array Content topics/tags
*/
public $topics = [];
/**
* @inheritdoc
*/
public $fileManagerEnableHistory = true;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'cfiles_file';
}
/**
* @inheritdoc
*/
public function getContentName()
{
return Yii::t('CfilesModule.base', "File");
}
/**
* @inheritdoc
*/
public function getContentDescription()
{
return $this->getTitle();
}
/**
* @inheritdoc
*/
public function getIcon()
{
return FileUtils::getIconClassByExt(FileHelper::getExtension($this->baseFile));
}
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
[['parent_folder_id'], 'required'],
['parent_folder_id', 'integer'],
['parent_folder_id', 'validateParentFolderId'],
['description', 'string', 'max' => 1000],
['topics', 'safe'],
['hidden', 'boolean'],
];
if ($this->parentFolder && $this->parentFolder->content->isPublic()) {
$rules[] = ['visibility', 'integer', 'min' => 0, 'max' => 1];
}
return $rules;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return array_merge(parent::attributeLabels(), [
'id' => 'ID',
'parent_folder_id' => Yii::t('CfilesModule.base', 'Folder ID'),
]);
}
/**
* @inheritdoc
*/
public function getSearchAttributes()
{
$attributes = [
'description' => $this->description,
];
if ($this->getCreator()) {
$attributes['creator'] = $this->getCreator()->getDisplayName();
}
if ($this->getEditor()) {
$attributes['editor'] = $this->getEditor()->getDisplayName();
}
if ($this->baseFile) {
$attributes['name'] = $this->getTitle();
}
$this->trigger(self::EVENT_SEARCH_ADD, new SearchAddEvent($attributes));
return $attributes;
}
public function setUploadedFile(UploadedFile $uploadedFile): bool
{
if ($this->baseFile) {
$baseFile = FileUpload::findOne($this->baseFile->id);
} else {
$baseFile = new FileUpload(['show_in_stream' => false]);
}
$baseFile->setUploadedFile($uploadedFile);
return $this->setFileContent($baseFile);
}
public function setFileContent(BaseFile $fileContent): bool
{
$this->populateRelation('baseFile', $fileContent);
// Temp Fix: https://github.com/yiisoft/yii2/issues/15875
$this->_setFileContent = $fileContent;
return $this->baseFile->validate();
}
/**
* @inheritdoc
*/
public function afterFind()
{
$this->topics = Topic::findByContent($this->content);
parent::afterFind();
}
public function afterSave($insert, $changedAttributes)
{
// Temp Fix: https://github.com/yiisoft/yii2/issues/15875
if ($this->_setFileContent !== null) {
$this->populateRelation('baseFile', $this->_setFileContent);
}
$isNewBaseFile = $this->baseFile && ($insert || $this->baseFile->isNewRecord);
if ($isNewBaseFile) {
$this->baseFile->setPolymorphicRelation($this);
}
$fileTitleChanged = ($this->baseFile && $this->baseFile->getOldAttribute('file_name') != $this->baseFile->file_name);
$newVersionUploaded = ($this->baseFile && isset($this->baseFile->uploadedFile) && $this->baseFile->uploadedFile instanceof UploadedFile);
// Insert new base File OR Update the existing File if title has been changed or new file version has been uploaded
if ($isNewBaseFile || $fileTitleChanged || $newVersionUploaded) {
$this->baseFile->save(false);
}
// Save topics
Topic::attach($this->content, $this->topics);
$this->updateVisibility($this->visibility);
parent::afterSave($insert, $changedAttributes);
RichText::postProcess($this->description, $this);
}
public function updateVisibility($visibility)
{
if ($visibility === null) {
return;
}
if (!$this->parentFolder->content->isPrivate() || $visibility == Content::VISIBILITY_PRIVATE) {
// For user profile files we use Content::VISIBILITY_OWNER isntead of private
$this->content->visibility = $visibility;
}
}
public function getVisibilityTitle()
{
if (Yii::$app->getModule('friendship')->settings->get('enable') && $this->content->container instanceof User) {
if ($this->content->container->isCurrentuser()) {
$privateText = Yii::t('CfilesModule.base', 'This file is only visible for you and your friends.');
} else {
$privateText = Yii::t('CfilesModule.base', 'This file is protected.');
}
return $this->content->isPublic()
? Yii::t('CfilesModule.base', 'This file is public.')
: $privateText;
}
return $this->content->isPublic()
? Yii::t('CfilesModule.base', 'This file is public.')
: Yii::t('CfilesModule.base', 'This file is private.');
}
/**
* @inheritdoc
*/
public function getItemId()
{
return 'file_' . $this->id;
}
/**
* @inheritdoc
*/
public function getContentId()
{
return $this->content->id;
}
/**
* @return string
*/
public function getItemType()
{
return FileUtils::getItemTypeByExt(FileHelper::getExtension($this->baseFile));
}
/**
* @inheritdoc
*/
public function getTitle()
{
// needs to be checked cause used with uninitialized basefile by search index
if (!empty($this->baseFile)) {
return $this->baseFile->file_name;
} else {
return '';
}
}
/**
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* @return int
*/
public function getDownloadCount()
{
return $this->download_count;
}
public function setTitle($title)
{
if (!empty($this->baseFile)) {
$this->baseFile->file_name = $title;
}
}
/**
* @inheritdoc
*/
public function getSize()
{
return $this->baseFile->size;
}
/**
* Returns the URL to the folder where this file is located
* @inheritdoc
*/
public function getUrl(bool $scheme = false)
{
if ($this->parentFolder === null) {
Yii::warning('Could not get parent folder for file id: ' . $this->id, 'cfiles');
return '';
}
return $this->parentFolder->getUrl($scheme);
}
/**
* @inheritdoc
*/
public function getEditUrl()
{
return $this->content->container->createUrl('/cfiles/edit/file', ['id' => $this->getItemId()]);
}
/**
* Get the post related to the given file file.
*/
public static function getBasePost(BaseFile $file = null)
{
if ($file === null) {
return null;
}
$searchItem = $file;
// if the item is connected to a Comment, we have to search for the corresponding Post
if ($file->object_model === Comment::class) {
$searchItem = Comment::findOne($file->object_id);
}
return Content::find()->where([
'content.object_id' => $searchItem->object_id,
'content.object_model' => $searchItem->object_model,
])->one();
}
public function getBaseFile()
{
return $this->hasOne(BaseFile::class, ['object_id' => 'id'])
->andWhere(['file.object_model' => self::class]);
}
public static function getPathFromId($id, $parentFolderPath = false, $separator = '/', $withRoot = false)
{
if ($id == 0) {
return $separator;
}
$item = File::findOne(['id' => $id]);
if (empty($item)) {
return null;
}
$tempFolder = $item->parentFolder;
$path = $separator;
if (!$parentFolderPath) {
$path .= $item->title;
}
$counter = 0;
// break at maxdepth 20 to avoid hangs
while (!empty($tempFolder) && $counter++ <= 20) {
$path = $separator . $tempFolder->title . $path;
$tempFolder = $tempFolder->parentFolder;
}
return $path;
}
public function getFullPath($separator = '/')
{
return $this->getPathFromId($this->id, false, $separator);
}
/**
* Returns a query for all posted files visible for the current user.
*
* @param $contentContainer ContentContainerActiveRecord
* @param array $filesOrder orderBy array appended to the files query
* @return ActiveQuery
*/
public static function getPostedFiles($contentContainer, $filesOrder = ['file.updated_at' => SORT_ASC, 'file.title' => SORT_ASC])
{
// only accept Posts as the base content, so stuff from sumbmodules like files itsself or gallery will be excluded
// Initialise sub queries to get files from Posts and Comments
$subQueries = [
Post::class => Content::find()
->select('content.object_id')
->where(['content.object_model' => Post::class]),
Comment::class => Content::find()
->select('comment.id')
->innerJoin('comment', 'comment.object_model = content.object_model AND comment.object_id = content.object_id')
->where(['comment.object_model' => Post::class]),
];
$query = BaseFile::find();
foreach ($subQueries as $objectClass => $subQuery) {
// Filter Content records by container and visibility states
$subQuery->andWhere(['content.contentcontainer_id' => $contentContainer->contentContainerRecord->id])
->andWhere(['content.state' => Content::STATE_PUBLISHED]);
if (!$contentContainer->canAccessPrivateContent()) {
// Note this will cut comment images, but including the visibility of comments is pretty complex...
$subQuery->andWhere(['content.visibility' => Content::VISIBILITY_PUBLIC]);
}
$query->orWhere([
'AND',
['file.object_model' => $objectClass],
['IN', 'file.object_id', $subQuery],
]);
}
return $query->orderBy($filesOrder);
}
/**
* @return file of given name in given parent folder
*/
public static function getFileByName($name, $parentFolderId, $contentContainer)
{
$filesQuery = self::find()->contentContainer($contentContainer)
->joinWith('baseFile')
->readable()
->andWhere([
'file_name' => $name,
'cfiles_file.parent_folder_id' => $parentFolderId,
]);
return $filesQuery->one();
}
/**
* Get File by guid
*
* @param $guid
* @return array|File|\yii\db\ActiveRecord
*/
public static function getFileByGuid($guid)
{
return self::find()
->innerJoin('file', 'object_id = ' . self::tableName() . '.id')
->where(['guid' => $guid])
->andWhere(['object_model' => self::class])
->one();
}
/**
* @inheritdoc
*/
public function getVersionsUrl(int $versionId = 0): ?string
{
if (!$this->content->canEdit()) {
return null;
}
$options = ['id' => $this->id];
if (!empty($versionId)) {
$options['version'] = $versionId;
}
return $this->content->container->createUrl('/cfiles/version', $options);
}
/**
* @inheritdoc
*/
public function getDeleteVersionUrl(int $versionId): ?string
{
if (!$this->canManage()) {
return null;
}
return $this->content->container->createUrl('/cfiles/version/delete', [
'id' => $this->id,
'version' => $versionId,
]);
}
/**
* @inheritdoc
*/
public function renameConflicted(): bool
{
if ($this->isNewRecord) {
return false;
}
$this->baseFile->file_name = 'conflict' . $this->baseFile->id . '-' . $this->baseFile->file_name;
return $this->baseFile->save();
}
}