Commit 42e7eb84 authored by Reinis Mazeiks's avatar Reinis Mazeiks
Browse files

Refactor and fix URL generation for 3D models

parent 4dc7fba7
<?php declare(strict_types=1);
namespace Scandi\Product3DGraphQl\Helper;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\UrlInterface;
class ModelUrls
{
const UPLOAD_DIRECTORY = 'scandi/product_3d_models/';
/** @var Filesystem */
protected $fileSystem;
/** @var UrlInterface */
private $url;
/**
* ModelUrls constructor.
* @param Filesystem $fileSystem
* @param UrlInterface $url
*/
public function __construct(Filesystem $fileSystem, UrlInterface $url)
{
$this->fileSystem = $fileSystem;
$this->url = $url;
}
public function getUploadPath()
{
return $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)
->getAbsolutePath(self::UPLOAD_DIRECTORY);
}
public function getUrlForModelFile(string $file)
{
return sprintf(
"%s%s",
$this->getBaseUrl(),
ltrim($file, '/')
);
}
public function getBaseUrl()
{
return sprintf(
'%s/%s',
rtrim($this->url->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]), '/'),
self::UPLOAD_DIRECTORY
);
}
}
......@@ -7,8 +7,8 @@ use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;
use Scandi\Product3DGraphQl\Api\Data\Model3DInterface;
use Scandi\Product3DGraphQl\Helper\ModelUrls;
class Model3D extends AbstractModel implements Model3DInterface
{
......@@ -16,21 +16,21 @@ class Model3D extends AbstractModel implements Model3DInterface
const URL = 'url';
const FILE = 'file';
/** @var StoreManagerInterface */
protected $storeManager;
/** @var ModelUrls */
protected $modelUrls;
/**
* Model3D constructor.
* @param Context $context
* @param Registry $registry
* @param StoreManagerInterface $storeManager
* @param ModelUrls $modelUrls
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
*/
public function __construct(
Context $context,
Registry $registry,
StoreManagerInterface $storeManager,
ModelUrls $modelUrls,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null
) {
......@@ -40,7 +40,7 @@ class Model3D extends AbstractModel implements Model3DInterface
$resource,
$resourceCollection
);
$this->storeManager = $storeManager;
$this->modelUrls = $modelUrls;
}
/**
......@@ -97,7 +97,7 @@ class Model3D extends AbstractModel implements Model3DInterface
public function getResourceUrl(): string
{
return $this->storeManager->getStore()->getBaseUrl() . $this->getUrl();
return $this->modelUrls->getUrlForModelFile($this->getUrl());
}
/**
......
......@@ -4,9 +4,8 @@ namespace Scandi\Product3DGraphQl\Model;
use Exception;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;
use Scandi\Product3DGraphQl\Helper\ModelUrls;
class Upload
{
......@@ -17,11 +16,8 @@ class Upload
/** @var ScopeConfigInterface */
protected $scopeConfig;
/** @var Filesystem */
protected $fileSystem;
/** @var string */
protected $uploadFolder;
/** @var ModelUrls */
protected $modelUrls;
/** @var string[] */
protected $allowedExtensions;
......@@ -30,21 +26,18 @@ class Upload
* Upload constructor.
* @param UploaderFactory $uploaderFactory
* @param ScopeConfigInterface $scopeConfig
* @param Filesystem $fileSystem
* @param string $uploadFolder
* @param ModelUrls $modelUrls
* @param string[] $allowedExtensions
*/
public function __construct(
UploaderFactory $uploaderFactory,
ScopeConfigInterface $scopeConfig,
Filesystem $fileSystem,
string $uploadFolder = 'scandi/product_3d_models/',
ModelUrls $modelUrls,
array $allowedExtensions = ['glb']
) {
$this->uploaderFactory = $uploaderFactory;
$this->scopeConfig = $scopeConfig;
$this->fileSystem = $fileSystem;
$this->uploadFolder = $uploadFolder;
$this->modelUrls = $modelUrls;
$this->allowedExtensions = $allowedExtensions;
}
......@@ -62,12 +55,6 @@ class Upload
->setFilesDispersion(true)
->setAllowCreateFolders(true);
return $uploader->save($this->getUploadPath());
}
protected function getUploadPath()
{
return $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)
->getAbsolutePath($this->uploadFolder);
return $uploader->save($this->modelUrls->getUploadPath());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment