1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\saver; |
5
|
|
|
|
6
|
|
|
use League\Flysystem\FilesystemInterface; |
7
|
|
|
use tkanstantsin\fileupload\config\Alias; |
8
|
|
|
use tkanstantsin\fileupload\FileManager; |
9
|
|
|
use tkanstantsin\fileupload\model\Type; |
10
|
|
|
use tkanstantsin\yii2fileupload\model\File; |
|
|
|
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
|
|
|
12
|
|
|
use yii\web\UploadedFile; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FileUpload |
16
|
|
|
* |
17
|
|
|
* @todo: create Yii2 independent Uploader and/or split existed uploader. |
18
|
|
|
*/ |
19
|
|
|
class Uploader extends File |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var FileManager |
23
|
|
|
*/ |
24
|
|
|
public $fileManager; |
25
|
|
|
/** |
26
|
|
|
* @var UploadedFile |
27
|
|
|
*/ |
28
|
|
|
public $uploadedFile; |
29
|
|
|
/** |
30
|
|
|
* @var Alias |
31
|
|
|
*/ |
32
|
|
|
private $aliasConfig; |
33
|
|
|
/** |
34
|
|
|
* @var FilesystemInterface |
35
|
|
|
*/ |
36
|
|
|
private $filesystem; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* FileUpload constructor. |
40
|
|
|
* @param FileManager $fileManager |
41
|
|
|
* @param UploadedFile $uploadedFile |
42
|
|
|
* @param array $aliasConfig |
43
|
|
|
* @param FilesystemInterface $filesystem |
44
|
|
|
* @param array $config |
45
|
|
|
*/ |
46
|
|
|
public function __construct(FileManager $fileManager, UploadedFile $uploadedFile, Alias $aliasConfig, FilesystemInterface $filesystem, array $config = []) |
47
|
|
|
{ |
48
|
|
|
$this->fileManager = $fileManager; |
49
|
|
|
$this->uploadedFile = $uploadedFile; |
50
|
|
|
$this->aliasConfig = $aliasConfig; |
51
|
|
|
$this->filesystem = $filesystem; |
52
|
|
|
|
53
|
|
|
parent::__construct($config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function init(): void |
57
|
|
|
{ |
58
|
|
|
parent::init(); |
59
|
|
|
|
60
|
|
|
$this->parent_model = $this->aliasConfig->name; |
|
|
|
|
61
|
|
|
$this->name = pathinfo($this->uploadedFile->name, PATHINFO_FILENAME); |
|
|
|
|
62
|
|
|
$this->extension = $this->uploadedFile->extension; |
|
|
|
|
63
|
|
|
$this->size = (int) $this->uploadedFile->size; |
|
|
|
|
64
|
|
|
$this->mime_type = $this->uploadedFile->type; |
|
|
|
|
65
|
|
|
$this->type_id = Type::getByMimeType((string) $this->mime_type); |
|
|
|
|
66
|
|
|
$this->hash = \is_resource($this->uploadedFile->tempName) |
|
|
|
|
67
|
|
|
? hash('md5', stream_get_contents($this->uploadedFile->tempName)) |
68
|
|
|
: hash_file('md5', $this->uploadedFile->tempName); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function rules(): array |
75
|
|
|
{ |
76
|
|
|
return ArrayHelper::merge(parent::rules(), [ |
77
|
|
|
[['uploadedFile', 'parent_model'], 'required'], |
78
|
|
|
[['uploadedFile'], 'file', 'maxSize' => $this->aliasConfig->maxSize], |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Saves file |
84
|
|
|
* @return bool |
85
|
|
|
* @throws \League\Flysystem\FileExistsException |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
*/ |
88
|
|
|
public function upload(): bool |
89
|
|
|
{ |
90
|
|
|
$saved = $this->save(); |
91
|
|
|
|
92
|
|
|
// ignore saving error |
93
|
|
|
$this->uploadFile($this->aliasConfig->getFileDirectory($this), $this->aliasConfig->getFileName($this)); |
94
|
|
|
|
95
|
|
|
return $saved; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Copy file into FileSaver::$filesystem |
100
|
|
|
* @param string $dirPath |
101
|
|
|
* @param string $fileName |
102
|
|
|
* @return bool |
103
|
|
|
* @throws \League\Flysystem\FileExistsException |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function uploadFile(string $dirPath, string $fileName): bool |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
if (!$this->filesystem->createDir($dirPath)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$targetFilePath = $dirPath . DIRECTORY_SEPARATOR . $fileName; |
114
|
|
|
|
115
|
|
|
if ($this->filesystem->has($targetFilePath)) { |
116
|
|
|
$this->addError('', "File by path `{$targetFilePath}` already exists."); |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$contentResource = $this->getFileContent(); |
|
|
|
|
122
|
|
|
if ($contentResource === null) { |
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->filesystem->writeStream($targetFilePath, $contentResource); |
127
|
|
|
} catch (\Exception $e) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return resource|null |
134
|
|
|
*/ |
135
|
|
|
private function getFileContent() |
136
|
|
|
{ |
137
|
|
|
$resource = \is_resource($this->uploadedFile->tempName) |
138
|
|
|
? $this->uploadedFile->tempName |
139
|
|
|
: fopen($this->uploadedFile->tempName, 'rb'); |
140
|
|
|
|
141
|
|
|
return $resource ?: null; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
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