Completed
Push — master ( af3ab1...c8340a )
by Morris
21:16 queued 06:37
created

JsController::getFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright 2017, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Roeland Jago Douma <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
namespace OC\Core\Controller;
27
28
use OC\Files\AppData\Factory;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\NotFoundResponse;
32
use OCP\AppFramework\Http\FileDisplayResponse;
33
use OCP\AppFramework\Http\Response;
34
use OCP\AppFramework\Utility\ITimeFactory;
35
use OCP\Files\IAppData;
36
use OCP\Files\NotFoundException;
37
use OCP\Files\SimpleFS\ISimpleFile;
38
use OCP\Files\SimpleFS\ISimpleFolder;
39
use OCP\IRequest;
40
41 View Code Duplication
class JsController 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...
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