Passed
Push — master ( 482b42...fe08f4 )
by Morris
14:49 queued 04:12
created
apps/workflowengine/lib/Check/FileMimeType.php 1 patch
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -29,144 +29,144 @@
 block discarded – undo
29 29
 use OCP\WorkflowEngine\IFileCheck;
30 30
 
31 31
 class FileMimeType extends AbstractStringCheck implements IFileCheck {
32
-	use TFileCheck {
33
-		setFileInfo as _setFileInfo;
34
-	}
35
-
36
-	/** @var array */
37
-	protected $mimeType;
38
-
39
-	/** @var IRequest */
40
-	protected $request;
41
-
42
-	/** @var IMimeTypeDetector */
43
-	protected $mimeTypeDetector;
44
-
45
-	/**
46
-	 * @param IL10N $l
47
-	 * @param IRequest $request
48
-	 * @param IMimeTypeDetector $mimeTypeDetector
49
-	 */
50
-	public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
51
-		parent::__construct($l);
52
-		$this->request = $request;
53
-		$this->mimeTypeDetector = $mimeTypeDetector;
54
-	}
55
-
56
-	/**
57
-	 * @param IStorage $storage
58
-	 * @param string $path
59
-	 * @param bool $isDir
60
-	 */
61
-	public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
62
-		$this->_setFileInfo($storage, $path, $isDir);
63
-		if (!isset($this->mimeType[$this->storage->getId()][$this->path])
64
-			|| $this->mimeType[$this->storage->getId()][$this->path] === '') {
65
-			if ($isDir) {
66
-				$this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
67
-			} else {
68
-				$this->mimeType[$this->storage->getId()][$this->path] = null;
69
-			}
70
-		}
71
-	}
72
-
73
-	/**
74
-	 * The mimetype is only cached if the file exists. Otherwise files access
75
-	 * control will cache "application/octet-stream" for all the target node on:
76
-	 * rename, move, copy and all other methods which create a new item
77
-	 *
78
-	 * To check this:
79
-	 * 1. Add an automated tagging rule which tags on mimetype NOT "httpd/unix-directory"
80
-	 * 2. Add an access control rule which checks for any mimetype
81
-	 * 3. Create a folder and rename it, the folder should not be tagged, but it is
82
-	 *
83
-	 * @param string $storageId
84
-	 * @param string|null $path
85
-	 * @param string $mimeType
86
-	 * @return string
87
-	 */
88
-	protected function cacheAndReturnMimeType(string $storageId, ?string $path, string $mimeType): string {
89
-		if ($path !== null && $this->storage->file_exists($path)) {
90
-			$this->mimeType[$storageId][$path] = $mimeType;
91
-		}
92
-
93
-		return $mimeType;
94
-	}
95
-
96
-	/**
97
-	 * Make sure that even though the content based check returns an application/octet-stream can still be checked based on mimetypemappings of their extension
98
-	 *
99
-	 * @param string $operator
100
-	 * @param string $value
101
-	 * @return bool
102
-	 */
103
-	public function executeCheck($operator, $value) {
104
-		$actualValue = $this->getActualValue();
105
-		return $this->executeStringCheck($operator, $value, $actualValue) ||
106
-			$this->executeStringCheck($operator, $value, $this->mimeTypeDetector->detectPath($this->path));
107
-	}
108
-
109
-	/**
110
-	 * @return string
111
-	 */
112
-	protected function getActualValue() {
113
-		if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
114
-			return $this->mimeType[$this->storage->getId()][$this->path];
115
-		}
116
-
117
-		if ($this->storage->is_dir($this->path)) {
118
-			return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
119
-		}
120
-
121
-		if ($this->storage->file_exists($this->path)) {
122
-			$path = $this->storage->getLocalFile($this->path);
123
-			$mimeType = $this->mimeTypeDetector->detectContent($path);
124
-			return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
125
-		}
126
-
127
-		if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) {
128
-			// Creating a folder
129
-			if ($this->request->getMethod() === 'MKCOL') {
130
-				return 'httpd/unix-directory';
131
-			}
132
-		}
133
-
134
-		// We do not cache this, as the file did not exist yet.
135
-		// In case it does in the future, we will check with detectContent()
136
-		// again to get the real mimetype of the content, rather than
137
-		// guessing it from the path.
138
-		return $this->mimeTypeDetector->detectPath($this->path);
139
-	}
140
-
141
-	/**
142
-	 * @return bool
143
-	 */
144
-	protected function isWebDAVRequest() {
145
-		return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
146
-			$this->request->getPathInfo() === '/webdav' ||
147
-			strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
148
-			$this->request->getPathInfo() === '/dav/files' ||
149
-			strpos($this->request->getPathInfo(), '/dav/files/') === 0 ||
150
-			$this->request->getPathInfo() === '/dav/uploads' ||
151
-			strpos($this->request->getPathInfo(), '/dav/uploads/') === 0
152
-		);
153
-	}
154
-
155
-	/**
156
-	 * @return bool
157
-	 */
158
-	protected function isPublicWebDAVRequest() {
159
-		return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && (
160
-			$this->request->getPathInfo() === '/webdav' ||
161
-			strpos($this->request->getPathInfo(), '/webdav/') === 0
162
-		);
163
-	}
164
-
165
-	public function supportedEntities(): array {
166
-		return [ File::class ];
167
-	}
168
-
169
-	public function isAvailableForScope(int $scope): bool {
170
-		return true;
171
-	}
32
+    use TFileCheck {
33
+        setFileInfo as _setFileInfo;
34
+    }
35
+
36
+    /** @var array */
37
+    protected $mimeType;
38
+
39
+    /** @var IRequest */
40
+    protected $request;
41
+
42
+    /** @var IMimeTypeDetector */
43
+    protected $mimeTypeDetector;
44
+
45
+    /**
46
+     * @param IL10N $l
47
+     * @param IRequest $request
48
+     * @param IMimeTypeDetector $mimeTypeDetector
49
+     */
50
+    public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
51
+        parent::__construct($l);
52
+        $this->request = $request;
53
+        $this->mimeTypeDetector = $mimeTypeDetector;
54
+    }
55
+
56
+    /**
57
+     * @param IStorage $storage
58
+     * @param string $path
59
+     * @param bool $isDir
60
+     */
61
+    public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void {
62
+        $this->_setFileInfo($storage, $path, $isDir);
63
+        if (!isset($this->mimeType[$this->storage->getId()][$this->path])
64
+            || $this->mimeType[$this->storage->getId()][$this->path] === '') {
65
+            if ($isDir) {
66
+                $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
67
+            } else {
68
+                $this->mimeType[$this->storage->getId()][$this->path] = null;
69
+            }
70
+        }
71
+    }
72
+
73
+    /**
74
+     * The mimetype is only cached if the file exists. Otherwise files access
75
+     * control will cache "application/octet-stream" for all the target node on:
76
+     * rename, move, copy and all other methods which create a new item
77
+     *
78
+     * To check this:
79
+     * 1. Add an automated tagging rule which tags on mimetype NOT "httpd/unix-directory"
80
+     * 2. Add an access control rule which checks for any mimetype
81
+     * 3. Create a folder and rename it, the folder should not be tagged, but it is
82
+     *
83
+     * @param string $storageId
84
+     * @param string|null $path
85
+     * @param string $mimeType
86
+     * @return string
87
+     */
88
+    protected function cacheAndReturnMimeType(string $storageId, ?string $path, string $mimeType): string {
89
+        if ($path !== null && $this->storage->file_exists($path)) {
90
+            $this->mimeType[$storageId][$path] = $mimeType;
91
+        }
92
+
93
+        return $mimeType;
94
+    }
95
+
96
+    /**
97
+     * Make sure that even though the content based check returns an application/octet-stream can still be checked based on mimetypemappings of their extension
98
+     *
99
+     * @param string $operator
100
+     * @param string $value
101
+     * @return bool
102
+     */
103
+    public function executeCheck($operator, $value) {
104
+        $actualValue = $this->getActualValue();
105
+        return $this->executeStringCheck($operator, $value, $actualValue) ||
106
+            $this->executeStringCheck($operator, $value, $this->mimeTypeDetector->detectPath($this->path));
107
+    }
108
+
109
+    /**
110
+     * @return string
111
+     */
112
+    protected function getActualValue() {
113
+        if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
114
+            return $this->mimeType[$this->storage->getId()][$this->path];
115
+        }
116
+
117
+        if ($this->storage->is_dir($this->path)) {
118
+            return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory');
119
+        }
120
+
121
+        if ($this->storage->file_exists($this->path)) {
122
+            $path = $this->storage->getLocalFile($this->path);
123
+            $mimeType = $this->mimeTypeDetector->detectContent($path);
124
+            return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType);
125
+        }
126
+
127
+        if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) {
128
+            // Creating a folder
129
+            if ($this->request->getMethod() === 'MKCOL') {
130
+                return 'httpd/unix-directory';
131
+            }
132
+        }
133
+
134
+        // We do not cache this, as the file did not exist yet.
135
+        // In case it does in the future, we will check with detectContent()
136
+        // again to get the real mimetype of the content, rather than
137
+        // guessing it from the path.
138
+        return $this->mimeTypeDetector->detectPath($this->path);
139
+    }
140
+
141
+    /**
142
+     * @return bool
143
+     */
144
+    protected function isWebDAVRequest() {
145
+        return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
146
+            $this->request->getPathInfo() === '/webdav' ||
147
+            strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
148
+            $this->request->getPathInfo() === '/dav/files' ||
149
+            strpos($this->request->getPathInfo(), '/dav/files/') === 0 ||
150
+            $this->request->getPathInfo() === '/dav/uploads' ||
151
+            strpos($this->request->getPathInfo(), '/dav/uploads/') === 0
152
+        );
153
+    }
154
+
155
+    /**
156
+     * @return bool
157
+     */
158
+    protected function isPublicWebDAVRequest() {
159
+        return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && (
160
+            $this->request->getPathInfo() === '/webdav' ||
161
+            strpos($this->request->getPathInfo(), '/webdav/') === 0
162
+        );
163
+    }
164
+
165
+    public function supportedEntities(): array {
166
+        return [ File::class ];
167
+    }
168
+
169
+    public function isAvailableForScope(int $scope): bool {
170
+        return true;
171
+    }
172 172
 }
Please login to merge, or discard this patch.