PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/MediaStorage/Helper/File/Storage/Database.php

https://gitlab.com/svillegas/magento2
PHP | 336 lines | 186 code | 29 blank | 121 comment | 17 complexity | 0f36a8be6faa9f161f9eb7407c157ee6 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Database saving file helper
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\MediaStorage\Helper\File\Storage;
  12. use Magento\Framework\App\Filesystem\DirectoryList;
  13. use Magento\Framework\Filesystem;
  14. class Database extends \Magento\Framework\App\Helper\AbstractHelper
  15. {
  16. /**
  17. * Database storage model
  18. * @var null|\Magento\MediaStorage\Model\File\Storage\Database
  19. */
  20. protected $_databaseModel = null;
  21. /**
  22. * Storage resource model
  23. * @var null|\Magento\MediaStorage\Model\ResourceModel\File\Storage\Database
  24. */
  25. protected $_resourceModel = null;
  26. /**
  27. * Db usage flag
  28. *
  29. * @var bool
  30. */
  31. protected $_useDb = null;
  32. /**
  33. * Media dir
  34. *
  35. * @var string
  36. */
  37. protected $_mediaBaseDirectory;
  38. /**
  39. * @var Filesystem
  40. */
  41. protected $_filesystem;
  42. /**
  43. * @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory
  44. */
  45. protected $_dbStorageFactory;
  46. /**
  47. * @var \Magento\MediaStorage\Model\File\Storage\File
  48. */
  49. protected $_fileStorage;
  50. /**
  51. * @param \Magento\Framework\App\Helper\Context $context
  52. * @param \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory
  53. * @param \Magento\MediaStorage\Model\File\Storage\File $fileStorage
  54. * @param Filesystem $filesystem
  55. */
  56. public function __construct(
  57. \Magento\Framework\App\Helper\Context $context,
  58. \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory,
  59. \Magento\MediaStorage\Model\File\Storage\File $fileStorage,
  60. Filesystem $filesystem
  61. ) {
  62. $this->_filesystem = $filesystem;
  63. $this->_dbStorageFactory = $dbStorageFactory;
  64. $this->_fileStorage = $fileStorage;
  65. parent::__construct($context);
  66. }
  67. /**
  68. * Check if we use DB storage
  69. * Note: Disabled as not completed feature
  70. *
  71. * @return bool
  72. */
  73. public function checkDbUsage()
  74. {
  75. if (null === $this->_useDb) {
  76. $currentStorage = (int)$this->scopeConfig->getValue(
  77. \Magento\MediaStorage\Model\File\Storage::XML_PATH_STORAGE_MEDIA,
  78. 'default'
  79. );
  80. $this->_useDb = $currentStorage == \Magento\MediaStorage\Model\File\Storage::STORAGE_MEDIA_DATABASE;
  81. }
  82. return $this->_useDb;
  83. }
  84. /**
  85. * Get database storage model
  86. *
  87. * @return \Magento\MediaStorage\Model\File\Storage\Database
  88. */
  89. public function getStorageDatabaseModel()
  90. {
  91. if ($this->_databaseModel === null) {
  92. $this->_databaseModel = $this->_dbStorageFactory->create();
  93. }
  94. return $this->_databaseModel;
  95. }
  96. /**
  97. * Get file storage model
  98. *
  99. * @return \Magento\MediaStorage\Model\File\Storage\File
  100. */
  101. public function getStorageFileModel()
  102. {
  103. return $this->_fileStorage;
  104. }
  105. /**
  106. * Get storage model
  107. *
  108. * @return \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database
  109. */
  110. public function getResourceStorageModel()
  111. {
  112. if ($this->_resourceModel === null) {
  113. $this->_resourceModel = $this->getStorageDatabaseModel()->getResource();
  114. }
  115. return $this->_resourceModel;
  116. }
  117. /**
  118. * Save file in DB storage
  119. *
  120. * @param string $filename
  121. * @return void
  122. */
  123. public function saveFile($filename)
  124. {
  125. if ($this->checkDbUsage()) {
  126. $this->getStorageDatabaseModel()->saveFile($this->_removeAbsPathFromFileName($filename));
  127. }
  128. }
  129. /**
  130. * Rename file in DB storage
  131. *
  132. * @param string $oldName
  133. * @param string $newName
  134. * @return void
  135. */
  136. public function renameFile($oldName, $newName)
  137. {
  138. if ($this->checkDbUsage()) {
  139. $this->getStorageDatabaseModel()->renameFile(
  140. $this->_removeAbsPathFromFileName($oldName),
  141. $this->_removeAbsPathFromFileName($newName)
  142. );
  143. }
  144. }
  145. /**
  146. * Copy file in DB storage
  147. *
  148. * @param string $oldName
  149. * @param string $newName
  150. * @return void
  151. */
  152. public function copyFile($oldName, $newName)
  153. {
  154. if ($this->checkDbUsage()) {
  155. $this->getStorageDatabaseModel()->copyFile(
  156. $this->_removeAbsPathFromFileName($oldName),
  157. $this->_removeAbsPathFromFileName($newName)
  158. );
  159. }
  160. }
  161. /**
  162. * Check whether file exists in DB
  163. *
  164. * @param string $filename can be both full path or partial (like in DB)
  165. * @return bool|null
  166. */
  167. public function fileExists($filename)
  168. {
  169. if ($this->checkDbUsage()) {
  170. return $this->getStorageDatabaseModel()->fileExists($this->_removeAbsPathFromFileName($filename));
  171. } else {
  172. return null;
  173. }
  174. }
  175. /**
  176. * Get unique name for passed file in case this file already exists
  177. *
  178. * @param string $directory - can be both full path or partial (like in DB)
  179. * @param string $filename - not just a filename. Can have directory chunks. return will have this form
  180. * @return string
  181. */
  182. public function getUniqueFilename($directory, $filename)
  183. {
  184. if ($this->checkDbUsage()) {
  185. $directory = $this->_removeAbsPathFromFileName($directory);
  186. if ($this->fileExists($directory . $filename)) {
  187. $index = 1;
  188. $extension = strrchr($filename, '.');
  189. $filenameWoExtension = substr($filename, 0, -1 * strlen($extension));
  190. while ($this->fileExists($directory . $filenameWoExtension . '_' . $index . $extension)) {
  191. $index++;
  192. }
  193. $filename = $filenameWoExtension . '_' . $index . $extension;
  194. }
  195. }
  196. return $filename;
  197. }
  198. /**
  199. * Save database file to file system
  200. *
  201. * @param string $filename
  202. * @return bool|int
  203. */
  204. public function saveFileToFilesystem($filename)
  205. {
  206. if ($this->checkDbUsage()) {
  207. /** @var $file \Magento\MediaStorage\Model\File\Storage\Database */
  208. $file = $this->_dbStorageFactory->create()->loadByFilename($this->_removeAbsPathFromFileName($filename));
  209. if (!$file->getId()) {
  210. return false;
  211. }
  212. return $this->getStorageFileModel()->saveFile($file->getData(), true);
  213. }
  214. return false;
  215. }
  216. /**
  217. * Return relative uri for media content by full path
  218. *
  219. * @param string $fullPath
  220. * @return string
  221. */
  222. public function getMediaRelativePath($fullPath)
  223. {
  224. $relativePath = ltrim(str_replace($this->getMediaBaseDir(), '', $fullPath), '\\/');
  225. return str_replace('\\', '/', $relativePath);
  226. }
  227. /**
  228. * Deletes from DB files, which belong to one folder
  229. *
  230. * @param string $folderName
  231. * @return void
  232. */
  233. public function deleteFolder($folderName)
  234. {
  235. if ($this->checkDbUsage()) {
  236. $this->getResourceStorageModel()->deleteFolder($this->_removeAbsPathFromFileName($folderName));
  237. }
  238. }
  239. /**
  240. * Deletes from DB files, which belong to one folder
  241. *
  242. * @param string $filename
  243. * @return void
  244. */
  245. public function deleteFile($filename)
  246. {
  247. if ($this->checkDbUsage()) {
  248. $this->getStorageDatabaseModel()->deleteFile($this->_removeAbsPathFromFileName($filename));
  249. }
  250. }
  251. /**
  252. * Saves uploaded by \Magento\MediaStorage\Model\File\Uploader file to DB with existence tests
  253. *
  254. * param $result should be result from \Magento\MediaStorage\Model\File\Uploader::save() method
  255. * Checks in DB, whether uploaded file exists ($result['file'])
  256. * If yes, renames file on FS (!!!!!)
  257. * Saves file with unique name into DB
  258. * If passed file exists returns new name, file was renamed to (in the same context)
  259. * Otherwise returns $result['file']
  260. *
  261. * @param array $result
  262. * @return string
  263. */
  264. public function saveUploadedFile($result)
  265. {
  266. if ($this->checkDbUsage()) {
  267. $path = rtrim(str_replace(['\\', '/'], '/', $result['path']), '/');
  268. $file = '/' . ltrim($result['file'], '\\/');
  269. $uniqueResultFile = $this->getUniqueFilename($path, $file);
  270. if ($uniqueResultFile !== $file) {
  271. $dirWrite = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
  272. $dirWrite->renameFile($path . $file, $path . $uniqueResultFile);
  273. }
  274. $this->saveFile($path . $uniqueResultFile);
  275. return $uniqueResultFile;
  276. } else {
  277. return $result['file'];
  278. }
  279. }
  280. /**
  281. * Convert full file path to local (as used by model)
  282. * If not - returns just a filename
  283. *
  284. * @param string $filename
  285. * @return string
  286. */
  287. protected function _removeAbsPathFromFileName($filename)
  288. {
  289. return $this->getMediaRelativePath($filename);
  290. }
  291. /**
  292. * Return Media base dir
  293. *
  294. * @return string
  295. */
  296. public function getMediaBaseDir()
  297. {
  298. if (null === $this->_mediaBaseDirectory) {
  299. $mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
  300. $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
  301. }
  302. return $this->_mediaBaseDirectory;
  303. }
  304. }