43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Catalog\Model\MixStepPricing;
|
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Filesystem;
|
|
use Magento\MediaStorage\Model\File\UploaderFactory;
|
|
|
|
class FileUploader
|
|
{
|
|
private const TARGET_DIRECTORY = 'esun_mix_step_pricing';
|
|
|
|
public function __construct(
|
|
private readonly UploaderFactory $uploaderFactory,
|
|
private readonly Filesystem $filesystem
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @throws LocalizedException
|
|
*/
|
|
public function upload(string $fileId): string
|
|
{
|
|
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
|
|
$directory->create(self::TARGET_DIRECTORY);
|
|
|
|
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
|
|
$uploader->setAllowedExtensions(['xlsx', 'xls']);
|
|
$uploader->setAllowRenameFiles(true);
|
|
$uploader->setFilesDispersion(false);
|
|
$result = $uploader->save($directory->getAbsolutePath(self::TARGET_DIRECTORY));
|
|
if (!$result || empty($result['path']) || empty($result['file'])) {
|
|
throw new LocalizedException(__('The Excel file could not be uploaded.'));
|
|
}
|
|
|
|
return rtrim((string)$result['path'], DIRECTORY_SEPARATOR)
|
|
. DIRECTORY_SEPARATOR
|
|
. ltrim((string)$result['file'], DIRECTORY_SEPARATOR);
|
|
}
|
|
}
|