Database.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Model\File\Storage;
  7. /**
  8. * Class Database
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Database extends \Magento\MediaStorage\Model\File\Storage\Database\AbstractDatabase
  14. {
  15. /**
  16. * Prefix of model events names
  17. *
  18. * @var string
  19. */
  20. protected $_eventPrefix = 'media_storage_file_storage_database';
  21. /**
  22. * Directory singleton
  23. *
  24. * @var \Magento\MediaStorage\Model\File\Storage\Directory\Database
  25. */
  26. protected $_directoryModel = null;
  27. /**
  28. * Collect errors during sync process
  29. *
  30. * @var string[]
  31. */
  32. protected $_errors = [];
  33. /**
  34. * @var \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory
  35. */
  36. protected $_directoryFactory;
  37. /**
  38. * @var \Magento\MediaStorage\Helper\File\Media
  39. */
  40. protected $_mediaHelper;
  41. /**
  42. * Store media base directory path
  43. *
  44. * @var string
  45. * @since 100.1.0
  46. */
  47. protected $mediaBaseDirectory = null;
  48. /**
  49. * @param \Magento\Framework\Model\Context $context
  50. * @param \Magento\Framework\Registry $registry
  51. * @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
  52. * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
  53. * @param \Magento\Framework\App\Config\ScopeConfigInterface $configuration
  54. * @param \Magento\MediaStorage\Helper\File\Media $mediaHelper
  55. * @param \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource
  56. * @param Directory\DatabaseFactory $directoryFactory
  57. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  58. * @param string $connectionName
  59. * @param array $data
  60. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  61. */
  62. public function __construct(
  63. \Magento\Framework\Model\Context $context,
  64. \Magento\Framework\Registry $registry,
  65. \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
  66. \Magento\Framework\Stdlib\DateTime\DateTime $dateModel,
  67. \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
  68. \Magento\MediaStorage\Helper\File\Media $mediaHelper,
  69. \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource,
  70. \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory $directoryFactory,
  71. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  72. $connectionName = null,
  73. array $data = []
  74. ) {
  75. $this->_directoryFactory = $directoryFactory;
  76. $this->_mediaHelper = $mediaHelper;
  77. parent::__construct(
  78. $context,
  79. $registry,
  80. $coreFileStorageDb,
  81. $dateModel,
  82. $configuration,
  83. $resource,
  84. $resourceCollection,
  85. $connectionName,
  86. $data
  87. );
  88. $this->_init(get_class($this->_resource));
  89. }
  90. /**
  91. * Retrieve directory model
  92. *
  93. * @return \Magento\MediaStorage\Model\File\Storage\Directory\Database
  94. */
  95. public function getDirectoryModel()
  96. {
  97. if ($this->_directoryModel === null) {
  98. $this->_directoryModel = $this->_directoryFactory->create(
  99. ['connectionName' => $this->getConnectionName()]
  100. );
  101. }
  102. return $this->_directoryModel;
  103. }
  104. /**
  105. * Create tables for file and directory storages
  106. *
  107. * @return $this
  108. */
  109. public function init()
  110. {
  111. $this->getDirectoryModel()->prepareStorage();
  112. $this->prepareStorage();
  113. return $this;
  114. }
  115. /**
  116. * Return storage name
  117. *
  118. * @return \Magento\Framework\Phrase
  119. */
  120. public function getStorageName()
  121. {
  122. return __('database "%1"', $this->getConnectionName());
  123. }
  124. /**
  125. * Load object data by filename
  126. *
  127. * @param string $filePath
  128. * @return $this
  129. */
  130. public function loadByFilename($filePath)
  131. {
  132. $filename = basename($filePath);
  133. $path = dirname($filePath);
  134. $this->_getResource()->loadByFilename($this, $filename, $path);
  135. return $this;
  136. }
  137. /**
  138. * Check if there was errors during sync process
  139. *
  140. * @return bool
  141. */
  142. public function hasErrors()
  143. {
  144. return !empty($this->_errors) || $this->getDirectoryModel()->hasErrors();
  145. }
  146. /**
  147. * Clear files and directories in storage
  148. *
  149. * @return $this
  150. */
  151. public function clear()
  152. {
  153. $this->getDirectoryModel()->clearDirectories();
  154. $this->_getResource()->clearFiles();
  155. return $this;
  156. }
  157. /**
  158. * Export directories from storage
  159. *
  160. * @param int $offset
  161. * @param int $count
  162. * @return bool|array
  163. */
  164. public function exportDirectories($offset = 0, $count = 100)
  165. {
  166. return $this->getDirectoryModel()->exportDirectories($offset, $count);
  167. }
  168. /**
  169. * Import directories to storage
  170. *
  171. * @param array $dirs
  172. * @return \Magento\MediaStorage\Model\File\Storage\Directory\Database
  173. */
  174. public function importDirectories($dirs)
  175. {
  176. return $this->getDirectoryModel()->importDirectories($dirs);
  177. }
  178. /**
  179. * Export files list in defined range
  180. *
  181. * @param int $offset
  182. * @param int $count
  183. * @return array|bool
  184. */
  185. public function exportFiles($offset = 0, $count = 100)
  186. {
  187. $offset = (int)$offset >= 0 ? (int)$offset : 0;
  188. $count = (int)$count >= 1 ? (int)$count : 1;
  189. $result = $this->_getResource()->getFiles($offset, $count);
  190. if (empty($result)) {
  191. return false;
  192. }
  193. return $result;
  194. }
  195. /**
  196. * Import files list
  197. *
  198. * @param array $files
  199. * @return $this
  200. */
  201. public function importFiles($files)
  202. {
  203. if (!is_array($files)) {
  204. return $this;
  205. }
  206. $dateSingleton = $this->_date;
  207. foreach ($files as $file) {
  208. if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
  209. continue;
  210. }
  211. try {
  212. $file['update_time'] = $dateSingleton->date();
  213. $file['directory_id'] = isset(
  214. $file['directory']
  215. ) && strlen(
  216. $file['directory']
  217. ) ? $this->_directoryFactory->create(
  218. ['connectionName' => $this->getConnectionName()]
  219. )->loadByPath(
  220. $file['directory']
  221. )->getId() : null;
  222. $this->_getResource()->saveFile($file);
  223. } catch (\Exception $e) {
  224. $this->_errors[] = $e->getMessage();
  225. $this->_logger->critical($e);
  226. }
  227. }
  228. return $this;
  229. }
  230. /**
  231. * Store file into database
  232. *
  233. * @param string $filename
  234. * @return $this
  235. */
  236. public function saveFile($filename)
  237. {
  238. $fileInfo = $this->_mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $filename);
  239. $filePath = $fileInfo['directory'];
  240. $directory = $this->_directoryFactory->create()->loadByPath($filePath);
  241. if (!$directory->getId()) {
  242. $directory = $this->getDirectoryModel()->createRecursive($filePath);
  243. }
  244. $fileInfo['directory_id'] = $directory->getId();
  245. $this->_getResource()->saveFile($fileInfo);
  246. return $this;
  247. }
  248. /**
  249. * Check whether file exists in DB
  250. *
  251. * @param string $filePath
  252. * @return bool
  253. */
  254. public function fileExists($filePath)
  255. {
  256. return $this->_getResource()->fileExists(basename($filePath), dirname($filePath));
  257. }
  258. /**
  259. * Copy files
  260. *
  261. * @param string $oldFilePath
  262. * @param string $newFilePath
  263. * @return $this
  264. */
  265. public function copyFile($oldFilePath, $newFilePath)
  266. {
  267. $this->_getResource()->copyFile(
  268. basename($oldFilePath),
  269. dirname($oldFilePath),
  270. basename($newFilePath),
  271. dirname($newFilePath)
  272. );
  273. return $this;
  274. }
  275. /**
  276. * Rename files in database
  277. *
  278. * @param string $oldFilePath
  279. * @param string $newFilePath
  280. * @return $this
  281. */
  282. public function renameFile($oldFilePath, $newFilePath)
  283. {
  284. $this->_getResource()->renameFile(
  285. basename($oldFilePath),
  286. dirname($oldFilePath),
  287. basename($newFilePath),
  288. dirname($newFilePath)
  289. );
  290. $newPath = dirname($newFilePath);
  291. $directory = $this->_directoryFactory->create()->loadByPath($newPath);
  292. if (!$directory->getId()) {
  293. $directory = $this->getDirectoryModel()->createRecursive($newPath);
  294. }
  295. $this->loadByFilename($newFilePath);
  296. if ($this->getId()) {
  297. $this->setDirectoryId($directory->getId())->save();
  298. }
  299. return $this;
  300. }
  301. /**
  302. * Return directory listing
  303. *
  304. * @param string $directory
  305. * @return array
  306. */
  307. public function getDirectoryFiles($directory)
  308. {
  309. $directory = $this->_coreFileStorageDb->getMediaRelativePath($directory);
  310. return $this->_getResource()->getDirectoryFiles($directory);
  311. }
  312. /**
  313. * Delete file from database
  314. *
  315. * @param string $path
  316. * @return $this
  317. */
  318. public function deleteFile($path)
  319. {
  320. $filename = basename($path);
  321. $directory = dirname($path);
  322. $this->_getResource()->deleteFile($filename, $directory);
  323. return $this;
  324. }
  325. /**
  326. * Retrieve media base directory path
  327. *
  328. * @return string
  329. * @since 100.1.0
  330. */
  331. public function getMediaBaseDirectory()
  332. {
  333. if ($this->mediaBaseDirectory === null) {
  334. $this->mediaBaseDirectory = $this->_coreFileStorageDb->getMediaBaseDir();
  335. }
  336. return $this->mediaBaseDirectory;
  337. }
  338. }