|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\formatter\preview; |
|
5
|
|
|
|
|
6
|
|
|
use tkanstantsin\fileupload\model\IFile; |
|
7
|
|
|
use tkanstantsin\fileupload\model\Type; |
|
8
|
|
|
use yii\helpers\Url; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class PreviewCreator |
|
12
|
|
|
* @todo: refactor. |
|
13
|
|
|
*/ |
|
14
|
|
|
class Preview |
|
15
|
|
|
{ |
|
16
|
|
|
public const DEFAULT_PREVIEW = 'default'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $iconPreviewArray = [ |
|
22
|
|
|
'file' => ['txt', 'doc',], |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
public static $availableExtensionArray = [ |
|
29
|
|
|
'jpg', 'jpeg', 'png', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param IFile $file |
|
34
|
|
|
* @return mixed|string |
|
35
|
|
|
* @throws \yii\base\InvalidParamException |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function create(IFile $file) |
|
38
|
|
|
{ |
|
39
|
|
|
return static::isPreviewAvailable($file) |
|
40
|
|
|
// TODO: move into component. |
|
41
|
|
|
? Url::to([ |
|
42
|
|
|
'/file/get', |
|
43
|
|
|
'id' => $file->getId(), |
|
44
|
|
|
'updatedAt' => $file->getUpdatedAt(), |
|
45
|
|
|
'fileType' => Type::IMAGE, |
|
46
|
|
|
'fileName' => $file->getFullName(), |
|
47
|
|
|
]) |
|
48
|
|
|
: static::getIcon($file); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param IFile $file |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getIcon(IFile $file) |
|
56
|
|
|
{ |
|
57
|
|
|
// return icon |
|
58
|
|
|
foreach (static::$iconPreviewArray as $preview => $extensionArray) { |
|
59
|
|
|
if (\in_array($file->getExtension(), $extensionArray, true)) { |
|
60
|
|
|
return static::getPreviewIcon($preview); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return static::getPreviewIcon(self::DEFAULT_PREVIEW); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function getPreviewIcon($preview) |
|
68
|
|
|
{ |
|
69
|
|
|
return $preview; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param IFile $file |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
protected static function isPreviewAvailable(IFile $file): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return \in_array($file->getExtension(), static::$availableExtensionArray, true); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths