Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit c38cf591 authored by Nicolas BEJEAN's avatar Nicolas BEJEAN
Browse files

Création de nouvelles clases PdfImageUpload et VideoImageUpload

parent 9667e2ca
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ namespace NicolasBejean\MediaManager\Controller\Adminhtml\Pdf; ...@@ -4,7 +4,7 @@ namespace NicolasBejean\MediaManager\Controller\Adminhtml\Pdf;
use Magento\Backend\App\Action; use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context; use Magento\Backend\App\Action\Context;
use NicolasBejean\MediaManager\Model\ImageUploader; use NicolasBejean\MediaManager\Model\PdfImageUploader;
use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultFactory;
/** /**
...@@ -15,22 +15,22 @@ class PdfImageUpload extends Action ...@@ -15,22 +15,22 @@ class PdfImageUpload extends Action
/** /**
* Image uploader * Image uploader
* *
* @var \NicolasBejean\MediaManager\Model\ImageUploader * @var \NicolasBejean\MediaManager\Model\PdfImageUploader
*/ */
protected $imageUploader; protected $pdfImageUploader;
/** /**
* Upload constructor. * Upload constructor.
* *
* @param \Magento\Backend\App\Action\Context $context * @param \Magento\Backend\App\Action\Context $context
* @param \NicolasBejean\MediaManager\Model\ImageUploader $imageUploader * @param \NicolasBejean\MediaManager\Model\PdfImageUploader $pdfImageUploader
*/ */
public function __construct( public function __construct(
Context $context, Context $context,
ImageUploader $imageUploader PdfImageUploader $pdfImageUploader
) { ) {
parent::__construct($context); parent::__construct($context);
$this->imageUploader = $imageUploader; $this->pdfImageUploader = $pdfImageUploader;
} }
/** /**
...@@ -53,7 +53,7 @@ class PdfImageUpload extends Action ...@@ -53,7 +53,7 @@ class PdfImageUpload extends Action
$imageId = $this->_request->getParam('param_name', 'image'); $imageId = $this->_request->getParam('param_name', 'image');
try { try {
$result = $this->imageUploader->saveFileToTmpDir($imageId); $result = $this->pdfImageUploader->saveFileToTmpDir($imageId);
} catch (\Exception $e) { } catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
} }
......
...@@ -4,7 +4,7 @@ namespace NicolasBejean\MediaManager\Controller\Adminhtml\Video; ...@@ -4,7 +4,7 @@ namespace NicolasBejean\MediaManager\Controller\Adminhtml\Video;
use Magento\Backend\App\Action; use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context; use Magento\Backend\App\Action\Context;
use NicolasBejean\MediaManager\Model\ImageUploader; use NicolasBejean\MediaManager\Model\VideoImageUploader;
use Magento\Framework\Controller\ResultFactory; use Magento\Framework\Controller\ResultFactory;
/** /**
...@@ -15,22 +15,21 @@ class VideoImageUpload extends Action ...@@ -15,22 +15,21 @@ class VideoImageUpload extends Action
/** /**
* Image uploader * Image uploader
* *
* @var \NicolasBejean\MediaManager\Model\ImageUploader * @var \NicolasBejean\MediaManager\Model\VideoImageUploader
*/ */
protected $imageUploader; protected $videoImageUploader;
/** /**
* Upload constructor. * VideoImageUpload constructor.
* * @param Context $context
* @param \Magento\Backend\App\Action\Context $context * @param VideoImageUploader $videoImageUploader
* @param \NicolasBejean\MediaManager\Model\ImageUploader $imageUploader
*/ */
public function __construct( public function __construct(
Context $context, Context $context,
ImageUploader $imageUploader VideoImageUploader $videoImageUploader
) { ) {
parent::__construct($context); parent::__construct($context);
$this->imageUploader = $imageUploader; $this->VideoImageUploader = $videoImageUploader;
} }
/** /**
...@@ -44,16 +43,14 @@ class VideoImageUpload extends Action ...@@ -44,16 +43,14 @@ class VideoImageUpload extends Action
} }
/** /**
* Upload file controller action * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
*
* @return \Magento\Framework\Controller\ResultInterface
*/ */
public function execute() public function execute()
{ {
$imageId = $this->_request->getParam('param_name', 'image'); $imageId = $this->_request->getParam('param_name', 'image');
try { try {
$result = $this->imageUploader->saveFileToTmpDir($imageId); $result = $this->videoImageUploader->saveFileToTmpDir($imageId);
} catch (\Exception $e) { } catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
} }
......
<?php
namespace NicolasBejean\MediaManager\Model;
/**
* MediaManager PDF image uploader
*/
class PdfImageUploader
{
/**
* Core file storage database
*
* @var \Magento\MediaStorage\Helper\File\Storage\Database
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
/**
* List of allowed image mime types
*
* @var array
*/
private $allowedMimeTypes = [
'image/jpg',
'image/jpeg',
'image/gif',
'image/png',
];
/**
* ImageUploader constructor
*
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Psr\Log\LoggerInterface $logger
* @param string $baseTmpPath
* @param string $basePath
* @param string[] $allowedExtensions
*/
public function __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,
$allowedMimeTypes = []
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
$this->allowedMimeTypes = $allowedMimeTypes;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string[] $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve allowed extensions
*
* @return string[]
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for save and save it to tmp dir
*
* @param string $fileId
*
* @return string[]
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
}
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
unset($result['path']);
if (!$result) {
throw new \Magento\Framework\Exception\LocalizedException(
__('File can not be saved to the destination folder.')
);
}
/**
* Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
*/
$result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new \Magento\Framework\Exception\LocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
<?php
namespace NicolasBejean\MediaManager\Model;
/**
* MediaManager Video image uploader
*/
class VideoImageUploader
{
/**
* Core file storage database
*
* @var \Magento\MediaStorage\Helper\File\Storage\Database
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
/**
* List of allowed image mime types
*
* @var array
*/
private $allowedMimeTypes = [
'image/jpg',
'image/jpeg',
'image/gif',
'image/png',
];
/**
* ImageUploader constructor
*
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Psr\Log\LoggerInterface $logger
* @param string $baseTmpPath
* @param string $basePath
* @param string[] $allowedExtensions
*/
public function __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,
$allowedMimeTypes = []
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
$this->allowedMimeTypes = $allowedMimeTypes;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string[] $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve allowed extensions
*
* @return string[]
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for save and save it to tmp dir
*
* @param string $fileId
*
* @return string[]
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
}
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
unset($result['path']);
if (!$result) {
throw new \Magento\Framework\Exception\LocalizedException(
__('File can not be saved to the destination folder.')
);
}
/**
* Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
*/
$result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new \Magento\Framework\Exception\LocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
...@@ -193,7 +193,7 @@ ...@@ -193,7 +193,7 @@
<argument name="collectionProcessor" xsi:type="object">NicolasBejean\MediaManager\Model\Api\SearchCriteria\PdfCollectionProcessor</argument> <argument name="collectionProcessor" xsi:type="object">NicolasBejean\MediaManager\Model\Api\SearchCriteria\PdfCollectionProcessor</argument>
</arguments> </arguments>
</type> </type>
<type name="NicolasBejean\MediaManager\Model\ImageUploader"> <type name="NicolasBejean\MediaManager\Model\PdfImageUploader">
<arguments> <arguments>
<argument name="baseTmpPath" xsi:type="string">mediamanager/pdf/image</argument> <argument name="baseTmpPath" xsi:type="string">mediamanager/pdf/image</argument>
<argument name="basePath" xsi:type="string">mediamanager/pdf/image</argument> <argument name="basePath" xsi:type="string">mediamanager/pdf/image</argument>
...@@ -309,7 +309,7 @@ ...@@ -309,7 +309,7 @@
<argument name="collectionProcessor" xsi:type="object">NicolasBejean\MediaManager\Model\Api\SearchCriteria\VideoCollectionProcessor</argument> <argument name="collectionProcessor" xsi:type="object">NicolasBejean\MediaManager\Model\Api\SearchCriteria\VideoCollectionProcessor</argument>
</arguments> </arguments>
</type> </type>
<type name="NicolasBejean\MediaManager\Model\ImageUploader"> <type name="NicolasBejean\MediaManager\Model\VideoImageUploader">
<arguments> <arguments>
<argument name="baseTmpPath" xsi:type="string">mediamanager/video/image</argument> <argument name="baseTmpPath" xsi:type="string">mediamanager/video/image</argument>
<argument name="basePath" xsi:type="string">mediamanager/video/image</argument> <argument name="basePath" xsi:type="string">mediamanager/video/image</argument>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment