Database.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\MediaStorage\Helper\File\Storage;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. /**
  10. * Database saving file helper
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Database extends \Magento\Framework\App\Helper\AbstractHelper
  16. {
  17. /**
  18. * Database storage model
  19. * @var null|\Magento\MediaStorage\Model\File\Storage\Database
  20. */
  21. protected $_databaseModel = null;
  22. /**
  23. * Storage resource model
  24. * @var null|\Magento\MediaStorage\Model\ResourceModel\File\Storage\Database
  25. */
  26. protected $_resourceModel = null;
  27. /**
  28. * Db usage flag
  29. *
  30. * @var bool
  31. */
  32. protected $_useDb = null;
  33. /**
  34. * Media dir
  35. *
  36. * @var string
  37. */
  38. protected $_mediaBaseDirectory;
  39. /**
  40. * @var Filesystem
  41. */
  42. protected $_filesystem;
  43. /**
  44. * @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory
  45. */
  46. protected $_dbStorageFactory;
  47. /**
  48. * @var \Magento\MediaStorage\Model\File\Storage\File
  49. */
  50. protected $_fileStorage;
  51. /**
  52. * @param \Magento\Framework\App\Helper\Context $context
  53. * @param \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory
  54. * @param \Magento\MediaStorage\Model\File\Storage\File $fileStorage
  55. * @param Filesystem $filesystem
  56. */
  57. public function __construct(
  58. \Magento\Framework\App\Helper\Context $context,
  59. \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $dbStorageFactory,
  60. \Magento\MediaStorage\Model\File\Storage\File $fileStorage,
  61. Filesystem $filesystem
  62. ) {
  63. $this->_filesystem = $filesystem;
  64. $this->_dbStorageFactory = $dbStorageFactory;
  65. $this->_fileStorage = $fileStorage;
  66. parent::__construct($context);
  67. }
  68. /**
  69. * Check if we use DB storage
  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. }