Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFTP DOES NOT SUPPORT. PLEASE SEE CODE BELOW TO MAKE SFTP TO WORK #7

Closed
nagrgk opened this issue Nov 17, 2016 · 2 comments
Closed

SFTP DOES NOT SUPPORT. PLEASE SEE CODE BELOW TO MAKE SFTP TO WORK #7

nagrgk opened this issue Nov 17, 2016 · 2 comments

Comments

@nagrgk
Copy link

nagrgk commented Nov 17, 2016

step1 : add new file to following location: Firebear\ImportExport\Model\Filesystem\Io => Sftp.php

_conn, $filename); } } **step2 :** add test case to Firebear\ImportExport\Test\Unit\Model\Source\Type => SftpTest.php objectManagerHelper = new ObjectManagerHelper($this); $this->_sftpClient = $this->getMockBuilder('Firebear\ImportExport\Model\Filesystem\Io\Sftp') ->setMethods( [ 'read' ] ) ->getMock(); $this->_sftpClient->expects($this->any()) ->method('read') ->willReturn(true); $this->varDirectory = $this->getMock( 'Magento\Framework\Filesystem\Directory\Write', ['getAbsolutePath'], [], '', false ); //$this->varDirectory->expects($this->any())->method('getAbsolutePath')->will($this->returnValue(true)); $this->filesystem = $this->getMock( 'Magento\Framework\Filesystem', ['getDirectoryWrite'], [], '', false ); $this->filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn($this->varDirectory); $this->factory = $this->getMock('Magento\Framework\Filesystem\File\ReadFactory', [], [], '', false); $this->scopeConfigInterface = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface'); $this->_sftpMock = $this->objectManagerHelper->getObject( 'Firebear\ImportExport\Model\Source\Type\Sftp', [ 'scopeConfig' => $this->scopeConfigInterface, 'filesystem' => $this->filesystem, 'readFactory' => $this->factory ] ); $this->_sftpMock->setClient($this->_sftpClient); // $this->_ftpMock = $this->getMockBuilder('Firebear\ImportExport\Model\Source\Type\Ftp') // ->setConstructorArgs( // [ // $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface') // ->disableOriginalConstructor() // ->getMock(), // $this->getMockBuilder('Magento\Framework\Filesystem') // ->disableOriginalConstructor() // ->getMock(), // $this->getMockBuilder('Magento\Framework\Filesystem\File\ReadFactory') // ->disableOriginalConstructor() // ->getMock(), // ] // ) // ->setMethods( // [ // 'uploadSource', // '_getSourceClient', // ] // ) // ->getMock(); // // $this->_ftpMock->expects($this->any()) // ->method('_getSourceClient') // ->willReturn($this->_ftpClient); $this->_sftpMock->setData('import_source', 'sftp'); $this->_sftpMock->setData('sftp_file_path', '/public_html/import/products_2_replace.csv'); } public function testUploadSource() { try { $result = $this->_sftpMock->uploadSource(); } catch (\Exception $e) { $result = $e->getMessage(); } $this->assertEquals('/var/www/local-mage2dev.com/magento2/var/import/sftp/products_2_replace.csv', $result); } } **step3:** add sftp source to Firebear\ImportExport\Model\Source\Type => Sftp.php */ namespace Firebear\ImportExport\Model\Source\Type; class Sftp extends AbstractType { /** * @var string */ protected $_code = 'sftp'; /** * Download remote source file to temporary directory * * @return string * @throws \Magento\Framework\Exception\LocalizedException */ public function uploadSource() { if($client = $this->_getSourceClient()) { $sourceFilePath = $this->getData($this->_code . '_file_path'); $fileName = basename($sourceFilePath); //return get_class($this->_directory); $filePath = $this->_directory->getAbsolutePath($this->getImportPath() . '/' . $fileName); $filesystem = new \Magento\Framework\Filesystem\Io\File(); $filesystem->setAllowCreateFolders(true); $filesystem->checkAndCreateFolder($this->_directory->getAbsolutePath($this->getImportPath())); $result = $client->read($sourceFilePath, $filePath); if($result) { return $this->_directory->getAbsolutePath($this->getImportPath() . '/' . $fileName); } else { throw new \Magento\Framework\Exception\LocalizedException(__("File not found")); } } else { throw new \Magento\Framework\Exception\LocalizedException(__("Can't initialize %s client", $this->_code)); } } /** * Download remote images to temporary media directory * * @param $importImage * @param $imageSting * * @throws \Magento\Framework\Exception\LocalizedException */ public function importImage($importImage, $imageSting) { if($client = $this->_getSourceClient()) { $sourceFilePath = $this->getData($this->_code . '_file_path'); $sourceDirName = dirname($sourceFilePath); $filePath = $this->_directory->getAbsolutePath($this->getMediaImportPath() . $imageSting); $dirname = dirname($filePath); if (!is_dir($dirname)) { mkdir($dirname, 0775, true); } if($filePath) { $result = $client->read($sourceDirName . '/' . $importImage, $filePath); } } } /** * Check if remote file was modified since the last import * * @param int $timestamp * @return bool|int */ public function checkModified($timestamp) { if($client = $this->_getSourceClient()) { $sourceFilePath = $this->getData($this->_code . '_file_path'); if(!$this->_metadata) { $this->_metadata['modified'] = $client->mdtm($sourceFilePath); } $modified = $this->_metadata['modified']; return ($timestamp != $this->_metadata['modified']) ? $modified : false; } return false; } /** * Prepare and return SFTP client * * @return \Firebear\ImportExport\Model\Filesystem\Io\Sftp * @throws \Magento\Framework\Exception\LocalizedException */ protected function _getSourceClient() { if(!$this->getClient()) { if($this->getData('host') && $this->getData('port') && $this->getData('user') && $this->getData('password')) { $settings = $this->getData(); } else { $settings = $this->_scopeConfig->getValue( 'firebear_importexport/sftp', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } $settings['passive'] = true; try { $connection = new \Firebear\ImportExport\Model\Filesystem\Io\Sftp(); $connection->open( $settings ); $this->_client = $connection; } catch(\Exception $e){ throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage())); } } return $this->getClient(); } } **step 4:** add source types to /Firebear/ImportExport/etc/ => source_types.xml

step5 : change config.xml :

22

step6: add sftp config settings: user should be username, not 'user'

SFTP

Host


Port


Username


Password
You will be able to define file path during import/export process

this makes sftp available and product import works with sftp.

FIREBEAR:No one using ftp now days and not secure. Please start adding some secure points to module if you build and sell it. We bought it and finally added our own code to work.

Uploading sftp.zip…

@biotech
Copy link
Member

biotech commented Mar 1, 2017

Thanks for that piece - we will have look and implement full support of SFTP!

@biotech
Copy link
Member

biotech commented May 31, 2017

SFTP support added on full version - https://firebearstudio.com/improved-import.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants