Code Duplication    Length = 73-73 lines in 2 locations

core/Controller/CssController.php 1 location

@@ 35-107 (lines=73) @@
32
use OCP\Files\SimpleFS\ISimpleFolder;
33
use OCP\IRequest;
34
35
class CssController extends Controller {
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

core/Controller/JsController.php 1 location

@@ 36-108 (lines=73) @@
33
use OCP\Files\SimpleFS\ISimpleFolder;
34
use OCP\IRequest;
35
36
class JsController extends Controller {
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