File.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 File
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class File
  14. {
  15. /**
  16. * Prefix of model events names
  17. *
  18. * @var string
  19. */
  20. protected $_eventPrefix = 'media_storage_file_storage_file';
  21. /**
  22. * Store media base directory path
  23. *
  24. * @var string
  25. */
  26. protected $_mediaBaseDirectory = null;
  27. /**
  28. * Core file storage database
  29. *
  30. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  31. */
  32. protected $_storageHelper = null;
  33. /**
  34. * @var \Magento\MediaStorage\Helper\File\Media
  35. */
  36. protected $_mediaHelper = null;
  37. /**
  38. * Data at storage
  39. *
  40. * @var array
  41. */
  42. protected $_data = null;
  43. /**
  44. * Collect errors during sync process
  45. *
  46. * @var string[]
  47. */
  48. protected $_errors = [];
  49. /**
  50. * @var \Psr\Log\LoggerInterface
  51. */
  52. protected $_logger;
  53. /**
  54. * @param \Psr\Log\LoggerInterface $logger
  55. * @param \Magento\MediaStorage\Helper\File\Storage\Database $storageHelper
  56. * @param \Magento\MediaStorage\Helper\File\Media $mediaHelper
  57. * @param \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility
  58. */
  59. public function __construct(
  60. \Psr\Log\LoggerInterface $logger,
  61. \Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
  62. \Magento\MediaStorage\Helper\File\Media $mediaHelper,
  63. \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility
  64. ) {
  65. $this->_fileUtility = $fileUtility;
  66. $this->_storageHelper = $storageHelper;
  67. $this->_logger = $logger;
  68. $this->_mediaHelper = $mediaHelper;
  69. }
  70. /**
  71. * Initialization
  72. *
  73. * @return $this
  74. */
  75. public function init()
  76. {
  77. return $this;
  78. }
  79. /**
  80. * Return storage name
  81. *
  82. * @return \Magento\Framework\Phrase
  83. */
  84. public function getStorageName()
  85. {
  86. return __('File system');
  87. }
  88. /**
  89. * Get files and directories from storage
  90. *
  91. * @return array
  92. */
  93. public function getStorageData()
  94. {
  95. return $this->_fileUtility->getStorageData();
  96. }
  97. /**
  98. * Check if there was errors during sync process
  99. *
  100. * @return bool
  101. */
  102. public function hasErrors()
  103. {
  104. return !empty($this->_errors);
  105. }
  106. /**
  107. * Clear files and directories in storage
  108. *
  109. * @return $this
  110. */
  111. public function clear()
  112. {
  113. $this->_fileUtility->clear();
  114. return $this;
  115. }
  116. /**
  117. * Collect files and directories from storage
  118. *
  119. * @param int $offset
  120. * @param int $count
  121. * @param string $type
  122. * @return array|bool
  123. * @SuppressWarnings(PHPMD.NPathComplexity)
  124. */
  125. public function collectData($offset = 0, $count = 100, $type = 'files')
  126. {
  127. if (!in_array($type, ['files', 'directories'])) {
  128. return false;
  129. }
  130. $offset = (int)$offset >= 0 ? (int)$offset : 0;
  131. $count = (int)$count >= 1 ? (int)$count : 1;
  132. if (empty($this->_data)) {
  133. $this->_data = $this->getStorageData();
  134. }
  135. if (!array_key_exists($type, $this->_data)) {
  136. return false;
  137. }
  138. $slice = array_slice($this->_data[$type], $offset, $count);
  139. return $slice ?: false;
  140. }
  141. /**
  142. * Retrieve connection name saved at config
  143. *
  144. * @return null
  145. */
  146. public function getConfigConnectionName()
  147. {
  148. return null;
  149. }
  150. /**
  151. * Retrieve connection name
  152. *
  153. * @return null
  154. */
  155. public function getConnectionName()
  156. {
  157. return null;
  158. }
  159. /**
  160. * Export directories list from storage
  161. *
  162. * @param int $offset
  163. * @param int $count
  164. * @return array|bool
  165. */
  166. public function exportDirectories($offset = 0, $count = 100)
  167. {
  168. return $this->collectData($offset, $count, 'directories');
  169. }
  170. /**
  171. * Export files list in defined range
  172. *
  173. * @param int $offset
  174. * @param int $count
  175. * @return array|bool
  176. */
  177. public function exportFiles($offset = 0, $count = 1)
  178. {
  179. $slice = $this->collectData($offset, $count, 'files');
  180. if (!$slice) {
  181. return false;
  182. }
  183. $result = [];
  184. foreach ($slice as $fileName) {
  185. try {
  186. $fileInfo = $this->_mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $fileName);
  187. } catch (\Exception $e) {
  188. $this->_logger->critical($e);
  189. continue;
  190. }
  191. $result[] = $fileInfo;
  192. }
  193. return $result;
  194. }
  195. /**
  196. * Import entities to storage
  197. *
  198. * @param array $data
  199. * @param string $callback
  200. * @return $this
  201. */
  202. public function import($data, $callback)
  203. {
  204. if (!is_array($data) || !method_exists($this, $callback)) {
  205. return $this;
  206. }
  207. foreach ($data as $part) {
  208. try {
  209. $this->{$callback}($part);
  210. } catch (\Exception $e) {
  211. $this->_errors[] = $e->getMessage();
  212. $this->_logger->critical($e);
  213. }
  214. }
  215. return $this;
  216. }
  217. /**
  218. * Import directories to storage
  219. *
  220. * @param array $dirs
  221. * @return $this
  222. */
  223. public function importDirectories($dirs)
  224. {
  225. return $this->import($dirs, 'saveDir');
  226. }
  227. /**
  228. * Import files list
  229. *
  230. * @param array $files
  231. * @return $this
  232. */
  233. public function importFiles($files)
  234. {
  235. return $this->import($files, 'saveFile');
  236. }
  237. /**
  238. * Save directory to storage
  239. *
  240. * @param array $dir
  241. * @return bool
  242. */
  243. public function saveDir($dir)
  244. {
  245. return $this->_fileUtility->saveDir($dir);
  246. }
  247. /**
  248. * Save file to storage
  249. *
  250. * @param array $file
  251. * @param bool $overwrite
  252. * @throws \Magento\Framework\Exception\LocalizedException
  253. * @return bool
  254. */
  255. public function saveFile($file, $overwrite = true)
  256. {
  257. if (isset($file['filename'])
  258. && !empty($file['filename'])
  259. && isset($file['content'])
  260. && !empty($file['content'])
  261. ) {
  262. try {
  263. $filename = isset(
  264. $file['directory']
  265. ) && !empty($file['directory']) ? $file['directory'] . '/' . $file['filename'] : $file['filename'];
  266. return $this->_fileUtility->saveFile($filename, $file['content'], $overwrite);
  267. } catch (\Exception $e) {
  268. $this->_logger->critical($e);
  269. throw new \Magento\Framework\Exception\LocalizedException(
  270. __('Unable to save file "%1" at "%2"', $file['filename'], $file['directory'])
  271. );
  272. }
  273. } else {
  274. throw new \Magento\Framework\Exception\LocalizedException(__('Wrong file info format'));
  275. }
  276. }
  277. /**
  278. * Retrieve media base directory path
  279. *
  280. * @return string
  281. */
  282. public function getMediaBaseDirectory()
  283. {
  284. if ($this->_mediaBaseDirectory === null) {
  285. $this->_mediaBaseDirectory = $this->_storageHelper->getMediaBaseDir();
  286. }
  287. return $this->_mediaBaseDirectory;
  288. }
  289. }