|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ImportService\Model\Import\Processor; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Filesystem; |
| 12 | +use Magento\ImportService\Exception as ImportServiceException; |
| 13 | +use Magento\ImportService\Model\Import\SourceProcessorPool; |
| 14 | +use Magento\ImportService\Model\Source\Validator; |
| 15 | + |
| 16 | +/** |
| 17 | + * CSV files processor for asynchronous import |
| 18 | + */ |
| 19 | +class ExternalFileProcessor implements SourceProcessorInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var \Magento\Framework\Filesystem |
| 23 | + */ |
| 24 | + private $fileSystem; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Magento\ImportService\Model\Source\Validator |
| 28 | + */ |
| 29 | + private $validator; |
| 30 | + |
| 31 | + /** |
| 32 | + * LocalPathFileProcessor constructor |
| 33 | + * |
| 34 | + * @param FileSystem $fileSystem |
| 35 | + * @param Validator $validator |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + FileSystem $fileSystem, |
| 39 | + Validator $validator |
| 40 | + ) { |
| 41 | + $this->fileSystem = $fileSystem; |
| 42 | + $this->validator = $validator; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $source, \Magento\ImportService\Api\Data\SourceUploadResponseInterface $response) |
| 49 | + { |
| 50 | + /** Validate the $source object */ |
| 51 | + if ($errors = $this->validator->validateRequest($source)) { |
| 52 | + throw new ImportServiceException( |
| 53 | + __('Invalid request: %1', implode(", ", $errors)) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** Check if the domain exists and the file within that domain exists */ |
| 58 | + if (!$this->validator->checkIfRemoteFileExists($source->getImportData())) { |
| 59 | + throw new ImportServiceException( |
| 60 | + __('Remote file %1 does not exist.', $source->getImportData()) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** Validate the remote file content type */ |
| 65 | + if (!$this->validator->validateMimeTypeForRemoteFile($source->getImportData())) { |
| 66 | + throw new ImportServiceException( |
| 67 | + __('Invalid mime type, expected is one of: %1', implode(", ", $this->validator->getAllowedMimeTypes())) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** @var string $workingDirectory */ |
| 72 | + $workingDirectory = SourceProcessorPool::WORKING_DIR; |
| 73 | + |
| 74 | + /** @var string $fileName */ |
| 75 | + $fileName = uniqid() . '.' . $source->getSourceType(); |
| 76 | + |
| 77 | + /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $writeInterface */ |
| 78 | + $writeInterface = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 79 | + |
| 80 | + /** If the directory is not present, it will be created */ |
| 81 | + $writeInterface->create($workingDirectory); |
| 82 | + |
| 83 | + /** @var string $copyFileFullPath*/ |
| 84 | + $copyFileFullPath = $writeInterface->getAbsolutePath($workingDirectory) . $fileName; |
| 85 | + |
| 86 | + /** Attempt a copy, may throw \Magento\Framework\Exception\FileSystemException */ |
| 87 | + $writeInterface->getDriver()->copy($source->getImportData(), $copyFileFullPath); |
| 88 | + |
| 89 | + return $response->setSource($source->setImportData($fileName)) |
| 90 | + ->setStatus($response::STATUS_UPLOADED); |
| 91 | + } |
| 92 | +} |
0 commit comments