b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / a / home / dev2.destoffenstraat.com / app / Firebear / ImportExport / Model / Api / Import /
Filename/home/a/home/dev2.destoffenstraat.com/app/Firebear/ImportExport/Model/Api/Import/Upload.php
Size3.64 kb
Permissionrw-rw-r--
Ownerroot : root
Create time21-Aug-2025 12:26
Last modified07-Nov-2022 06:44
Last accessed23-Aug-2025 13:16
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
* @copyright: Copyright © 2020 Firebear Studio. All rights reserved.
* @author: Firebear Studio <fbeardev@gmail.com>
*/
namespace Firebear\ImportExport\Model\Api\Import;

use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaStorage\Model\File\UploaderFactory;
use Firebear\ImportExport\Api\Import\UploadInterface;

/**
* File upload command (Service Provider Interface - SPI)
*
* @api
*/
class Upload implements UploadInterface
{
/**
* Target directory
*/
const UPLOAD_DIRECTORY = 'importexport';

/**
* @var array
*/
public static $allowedExtensions = ['csv', 'xml', 'ods', 'xlsx', 'zip'];

/**
* @var \Magento\Framework\Filesystem
*/
protected $filesystem;

/**
* @var UploaderFactory
*/
protected $uploaderFactory;

/**
* Upload media path
*
* @var string|null
*/
private $uploadPath;

/**
* Initialize command
*
* @param Filesystem $filesystem
* @param UploaderFactory $uploaderFactory
*/
public function __construct(
Filesystem $filesystem,
UploaderFactory $uploaderFactory
) {
$this->filesystem = $filesystem;
$this->uploaderFactory = $uploaderFactory;
}

/**
* Upload file
*
* @param string $fileName
* @param string $uniqueName
* @return string
* @throws LocalizedException
*/
public function execute($fileName = '', $uniqueName = false)
{
$uploader = $this->uploaderFactory->create([
'fileId' => 'file'
]);
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$extension = strtolower($uploader->getFileExtension());

if (in_array($extension, self::$allowedExtensions)) {
if ($uniqueName) {
$fileName = date('Y-m-d-H-m-s');
}

$result = $uploader->save(
$mediaDirectory->getAbsolutePath($this->getAbsolutePath()),
$this->correctFileName($fileName, $extension)
);

$file = $result['path'] . $result['file'];
if ($extension == 'zip') {
$zipFile = $file;
$zip = new \ZipArchive();
$zip->open($zipFile);
$file = $zip->getNameIndex(0);
$zip->extractTo(dirname($zipFile), $file);
$zip->close();
unlink($zipFile);
}

return 'pub/media/' . $mediaDirectory->getRelativePath($file);
}
throw new LocalizedException(__('Unsupported file type'));
}

/**
* Set absolute path
*
* @param string $uploadPath
* @return void
*/
public function setAbsolutePath($uploadPath)
{
$this->uploadPath = $uploadPath;
}

/**
* Retrieve absolute path
*
* @return string
*/
private function getAbsolutePath()
{
return $this->uploadPath ?: self::UPLOAD_DIRECTORY;
}

/**
* Add extension to file name if not exist
*
* @param $name
* @param $extension
*
* @return string
*/
private function correctFileName($name, $extension)
{
if ($name) {
$newFileNameInfo = pathinfo($name);
if (!isset($newFileNameInfo['extension'])
|| $newFileNameInfo['extension'] != $extension
) {
$name .= '.' . $extension;
}
}
return $name;
}
}