From 3a7d6846facec4e03966ab8e03f0fcbd946a8ef0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?=
 <skjnldsv@protonmail.com>
Date: Wed, 9 Nov 2016 11:18:43 +0100
Subject: [PATCH] Appdata integration 1 & log fix 2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
---
 lib/private/Template/CSSResourceLocator.php | 12 ++++-
 lib/private/Template/ResourceLocator.php    |  6 +--
 lib/private/Template/SCSSCacher.php         | 49 ++++++++++++++-------
 lib/private/TemplateLayout.php              |  3 +-
 4 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/lib/private/Template/CSSResourceLocator.php b/lib/private/Template/CSSResourceLocator.php
index 353555a6811..2e1db9a6757 100755
--- a/lib/private/Template/CSSResourceLocator.php
+++ b/lib/private/Template/CSSResourceLocator.php
@@ -26,14 +26,22 @@
 namespace OC\Template;
 
 class CSSResourceLocator extends ResourceLocator {
+
+	protected $appData;
+
+	public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map, $appData) {
+		$this->appData = $appData;
+		parent::__construct($logger, $theme, $core_map, $party_map);
+	}
+
 	/**
 	 * @param string $style
 	 */
 	public function doFind($style) {
 		if (strpos($style, '3rdparty') === 0
 			&& $this->appendIfExist($this->thirdpartyroot, $style.'.css')
-			|| $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss')
-			|| $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss')
+			|| $this->cacheAndAppendScssIfExist($this->serverroot, $style.'.scss', $this->appData)
+			|| $this->cacheAndAppendScssIfExist($this->serverroot, 'core/'.$style.'.scss', $this->appData)
 			|| $this->appendIfExist($this->serverroot, $style.'.css')
 			|| $this->appendIfExist($this->serverroot, 'core/'.$style.'.css')
 		) {
diff --git a/lib/private/Template/ResourceLocator.php b/lib/private/Template/ResourceLocator.php
index 37360a98d91..89a3a1b025b 100755
--- a/lib/private/Template/ResourceLocator.php
+++ b/lib/private/Template/ResourceLocator.php
@@ -114,16 +114,16 @@ abstract class ResourceLocator {
 	 * @param string|null $webRoot base for path, default map $root to $webRoot
 	 * @return bool True if the resource was found and cached, false otherwise
 	 */
-	protected function cacheAndAppendScssIfExist($root, $file, $webRoot = null) {
+	protected function cacheAndAppendScssIfExist($root, $file, $appData, $webRoot = null) {
 		if (is_file($root.'/'.$file)) {
 			$scssCache = new \OC\Template\SCSSCacher(
 				$this->logger,
 				$root,
-				$file);
+				$file,
+				$appData);
 
 			if($scssCache->process()) {
 				$this->append($root, $scssCache->getCachedSCSS(), $webRoot, false);
-				$this->logger->debug($root.'/'.$file.' compiled and successfully cached', ['app' => 'SCSSPHP']);
 				return true;
 			} else {
 				$this->logger->error('Failed to compile and/or save '.$root.'/'.$file, ['app' => 'SCSSPHP']);
diff --git a/lib/private/Template/SCSSCacher.php b/lib/private/Template/SCSSCacher.php
index 50e6eb4851d..43160a9c169 100755
--- a/lib/private/Template/SCSSCacher.php
+++ b/lib/private/Template/SCSSCacher.php
@@ -23,46 +23,53 @@ namespace OC\Template;
 
 use Leafo\ScssPhp\Compiler;
 use Leafo\ScssPhp\Exception\ParserException;
+use OCP\Files\NotFoundException;
 
 class SCSSCacher {
 
 	protected $root;
+	protected $folder;
 	protected $file;
 	protected $fileName;
 	protected $fileLoc;
 	protected $fileCache;
 	protected $rootCssLoc;
 
-	/** Cache folder from serverroot */
-	private $scssCache = "assets";
-
-
 	/** @var \OCP\ILogger */
 	protected $logger;
+	protected $appData;
 
 	/**
 	 * @param \OCP\ILogger $logger
 	 * @param string $root
 	 * @param string $file
 	 */
-	public function __construct(\OCP\ILogger $logger, $root, $file) {
+	public function __construct(\OCP\ILogger $logger, $root, $file, $appData) {
 		$this->logger = $logger;
+		$this->appData = $appData;
 		$this->root = $root;
 		$this->file = explode('/', $root.'/'.$file);
 
-		$this->fileName = array_pop($this->file);
+		$this->fileNameSCSS = array_pop($this->file);
+		$this->fileNameCSS = str_replace('.scss', '.css', $this->fileNameSCSS);
 		$this->fileLoc = implode('/', $this->file);
-		$this->fileCache = str_replace('.scss', '.css', $this->scssCache.'/'.$this->fileName);
 
 		// base uri to css file
 		$this->rootCssLoc = explode('/', $file);
 		array_pop($this->rootCssLoc);
 		$this->rootCssLoc = implode('/', $this->rootCssLoc);
+
+		try {
+			$this->folder = $this->appData->getFolder('css');
+		} catch(NotFoundException $e) {
+			// creating css appdata folder
+			$this->folder = $this->appData->newFolder('css');
+		}
 	}
 
 	public function process() {
 
-		if($this->is_cached($this->root.'/'.$this->fileCache, $this->fileLoc.'/'.$this->fileName)) {
+		if($this->is_cached()) {
 			return true;
 		} else {
 			return $this->cache();
@@ -70,33 +77,45 @@ class SCSSCacher {
 		return false;
 	}
 
-	private function is_cached($in, $out) {
-		if (! is_file($out) || filemtime($in) > filemtime($out)) {
-            return true;
-        }
+	private function is_cached() {
+		try{
+			$cachedfile = $this->folder->getFile($this->fileNameCSS);
+			if( $cachedfile->getMTime() > filemtime($this->fileLoc.'/'.$this->fileNameSCSS)
+				&& $cachedfile->getSize() > 0 ) {
+				return true;
+			}
+		} catch(NotFoundException $e) {
+			return false;
+		}
         return false;
 	}
 
 	private function cache() {
 		$scss = new Compiler();
 		$scss->setImportPaths($this->fileLoc);
-
 		if(\OC::$server->getSystemConfig()->getValue('debug')) {
 			// Debug mode
 			$scss->setFormatter('Leafo\ScssPhp\Formatter\Expanded');
 			$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
 		} else {
+			// Compression
 			$scss->setFormatter('Leafo\ScssPhp\Formatter\Crunched');
 		}
 
 		try {
-			$compiledScss = $scss->compile('@import "'.$this->fileName.'";');
+			$cachedfile = $this->folder->getFile($this->fileNameCSS);
+		} catch(NotFoundException $e) {
+			$cachedfile = $this->folder->newFile($this->fileNameCSS);
+		}
+
+		try {
+			$compiledScss = $scss->compile('@import "'.$this->fileNameSCSS.'";');
 		} catch(ParserException $e) {
 			$this->logger->error($e, ['app' => 'SCSSPHP']);
 			return false;
 		}
 
-		if(file_put_contents($this->fileCache, $this->rebaseUrls($compiledScss))) {
+		if($cachedfile->putContent($this->rebaseUrls($compiledScss))) {
 			$this->logger->debug($root.'/'.$file.' compiled and successfully cached', ['app' => 'SCSSPHP']);
 			return true;
 		}
diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php
index ad9f8bac0e4..96f612c313c 100644
--- a/lib/private/TemplateLayout.php
+++ b/lib/private/TemplateLayout.php
@@ -188,7 +188,8 @@ class TemplateLayout extends \OC_Template {
 			\OC::$server->getLogger(),
 			$theme,
 			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
-			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
+			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
+			\OC::$server->getAppDataDir('server'));
 		$locator->find($styles);
 		return $locator->getResources();
 	}
-- 
GitLab