ContentUploader::upload()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * File: ContentUploader.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Model\File;
13
14
use \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
15
use \LizardMedia\ProductAttachment\Api\Data\File\ContentUploaderInterface;
16
use \Magento\Downloadable\Api\Data\File\ContentInterface;
17
use \Magento\MediaStorage\Helper\File\Storage\Database;
18
use \Magento\MediaStorage\Helper\File\Storage;
19
use \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
20
use \Magento\MediaStorage\Model\File\Uploader;
21
use \Magento\Framework\App\Filesystem\DirectoryList;
22
use \Magento\Framework\Filesystem;
23
24
/**
25
 * Class ContentUploader
26
 * @package LizardMedia\ProductAttachment\Model\File
27
 */
28
class ContentUploader extends Uploader implements ContentUploaderInterface
29
{
30
    /**
31
     * Default MIME type
32
     */
33
    const DEFAULT_MIME_TYPE = 'application/octet-stream';
34
35
36
    /**
37
     * @var string
38
     */
39
    protected $filePrefix = 'magento_api';
40
41
42
    /**
43
     * @var \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface
44
     */
45
    private $attachmentConfig;
46
47
48
    /**
49
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
50
     */
51
    protected $mediaDirectory;
52
53
54
    /**
55
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
56
     */
57
    protected $systemTmpDirectory;
58
59
60
    /**
61
     * @param \LizardMedia\ProductAttachment\Api\Data\AttachmentInterface $attachment
62
     * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
63
     * @param \Magento\MediaStorage\Helper\File\Storage $fileStorage
64
     * @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
65
     * @param \Magento\Framework\Filesystem $filesystem
66
     *
67
     * @throws \Magento\Framework\Exception\FileSystemException
68
     */
69
    public function __construct(
70
        AttachmentInterface $attachment,
71
        Database $fileStorageDb,
72
        Storage $fileStorage,
73
        NotProtectedExtension $validator,
74
        Filesystem $filesystem
75
    ) {
76
        $this->attachmentConfig = $attachment;
77
        $this->_coreFileStorageDb = $fileStorageDb;
78
        $this->_coreFileStorage = $fileStorage;
79
        $this->_validator = $validator;
80
        $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
81
        $this->systemTmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
82
    }
83
84
85
86
    /**
87
     * @param \Magento\Downloadable\Api\Data\File\ContentInterface $fileContent
88
     * @param string $contentType
89
     *
90
     * @throws \InvalidArgumentException
91
     * @throws \Exception
92
     *
93
     * @return array
94
     */
95
    public function upload(ContentInterface $fileContent, string $contentType) : array
96
    {
97
        $this->_file = $this->decodeContent($fileContent);
98
99
        if (!file_exists($this->_file['tmp_name'])) {
100
            throw new \InvalidArgumentException(__('There was an error during file content upload.'));
101
        }
102
103
        $this->_fileExists = true;
104
        $this->_uploadType = self::SINGLE_STYLE;
105
        $this->setAllowRenameFiles(true);
106
        $this->setFilesDispersion(true);
107
        $result = $this->save($this->getDestinationDirectory($contentType));
108
        unset($result['path']);
109
        $result['status'] = 'new';
110
        $result['name'] = substr($result['file'], strrpos($result['file'], DIRECTORY_SEPARATOR) + 1);
111
        return $result;
112
    }
113
114
115
    /**
116
     * Decode base64 encoded content and save it in system tmp folder
117
     *
118
     * @param ContentInterface $fileContent
119
     *
120
     * @throws \Magento\Framework\Exception\FileSystemException
121
     *
122
     * @return array
123
     */
124
    protected function decodeContent(ContentInterface $fileContent)
125
    {
126
        $tmpFileName = $this->getTmpFileName();
127
        $fileSize = $this->systemTmpDirectory->writeFile($tmpFileName, base64_decode($fileContent->getFileData()));
128
129
        return [
130
            'name' => $fileContent->getName(),
131
            'type' => self::DEFAULT_MIME_TYPE,
132
            'tmp_name' => $this->systemTmpDirectory->getAbsolutePath($tmpFileName),
133
            'error' => 0,
134
            'size' => $fileSize,
135
        ];
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    protected function getTmpFileName()
142
    {
143
        return uniqid($this->filePrefix, true);
144
    }
145
146
147
    /**
148
     * @param string $contentType
149
     *
150
     * @return string
151
     */
152
    protected function getDestinationDirectory($contentType)
153
    {
154
        return $this->mediaDirectory->getAbsolutePath($this->attachmentConfig->getBaseTmpPath());
155
    }
156
}
157