2018-06-25 12:17:00 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-06-25 12:17:00 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2018-06-25 12:17:00 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2018-06-25 12:17:00 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-25 12:17:00 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Template\IconsCacher;
|
2020-01-22 23:13:36 +03:00
|
|
|
use OCP\App\AppPathNotFoundException;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\App\IAppManager;
|
2018-06-25 12:17:00 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class SvgController extends Controller {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $serverRoot;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2018-07-19 23:46:32 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
/** @var IconsCacher */
|
|
|
|
private $iconsCacher;
|
|
|
|
|
2018-06-25 12:17:00 +03:00
|
|
|
public function __construct(string $appName,
|
|
|
|
IRequest $request,
|
2018-07-19 23:46:32 +03:00
|
|
|
ITimeFactory $timeFactory,
|
2018-10-25 12:57:27 +03:00
|
|
|
IAppManager $appManager,
|
|
|
|
IconsCacher $iconsCacher) {
|
2018-06-25 12:17:00 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2020-10-05 16:12:57 +03:00
|
|
|
$this->serverRoot = \OC::$SERVERROOT;
|
2018-06-25 12:17:00 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
2018-07-19 23:46:32 +03:00
|
|
|
$this->appManager = $appManager;
|
2018-10-25 12:57:27 +03:00
|
|
|
$this->iconsCacher = $iconsCacher;
|
2018-06-25 12:17:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-25 13:05:18 +03:00
|
|
|
* @PublicPage
|
2018-06-25 12:17:00 +03:00
|
|
|
* @NoCSRFRequired
|
2018-10-17 10:24:21 +03:00
|
|
|
* @NoSameSiteCookieRequired
|
2018-06-25 12:17:00 +03:00
|
|
|
*
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
2018-06-29 17:10:53 +03:00
|
|
|
* @param string $folder
|
2018-06-25 12:17:00 +03:00
|
|
|
* @param string $fileName
|
|
|
|
* @param string $color
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-25 12:17:00 +03:00
|
|
|
*/
|
2018-06-29 17:10:53 +03:00
|
|
|
public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
|
|
|
|
$path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
|
2018-07-04 11:04:38 +03:00
|
|
|
return $this->getSvg($path, $color, $fileName);
|
2018-06-26 11:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-25 13:05:18 +03:00
|
|
|
* @PublicPage
|
2018-06-26 11:10:49 +03:00
|
|
|
* @NoCSRFRequired
|
2018-10-17 10:24:21 +03:00
|
|
|
* @NoSameSiteCookieRequired
|
2018-06-26 11:10:49 +03:00
|
|
|
*
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
2018-06-29 17:10:53 +03:00
|
|
|
* @param string $app
|
2018-06-26 11:10:49 +03:00
|
|
|
* @param string $fileName
|
|
|
|
* @param string $color
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-26 11:10:49 +03:00
|
|
|
*/
|
|
|
|
public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
|
2020-01-22 23:13:36 +03:00
|
|
|
try {
|
|
|
|
$appPath = $this->appManager->getAppPath($app);
|
|
|
|
} catch (AppPathNotFoundException $e) {
|
2018-06-26 11:10:49 +03:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
2020-01-22 23:13:36 +03:00
|
|
|
|
|
|
|
$path = $appPath . "/img/$fileName.svg";
|
2018-07-04 11:04:38 +03:00
|
|
|
return $this->getSvg($path, $color, $fileName);
|
2018-06-26 11:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $color
|
2019-06-20 16:23:06 +03:00
|
|
|
* @param string $fileName
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-26 11:10:49 +03:00
|
|
|
*/
|
2018-07-04 11:04:38 +03:00
|
|
|
private function getSvg(string $path, string $color, string $fileName) {
|
2018-06-25 12:17:00 +03:00
|
|
|
if (!file_exists($path)) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$svg = file_get_contents($path);
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
if ($svg === null) {
|
2018-06-25 12:17:00 +03:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
$svg = $this->iconsCacher->colorizeSvg($svg, $color);
|
2018-06-25 12:17:00 +03:00
|
|
|
|
|
|
|
$response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
|
|
|
|
|
|
|
// Set cache control
|
|
|
|
$ttl = 31536000;
|
2018-06-29 17:12:47 +03:00
|
|
|
$response->cacheFor($ttl);
|
2018-06-25 12:17:00 +03:00
|
|
|
$response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"');
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT' . $ttl . 'S'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2018-07-25 13:05:18 +03:00
|
|
|
}
|