Skip to content

Commit

Permalink
Merge pull request #29 from koongo-com/feature/SC-2216-file-driver
Browse files Browse the repository at this point in the history
Feature/sc 2216 file driver
  • Loading branch information
pecinaon authored Dec 11, 2024
2 parents 7914a7d + 71195bb commit 40791e1
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 96 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koongo-com/magento2-data-feed-manager",
"type": "magento2-module",
"version": "2.4.8.17",
"version": "2.4.9",
"license": "CC-BY-4.0",
"description": "Koongo is an ultimate product data feed management tool that streamlines the process of product data export from Magento 2 store to any of 500+ price comparison websites, online marketplaces, and affiliate networks worldwide. Koongo helps you upload your product data to selling channels like Google Shopping, Shop.com, Facebook, Rakuten, Twenga, Bol.com, Beslist.nl, Bing Ads, and more.",
"require": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function execute()
$path = $profile->getFilename(true); //Get full file path
$fileName = $profile->getFilename();

$content = file_get_contents($path);
$content = $this->version->fileGetContents($path);

$this->getResponse()
->setHttpResponseCode(200)
Expand Down
99 changes: 80 additions & 19 deletions src/Nostress/Koongo/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected $_appState;

protected \Magento\Framework\Filesystem\DriverInterface $driver;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Catalog\Model\Product\Type $productType
Expand Down Expand Up @@ -209,7 +211,8 @@ public function __construct(
\Magento\Framework\App\CacheInterface $cache,
\Magento\Framework\Module\ModuleList $moduleList,
\Magento\Framework\App\ProductMetadataInterface $productMetadataInterface,
\Magento\Framework\App\State $appState
\Magento\Framework\App\State $appState,
\Magento\Framework\Filesystem\DriverInterface $driver
) {
$this->_coreConfig = $coreConfig;
$this->_cacheTypeList = $cacheTypeList;
Expand All @@ -226,6 +229,7 @@ public function __construct(
$this->moduleList = $moduleList;
$this->_productMetadataInterface = $productMetadataInterface;
$this->_appState = $appState;
$this->driver = $driver;

parent::__construct($context);
}
Expand Down Expand Up @@ -344,7 +348,7 @@ public function getMediaBaseUrl($store = null)
// if ($this->_appState->getMode() == State::MODE_PRODUCTION) //Remove pub folder
// {
// //If production mode is enabled and it Document root target to pub directory
// $pubDirExist = file_exists(DirectoryList::PUB);
// $pubDirExist = $this->fileExists(DirectoryList::PUB);
// if(!$pubDirExist)
// {
// $url = str_replace("/".DirectoryList::PUB."/", "/", $url);
Expand Down Expand Up @@ -937,26 +941,83 @@ public function formatDatetime($datetime = "", $format = \DateTime::ATOM)

//*********************** FILE FUNCTIONS******************************

/**
* @param string $filename
* @return false|string[]
*/
public function file(string $filename)
{
return explode("\n", $this->driver->fileGetContents($filename));
}

/**
* @param string $path
* @param string|null $flag
* @param resource|null $context
* @return string
* @throws FileSystemException
*/
public function fileGetContents($path, $flag = null, $context = null)
{
return $this->driver->fileGetContents($path, $flag, $context);
}

/**
* @param string $filename
* @param string $mode
* @return resource|false
*/
public function fileOpen($filename, $mode)
{
return $this->driver->fileOpen($filename, $mode);
}

/**
* Tells whether the filename is a regular file
*
* @param string $filename
* @return bool
* @throws FileSystemException
*/
public function fileExists($filename)
{
return $this->driver->isFile($filename);
}

public function isFile($filename): bool
{
return $this->driver->isFile($filename);
}

/**
* @param resource $stream
* @return bool
*/
public function endOfFile($stream): bool
{
return $this->driver->endOfFile($stream);
}

public function createDirectory($dir)
{
if (!is_dir($dir)) {
mkdir($dir, self::DEF_FILE_PERMISSION, true);
if (!$this->driver->isDirectory($dir)) {
$this->driver->createDirectory($dir, self::DEF_FILE_PERMISSION);
}
}

public function createFile($file, $content)
{
if (!file_exists($file)) {
file_put_contents($file, $content);
chmod($file, self::DEF_FILE_PERMISSION);
if (!$this->fileExists($file)) {
$this->driver->filePutContents($file, $content);
$this->driver->changePermissions($file, self::DEF_FILE_PERMISSION);
}
}

/* creates a compressed zip file */
public function createZip($files = [], $destination = '', $overwrite = false)
{
//if the zip file already exists and overwrite is false, return false
if (file_exists($destination) && !$overwrite) {
if ($this->fileExists($destination) && !$overwrite) {
return false;
}
//vars
Expand All @@ -966,7 +1027,7 @@ public function createZip($files = [], $destination = '', $overwrite = false)
//cycle through each file
foreach ($files as $localName => $file) {
//make sure the file exists
if (file_exists($file)) {
if ($this->fileExists($file)) {
$valid_files[$localName] = $file;
}
}
Expand All @@ -978,7 +1039,7 @@ public function createZip($files = [], $destination = '', $overwrite = false)
$zip = new \ZipArchive();
$openZip = $zip->open($destination, $overwrite ? \ZIPARCHIVE::OVERWRITE : \ZIPARCHIVE::CREATE);
if ($openZip !== true) {
file_put_contents($destination, "");
$this->driver->filePutContents($destination, "");
$openZip = $zip->open($destination, \ZIPARCHIVE::OVERWRITE);
if ($openZip !== true) {
return false;
Expand All @@ -999,7 +1060,7 @@ public function createZip($files = [], $destination = '', $overwrite = false)
$zip->close();

//check to make sure the file exists
return file_exists($destination);
return $this->fileExists($destination);
} else {
return false;
}
Expand All @@ -1013,8 +1074,8 @@ public function createZip($files = [], $destination = '', $overwrite = false)
public function renameFile($originalFile, $newFile)
{
//rename file
if (is_file($originalFile)) {
rename($originalFile, $newFile);
if ($this->fileExists($originalFile)) {
$this->driver->rename($originalFile, $newFile);
}
}

Expand All @@ -1027,8 +1088,8 @@ public function deleteFile($file)
return;
}

if (file_exists($file)) {
unlink($file);
if ($this->fileExists($file)) {
$this->driver->deleteFile($file);
}
}

Expand Down Expand Up @@ -1081,15 +1142,15 @@ public function getFeedStorageDirPath($fileName = null, $subDirectory = null)

$reader = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$absolutePath = $reader->getAbsolutePath($path);
if (!is_dir($absolutePath)) {
mkdir($absolutePath);
if (!$this->driver->isDirectory($absolutePath)) {
$this->driver->createDirectory($absolutePath);
}

if (isset($subDirectory)) {
$path .= $subDirectory . self::PATH_DELIMITER;
$absolutePath = $reader->getAbsolutePath($path);
if (!is_dir($absolutePath)) {
mkdir($absolutePath);
if (!$this->driver->isDirectory($absolutePath)) {
$this->driver->createDirectory($absolutePath);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Nostress/Koongo/Helper/Data/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public function __construct(
\Magento\Integration\Model\OauthService $oauthService,
\Magento\Integration\Model\AuthorizationService $authorizationService,
\Magento\Framework\Acl\AclResource\ProviderInterface $resourceProvider,
\Magento\Integration\Model\Oauth\TokenFactory $tokenFactory
\Magento\Integration\Model\Oauth\TokenFactory $tokenFactory,
\Magento\Framework\Filesystem\DriverInterface $driver
) {
$this->_backendHelper = $backendHelper;
$this->_urlBuilder = $urlBuilder;
Expand All @@ -147,7 +148,8 @@ public function __construct(
$cache,
$moduleList,
$productMetadataInterface,
$appState
$appState,
$driver
);
}

Expand Down
65 changes: 40 additions & 25 deletions src/Nostress/Koongo/Helper/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ class Profile extends \Nostress\Koongo\Helper\Data
public function readXmlPreview($url, $encoding = 'utf-8', $max = 30000)
{

//if( !is_file( $url)) return false;
//if( !$this->driver->isFile( $url)) return false;

// read file by lines
$code = '';
$braked = false;
$fp = fopen($url, "r");
while (!feof($fp)) {
$result = fgets($fp, 10000);
$code .= $result;
// if max is reached, we must end xml with correct element
if (strlen($code) > $max) {
$braked = true;
break;

$resource = $this->driver->fileOpen($url, 'r');
if (!$resource) {
throw new \Exception(__("Can't open file $url for reading."));
}
try {
while (!$this->driver->endOfFile($resource)) {
$result = $this->driver->fileRead($resource, 10000);
$code .= $result;
// if max is reached, we must end xml with correct element
if (strlen($code) > $max) {
$braked = true;
break;
}
}
} finally {
$this->driver->fileClose($resource);
}
fclose($fp);

//XML file is already formated see "Format as pretty XML" in Nostress\Koongo\Model\Data\Transformation\Xslt
//$code = $this->formatXmlString($code);
Expand Down Expand Up @@ -94,7 +101,7 @@ public function formatXmlString($xml)
public function readCsvPreview($url, $columnSeparator = ';', $enclosure = '"', $encoding = 'utf-8', $max = 500)
{

//if( !is_file( $url)) return false;
//if( !$this->driver->isFile( $url)) return false;

if (empty($enclosure)) {
$enclosure = '"';
Expand All @@ -103,24 +110,32 @@ public function readCsvPreview($url, $columnSeparator = ';', $enclosure = '"', $
$row = 1;
$header = [];
$array = [];
if (($handle = fopen($url, "r")) !== false) {
while (($data = fgetcsv($handle, 10000, $columnSeparator, $enclosure)) !== false) {
if ($row > $max) {
break;
}
if ($row== 1) {
// overjump first row with comment if exist
if (count($data) == 1) {
continue;
if ($handle = $this->driver->fileOpen($url, 'r') !== false) {
try {
while (!$this->driver->endOfFile($handle)) {
$data = $this->driver->fileGetCsv($handle, 10000, $columnSeparator, $enclosure);
if ($data === false) {
break;
}

$header = $data;
} else {
$array[] = $data;
if ($row > $max) {
break;
}
if ($row== 1) {
// overjump first row with comment if exist
if (count($data) == 1) {
continue;
}

$header = $data;
} else {
$array[] = $data;
}
$row++;
}
$row++;
} finally {
$this->driver->fileClose($handle);
}
fclose($handle);
}

$html = '<table class="data-grid feed-preview-table"><thead><tr>';
Expand Down
7 changes: 7 additions & 0 deletions src/Nostress/Koongo/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ abstract class AbstractModel extends \Magento\Framework\Model\AbstractModel
*/
protected $translation;

/**
* @var \Magento\Framework\Filesystem\DriverInterface
*/
protected \Magento\Framework\Filesystem\DriverInterface $driver;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -172,11 +177,13 @@ public function __construct(
\Nostress\Koongo\Model\Translation $translation,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
\Magento\Framework\Filesystem\DriverInterface $driver,
array $data = []
) {
$this->helper = $helper;
$this->storeManager = $storeManager;
$this->translation = $translation;
$this->driver = $driver;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Nostress/Koongo/Model/Channel/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ public function __construct(
\Nostress\Koongo\Model\Taxonomy\Setup $taxonomySetup,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
\Magento\Framework\Filesystem\DriverInterface $driver,
array $data = []
) {
$this->channelFactory = $channelFactory;
$this->taxonomySetup = $taxonomySetup;

parent::__construct($context, $registry, $helper, $storeManager, $translation, $resource, $resourceCollection, $data);
parent::__construct($context, $registry, $helper, $storeManager, $translation, $resource, $resourceCollection, $driver, $data);
}

public function getFeedByCode($code = null)
Expand Down
Loading

0 comments on commit 40791e1

Please sign in to comment.