Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
ImageUploader Class Reference

Public Member Functions

 __construct (\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase, \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Psr\Log\LoggerInterface $logger, $baseTmpPath, $basePath, $allowedExtensions)
 
 setBaseTmpPath ($baseTmpPath)
 
 setBasePath ($basePath)
 
 setAllowedExtensions ($allowedExtensions)
 
 getBaseTmpPath ()
 
 getBasePath ()
 
 getAllowedExtensions ()
 
 getFilePath ($path, $imageName)
 
 moveFileFromTmp ($imageName)
 

Protected Attributes

 $coreFileStorageDatabase
 
 $mediaDirectory
 
 $storeManager
 
 $logger
 
 $baseTmpPath
 
 $basePath
 
 $allowedExtensions
 

Detailed Description

Catalog image uploader

Definition at line 11 of file ImageUploader.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\MediaStorage\Helper\File\Storage\Database  $coreFileStorageDatabase,
\Magento\Framework\Filesystem  $filesystem,
\Magento\MediaStorage\Model\File\UploaderFactory  $uploaderFactory,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Psr\Log\LoggerInterface  $logger,
  $baseTmpPath,
  $basePath,
  $allowedExtensions 
)

ImageUploader constructor

Parameters
\Magento\MediaStorage\Helper\File\Storage\Database$coreFileStorageDatabase
\Magento\Framework\Filesystem$filesystem
\Magento\MediaStorage\Model\File\UploaderFactory$uploaderFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
\Psr\Log\LoggerInterface$logger
string$baseTmpPath
string$basePath
string[]$allowedExtensions

Definition at line 91 of file ImageUploader.php.

100  {
101  $this->coreFileStorageDatabase = $coreFileStorageDatabase;
102  $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
103  $this->uploaderFactory = $uploaderFactory;
104  $this->storeManager = $storeManager;
105  $this->logger = $logger;
106  $this->baseTmpPath = $baseTmpPath;
107  $this->basePath = $basePath;
108  $this->allowedExtensions = $allowedExtensions;
109  }
$filesystem

Member Function Documentation

◆ getAllowedExtensions()

getAllowedExtensions ( )

Retrieve base path

Returns
string[]

Definition at line 172 of file ImageUploader.php.

◆ getBasePath()

getBasePath ( )

Retrieve base path

Returns
string

Definition at line 162 of file ImageUploader.php.

◆ getBaseTmpPath()

getBaseTmpPath ( )

Retrieve base tmp path

Returns
string

Definition at line 152 of file ImageUploader.php.

◆ getFilePath()

getFilePath (   $path,
  $imageName 
)

Retrieve path

Parameters
string$path
string$imageName
Returns
string

Definition at line 185 of file ImageUploader.php.

186  {
187  return rtrim($path, '/') . '/' . ltrim($imageName, '/');
188  }

◆ moveFileFromTmp()

moveFileFromTmp (   $imageName)

Checking file for moving and move it

Parameters
string$imageName
Returns
string
Exceptions

Definition at line 199 of file ImageUploader.php.

200  {
201  $baseTmpPath = $this->getBaseTmpPath();
202  $basePath = $this->getBasePath();
203 
204  $baseImagePath = $this->getFilePath($basePath, $imageName);
205  $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
206 
207  try {
208  $this->coreFileStorageDatabase->copyFile(
209  $baseTmpImagePath,
210  $baseImagePath
211  );
212  $this->mediaDirectory->renameFile(
213  $baseTmpImagePath,
214  $baseImagePath
215  );
216  } catch (\Exception $e) {
217  throw new \Magento\Framework\Exception\LocalizedException(
218  __('Something went wrong while saving the file(s).')
219  );
220  }
221 
222  return $imageName;
223  }
__()
Definition: __.php:13

◆ setAllowedExtensions()

setAllowedExtensions (   $allowedExtensions)

Set allowed extensions

Parameters
string[]$allowedExtensions
Returns
void

Definition at line 142 of file ImageUploader.php.

143  {
144  $this->allowedExtensions = $allowedExtensions;
145  }

◆ setBasePath()

setBasePath (   $basePath)

Set base path

Parameters
string$basePath
Returns
void

Definition at line 130 of file ImageUploader.php.

131  {
132  $this->basePath = $basePath;
133  }

◆ setBaseTmpPath()

setBaseTmpPath (   $baseTmpPath)

Set base tmp path

Parameters
string$baseTmpPath
Returns
void

Definition at line 118 of file ImageUploader.php.

119  {
120  $this->baseTmpPath = $baseTmpPath;
121  }

Field Documentation

◆ $allowedExtensions

$allowedExtensions
protected

Definition at line 65 of file ImageUploader.php.

◆ $basePath

$basePath
protected

Definition at line 58 of file ImageUploader.php.

◆ $baseTmpPath

$baseTmpPath
protected

Definition at line 51 of file ImageUploader.php.

◆ $coreFileStorageDatabase

$coreFileStorageDatabase
protected

Definition at line 18 of file ImageUploader.php.

◆ $logger

$logger
protected

Definition at line 44 of file ImageUploader.php.

◆ $mediaDirectory

$mediaDirectory
protected

Definition at line 25 of file ImageUploader.php.

◆ $storeManager

$storeManager
protected

Definition at line 39 of file ImageUploader.php.


The documentation for this class was generated from the following file: