t-kanstantsin /
fileupload
| 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; |
||
|
0 ignored issues
–
show
|
|||
| 11 | use yii\helpers\ArrayHelper; |
||
|
0 ignored issues
–
show
The type
yii\helpers\ArrayHelper was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 12 | use yii\web\UploadedFile; |
||
|
0 ignored issues
–
show
The type
yii\web\UploadedFile was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 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; |
||
|
0 ignored issues
–
show
|
|||
| 61 | $this->name = pathinfo($this->uploadedFile->name, PATHINFO_FILENAME); |
||
|
0 ignored issues
–
show
|
|||
| 62 | $this->extension = $this->uploadedFile->extension; |
||
|
0 ignored issues
–
show
|
|||
| 63 | $this->size = (int) $this->uploadedFile->size; |
||
|
0 ignored issues
–
show
|
|||
| 64 | $this->mime_type = $this->uploadedFile->type; |
||
|
0 ignored issues
–
show
|
|||
| 65 | $this->type_id = Type::getByMimeType((string) $this->mime_type); |
||
|
0 ignored issues
–
show
|
|||
| 66 | $this->hash = \is_resource($this->uploadedFile->tempName) |
||
|
0 ignored issues
–
show
|
|||
| 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(); |
||
|
0 ignored issues
–
show
Are you sure the assignment to
$contentResource is correct as $this->getFileContent() targeting tkanstantsin\fileupload\...oader::getFileContent() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 122 | if ($contentResource === null) { |
||
|
0 ignored issues
–
show
|
|||
| 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; |
||
|
0 ignored issues
–
show
|
|||
| 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