aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/ImageManager.php
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2018-02-26 13:54:00 +0100
committerJulius Härtl <jus@bitgrid.net>2018-04-19 20:14:38 +0200
commit272b392cacdf34d2166105c91b0a06de3e51ed06 (patch)
tree6df721b89919f811ff60b225622a23211ef11aff /apps/theming/lib/ImageManager.php
parent0bae516c66efe9004d0a8ff504e0deb0069fc9d9 (diff)
downloadnextcloud-server-272b392cacdf34d2166105c91b0a06de3e51ed06.tar.gz
nextcloud-server-272b392cacdf34d2166105c91b0a06de3e51ed06.zip
Move to more generic image handling and add extra images
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'apps/theming/lib/ImageManager.php')
-rw-r--r--apps/theming/lib/ImageManager.php72
1 files changed, 66 insertions, 6 deletions
diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php
index 14dba0d0742..06cc37a3d1b 100644
--- a/apps/theming/lib/ImageManager.php
+++ b/apps/theming/lib/ImageManager.php
@@ -24,11 +24,16 @@
namespace OCA\Theming;
+use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
+use OCP\IURLGenerator;
+/**
+ * @property IURLGenerator urlGenerator
+ */
class ImageManager {
/** @var IConfig */
@@ -36,27 +41,79 @@ class ImageManager {
/** @var IAppData */
private $appData;
+ /** @var array */
+ private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
+
/**
* ImageManager constructor.
*
* @param IConfig $config
* @param IAppData $appData
+ * @param IURLGenerator $urlGenerator
*/
public function __construct(IConfig $config,
- IAppData $appData
+ IAppData $appData,
+ IURLGenerator $urlGenerator
) {
$this->config = $config;
$this->appData = $appData;
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ public function getImageUrl(string $key): string {
+ $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
+ try {
+ $this->getImage($key);
+ return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
+ } catch (NotFoundException $e) {
+ }
+
+ switch ($key) {
+ case 'logo':
+ case 'logoheader':
+ case 'favicon':
+ return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
+ case 'background':
+ return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
+ }
+ }
+
+ public function getImageUrlAbsolute(string $key): string {
+ return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key));
+ }
+
+ /**
+ * @param $key
+ * @return ISimpleFile
+ * @throws NotFoundException
+ */
+ public function getImage(string $key): ISimpleFile {
+ $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
+ if ($logo === false) {
+ throw new NotFoundException();
+ }
+ $folder = $this->appData->getFolder('images');
+ return $folder->getFile($key);
+ }
+
+ public function getCustomImages(): array {
+ $images = [];
+ foreach ($this->supportedImageKeys as $key) {
+ $images[$key] = [
+ 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
+ 'url' => $this->getImageUrl($key),
+ ];
+ }
+ return $images;
}
/**
* Get folder for current theming files
*
- * @return \OCP\Files\SimpleFS\ISimpleFolder
+ * @return ISimpleFolder
* @throws NotPermittedException
- * @throws \RuntimeException
*/
- public function getCacheFolder() {
+ public function getCacheFolder(): ISimpleFolder {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
try {
$folder = $this->appData->getFolder($cacheBusterValue);
@@ -73,8 +130,9 @@ class ImageManager {
* @param string $filename
* @throws NotFoundException
* @return \OCP\Files\SimpleFS\ISimpleFile
+ * @throws NotPermittedException
*/
- public function getCachedImage($filename) {
+ public function getCachedImage($filename): ISimpleFile {
$currentFolder = $this->getCacheFolder();
return $currentFolder->getFile($filename);
}
@@ -85,8 +143,10 @@ class ImageManager {
* @param string $filename
* @param string $data
* @return \OCP\Files\SimpleFS\ISimpleFile
+ * @throws NotFoundException
+ * @throws NotPermittedException
*/
- public function setCachedImage($filename, $data) {
+ public function setCachedImage($filename, $data): ISimpleFile {
$currentFolder = $this->getCacheFolder();
if ($currentFolder->fileExists($filename)) {
$file = $currentFolder->getFile($filename);