ImageManager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Haertl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming;
  24. use OCP\IConfig;
  25. use OCP\Files\IAppData;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Files\NotPermittedException;
  28. class ImageManager {
  29. /** @var IConfig */
  30. private $config;
  31. /** @var IAppData */
  32. private $appData;
  33. /**
  34. * ImageManager constructor.
  35. *
  36. * @param IConfig $config
  37. * @param IAppData $appData
  38. */
  39. public function __construct(IConfig $config,
  40. IAppData $appData
  41. ) {
  42. $this->config = $config;
  43. $this->appData = $appData;
  44. }
  45. /**
  46. * Get folder for current theming files
  47. *
  48. * @return \OCP\Files\SimpleFS\ISimpleFolder
  49. * @throws NotPermittedException
  50. * @throws \RuntimeException
  51. */
  52. public function getCacheFolder() {
  53. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  54. try {
  55. $folder = $this->appData->getFolder($cacheBusterValue);
  56. } catch (NotFoundException $e) {
  57. $folder = $this->appData->newFolder($cacheBusterValue);
  58. $this->cleanup();
  59. }
  60. return $folder;
  61. }
  62. /**
  63. * Get a file from AppData
  64. *
  65. * @param string $filename
  66. * @throws NotFoundException
  67. * @return \OCP\Files\SimpleFS\ISimpleFile
  68. */
  69. public function getCachedImage($filename) {
  70. $currentFolder = $this->getCacheFolder();
  71. return $currentFolder->getFile($filename);
  72. }
  73. /**
  74. * Store a file for theming in AppData
  75. *
  76. * @param string $filename
  77. * @param string $data
  78. * @return \OCP\Files\SimpleFS\ISimpleFile
  79. */
  80. public function setCachedImage($filename, $data) {
  81. $currentFolder = $this->getCacheFolder();
  82. if ($currentFolder->fileExists($filename)) {
  83. $file = $currentFolder->getFile($filename);
  84. } else {
  85. $file = $currentFolder->newFile($filename);
  86. }
  87. $file->putContent($data);
  88. return $file;
  89. }
  90. /**
  91. * remove cached files that are not required any longer
  92. */
  93. public function cleanup() {
  94. $currentFolder = $this->getCacheFolder();
  95. $folders = $this->appData->getDirectoryListing();
  96. foreach ($folders as $folder) {
  97. if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
  98. $folder->delete();
  99. }
  100. }
  101. }
  102. }