Code Duplication    Length = 78-81 lines in 2 locations

core/Controller/CssController.php 1 location

@@ 44-124 (lines=81) @@
41
use OCP\IConfig;
42
use OCP\IRequest;
43
44
class CssController extends Controller {
45
46
	/** @var IAppData */
47
	protected $appData;
48
49
	/** @var ITimeFactory */
50
	protected $timeFactory;
51
52
	public function __construct(string $appName,
53
								IRequest $request,
54
								Factory $appDataFactory,
55
								ITimeFactory $timeFactory) {
56
		parent::__construct($appName, $request);
57
58
		$this->appData = $appDataFactory->get('css');
59
		$this->timeFactory = $timeFactory;
60
	}
61
62
	/**
63
	 * @PublicPage
64
	 * @NoCSRFRequired
65
	 *
66
	 * @param string $fileName css filename with extension
67
	 * @param string $appName css folder name
68
	 * @return FileDisplayResponse|NotFoundResponse
69
	 */
70
	public function getCss(string $fileName, string $appName): Response {
71
		try {
72
			$folder = $this->appData->getFolder($appName);
73
			$gzip = false;
74
			$file = $this->getFile($folder, $fileName, $gzip);
75
		} catch(NotFoundException $e) {
76
			return new NotFoundResponse();
77
		}
78
79
		$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
80
		if ($gzip) {
81
			$response->addHeader('Content-Encoding', 'gzip');
82
		}
83
84
		$ttl = 31536000;
85
		$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
86
87
		$expires = new \DateTime();
88
		$expires->setTimestamp($this->timeFactory->getTime());
89
		$expires->add(new \DateInterval('PT'.$ttl.'S'));
90
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
91
		$response->addHeader('Pragma', 'cache');
92
		return $response;
93
	}
94
95
	/**
96
	 * @param ISimpleFolder $folder
97
	 * @param string $fileName
98
	 * @param bool $gzip is set to true if we use the gzip file
99
	 * @return ISimpleFile
100
	 * @throws NotFoundException
101
	 */
102
	private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
103
		$encoding = $this->request->getHeader('Accept-Encoding');
104
105
		if (strpos($encoding, 'gzip') !== false) {
106
			try {
107
				$gzip = true;
108
				return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
109
			} catch (NotFoundException $e) {
110
				// continue
111
			}
112
		}
113
114
		$gzip = false;
115
		return $folder->getFile($fileName);
116
	}
117
}
118

core/Controller/JsController.php 1 location

@@ 41-118 (lines=78) @@
38
use OCP\Files\SimpleFS\ISimpleFolder;
39
use OCP\IRequest;
40
41
class JsController extends Controller {
42
43
	/** @var IAppData */
44
	protected $appData;
45
46
	/** @var ITimeFactory */
47
	protected $timeFactory;
48
49
	public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
50
		parent::__construct($appName, $request);
51
52
		$this->appData = $appDataFactory->get('js');
53
		$this->timeFactory = $timeFactory;
54
	}
55
56
	/**
57
	 * @PublicPage
58
	 * @NoCSRFRequired
59
	 *
60
	 * @param string $fileName css filename with extension
61
	 * @param string $appName css folder name
62
	 * @return FileDisplayResponse|NotFoundResponse
63
	 */
64
	public function getJs(string $fileName, string $appName): Response {
65
		try {
66
			$folder = $this->appData->getFolder($appName);
67
			$gzip = false;
68
			$file = $this->getFile($folder, $fileName, $gzip);
69
		} catch(NotFoundException $e) {
70
			return new NotFoundResponse();
71
		}
72
73
		$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
74
		if ($gzip) {
75
			$response->addHeader('Content-Encoding', 'gzip');
76
		}
77
78
		$ttl = 31536000;
79
		$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
80
81
		$expires = new \DateTime();
82
		$expires->setTimestamp($this->timeFactory->getTime());
83
		$expires->add(new \DateInterval('PT'.$ttl.'S'));
84
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
85
		$response->addHeader('Pragma', 'cache');
86
		return $response;
87
	}
88
89
	/**
90
	 * @param ISimpleFolder $folder
91
	 * @param string $fileName
92
	 * @param bool $gzip is set to true if we use the gzip file
93
	 * @return ISimpleFile
94
	 *
95
	 * @throws NotFoundException
96
	 */
97
	private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
98
		$encoding = $this->request->getHeader('Accept-Encoding');
99
100
		if (strpos($encoding, 'gzip') !== false) {
101
			try {
102
				$gzip = true;
103
				return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
104
			} catch (NotFoundException $e) {
105
				// continue
106
			}
107
		}
108
109
		$gzip = false;
110
		return $folder->getFile($fileName);
111
	}
112
}
113