Completed
Push — master ( 2c682a...16b8c0 )
by Morris
16:33 queued 16:14
created

CssController::getFile()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 3
dl 15
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, John Molakvoæ ([email protected])
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Core\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\NotFoundResponse;
27
use OCP\AppFramework\Http\FileDisplayResponse;
28
use OCP\AppFramework\Utility\ITimeFactory;
29
use OCP\Files\IAppData;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\SimpleFS\ISimpleFile;
32
use OCP\Files\SimpleFS\ISimpleFolder;
33
use OCP\IRequest;
34
35 View Code Duplication
class CssController extends Controller {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
37
	/** @var IAppData */
38
	protected $appData;
39
40
	/** @var ITimeFactory */
41
	protected $timeFactory;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param IAppData $appData
47
	 * @param ITimeFactory $timeFactory
48
	 */
49
	public function __construct($appName, IRequest $request, IAppData $appData, ITimeFactory $timeFactory) {
50
		parent::__construct($appName, $request);
51
52
		$this->appData = $appData;
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 getCss($fileName, $appName) {
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' => 'text/css']);
74
		if ($gzip) {
75
			$response->addHeader('Content-Encoding', 'gzip');
76
		}
77
		$response->cacheFor(86400);
78
		$expires = new \DateTime();
79
		$expires->setTimestamp($this->timeFactory->getTime());
80
		$expires->add(new \DateInterval('PT24H'));
81
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
82
		$response->addHeader('Pragma', 'cache');
83
		return $response;
84
	}
85
86
	/**
87
	 * @param ISimpleFolder $folder
88
	 * @param string $fileName
89
	 * @param bool $gzip is set to true if we use the gzip file
90
	 * @return ISimpleFile
91
	 */
92
	private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
93
		$encoding = $this->request->getHeader('Accept-Encoding');
94
95
		if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
96
			try {
97
				$gzip = true;
98
				return $folder->getFile($fileName . '.gz');
99
			} catch (NotFoundException $e) {
100
				// continue
101
			}
102
		}
103
104
		$gzip = false;
105
		return $folder->getFile($fileName);
106
	}
107
}
108