Completed
Pull Request — master (#4070)
by Roeland
13:01
created

JsController::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 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Core\Controller;
24
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\NotFoundResponse;
28
use OCP\AppFramework\Http\FileDisplayResponse;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\Files\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\SimpleFS\ISimpleFile;
33
use OCP\Files\SimpleFS\ISimpleFolder;
34
use OCP\IRequest;
35
36 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...
37
38
	/** @var IAppData */
39
	protected $appData;
40
41
	/** @var ITimeFactory */
42
	protected $timeFactory;
43
44
	/**
45
	 * @param string $appName
46
	 * @param IRequest $request
47
	 * @param IAppData $appData
48
	 * @param ITimeFactory $timeFactory
49
	 */
50
	public function __construct($appName, IRequest $request, IAppData $appData, ITimeFactory $timeFactory) {
51
		parent::__construct($appName, $request);
52
53
		$this->appData = $appData;
54
		$this->timeFactory = $timeFactory;
55
	}
56
57
	/**
58
	 * @PublicPage
59
	 * @NoCSRFRequired
60
	 *
61
	 * @param string $fileName css filename with extension
62
	 * @param string $appName css folder name
63
	 * @return FileDisplayResponse|NotFoundResponse
64
	 */
65
	public function getJs($fileName, $appName) {
66
		try {
67
			$folder = $this->appData->getFolder($appName);
68
			$gzip = false;
69
			$file = $this->getFile($folder, $fileName, $gzip);
70
		} catch(NotFoundException $e) {
71
			return new NotFoundResponse();
72
		}
73
74
		$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
75
		if ($gzip) {
76
			$response->addHeader('Content-Encoding', 'gzip');
77
		}
78
		$response->cacheFor(86400);
79
		$expires = new \DateTime();
80
		$expires->setTimestamp($this->timeFactory->getTime());
81
		$expires->add(new \DateInterval('PT24H'));
82
		$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
83
		$response->addHeader('Pragma', 'cache');
84
		return $response;
85
	}
86
87
	/**
88
	 * @param ISimpleFolder $folder
89
	 * @param string $fileName
90
	 * @param bool $gzip is set to true if we use the gzip file
91
	 * @return ISimpleFile
92
	 */
93
	private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
94
		$encoding = $this->request->getHeader('Accept-Encoding');
95
96
		if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
97
			try {
98
				$gzip = true;
99
				return $folder->getFile($fileName . '.gz');
100
			} catch (NotFoundException $e) {
101
				// continue
102
			}
103
		}
104
105
		$gzip = false;
106
		return $folder->getFile($fileName);
107
	}
108
}
109