Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Database.php
Go to the documentation of this file.
1 <?php
8 
11 
19 {
24  protected $_databaseModel = null;
25 
30  protected $_resourceModel = null;
31 
37  protected $_useDb = null;
38 
45 
49  protected $_filesystem;
50 
54  protected $_dbStorageFactory;
55 
59  protected $_fileStorage;
60 
67  public function __construct(
68  \Magento\Framework\App\Helper\Context $context,
69  \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory,
70  \Magento\MediaStorage\Model\File\Storage\File $fileStorage,
72  ) {
73  $this->_filesystem = $filesystem;
74  $this->_dbStorageFactory = $dbStorageFactory;
75  $this->_fileStorage = $fileStorage;
76  parent::__construct($context);
77  }
78 
84  public function checkDbUsage()
85  {
86  if (null === $this->_useDb) {
87  $currentStorage = (int)$this->scopeConfig->getValue(
88  \Magento\MediaStorage\Model\File\Storage::XML_PATH_STORAGE_MEDIA,
89  'default'
90  );
92  }
93 
94  return $this->_useDb;
95  }
96 
102  public function getStorageDatabaseModel()
103  {
104  if ($this->_databaseModel === null) {
105  $this->_databaseModel = $this->_dbStorageFactory->create();
106  }
107 
108  return $this->_databaseModel;
109  }
110 
116  public function getStorageFileModel()
117  {
118  return $this->_fileStorage;
119  }
120 
126  public function getResourceStorageModel()
127  {
128  if ($this->_resourceModel === null) {
129  $this->_resourceModel = $this->getStorageDatabaseModel()->getResource();
130  }
131  return $this->_resourceModel;
132  }
133 
140  public function saveFile($filename)
141  {
142  if ($this->checkDbUsage()) {
143  $this->getStorageDatabaseModel()->saveFile($this->_removeAbsPathFromFileName($filename));
144  }
145  }
146 
154  public function renameFile($oldName, $newName)
155  {
156  if ($this->checkDbUsage()) {
157  $this->getStorageDatabaseModel()->renameFile(
158  $this->_removeAbsPathFromFileName($oldName),
159  $this->_removeAbsPathFromFileName($newName)
160  );
161  }
162  }
163 
171  public function copyFile($oldName, $newName)
172  {
173  if ($this->checkDbUsage()) {
174  $this->getStorageDatabaseModel()->copyFile(
175  $this->_removeAbsPathFromFileName($oldName),
176  $this->_removeAbsPathFromFileName($newName)
177  );
178  }
179  }
180 
187  public function fileExists($filename)
188  {
189  if ($this->checkDbUsage()) {
190  return $this->getStorageDatabaseModel()->fileExists($this->_removeAbsPathFromFileName($filename));
191  } else {
192  return null;
193  }
194  }
195 
203  public function getUniqueFilename($directory, $filename)
204  {
205  if ($this->checkDbUsage()) {
206  $directory = $this->_removeAbsPathFromFileName($directory);
207  if ($this->fileExists($directory . $filename)) {
208  $index = 1;
209  $extension = strrchr($filename, '.');
210  $filenameWoExtension = substr($filename, 0, -1 * strlen($extension));
211  while ($this->fileExists($directory . $filenameWoExtension . '_' . $index . $extension)) {
212  $index++;
213  }
214  $filename = $filenameWoExtension . '_' . $index . $extension;
215  }
216  }
217  return $filename;
218  }
219 
226  public function saveFileToFilesystem($filename)
227  {
228  if ($this->checkDbUsage()) {
230  $file = $this->_dbStorageFactory->create()->loadByFilename($this->_removeAbsPathFromFileName($filename));
231  if (!$file->getId()) {
232  return false;
233  }
234 
235  return $this->getStorageFileModel()->saveFile($file->getData(), true);
236  }
237  return false;
238  }
239 
246  public function getMediaRelativePath($fullPath)
247  {
248  $relativePath = ltrim(str_replace($this->getMediaBaseDir(), '', $fullPath), '\\/');
249  return str_replace('\\', '/', $relativePath);
250  }
251 
258  public function deleteFolder($folderName)
259  {
260  if ($this->checkDbUsage()) {
261  $this->getResourceStorageModel()->deleteFolder($this->_removeAbsPathFromFileName($folderName));
262  }
263  }
264 
271  public function deleteFile($filename)
272  {
273  if ($this->checkDbUsage()) {
274  $this->getStorageDatabaseModel()->deleteFile($this->_removeAbsPathFromFileName($filename));
275  }
276  }
277 
291  public function saveUploadedFile($result)
292  {
293  if ($this->checkDbUsage()) {
294  $path = rtrim(str_replace(['\\', '/'], '/', $result['path']), '/');
295  $file = '/' . ltrim($result['file'], '\\/');
296 
297  $uniqueResultFile = $this->getUniqueFilename($path, $file);
298 
299  if ($uniqueResultFile !== $file) {
300  $dirWrite = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
301  $dirWrite->renameFile($path . $file, $path . $uniqueResultFile);
302  }
303  $this->saveFile($path . $uniqueResultFile);
304 
305  return $uniqueResultFile;
306  } else {
307  return $result['file'];
308  }
309  }
310 
318  protected function _removeAbsPathFromFileName($filename)
319  {
320  return $this->getMediaRelativePath($filename);
321  }
322 
328  public function getMediaBaseDir()
329  {
330  if (null === $this->_mediaBaseDirectory) {
331  $mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
332  $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
333  }
335  }
336 }
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory, \Magento\MediaStorage\Model\File\Storage\File $fileStorage, Filesystem $filesystem)
Definition: Database.php:67
$relativePath
Definition: get.php:35
$filesystem
$index
Definition: list.phtml:44