Completed
Pull Request — master (#4010)
by Joas
29:40 queued 15:51
created
apps/workflowengine/lib/Check/FileMimeType.php 1 patch
Indentation   +155 added lines, -155 removed lines patch added patch discarded remove patch
@@ -29,159 +29,159 @@
 block discarded – undo
29 29
 
30 30
 class FileMimeType extends AbstractStringCheck {
31 31
 
32
-	/** @var array */
33
-	protected $mimeType;
34
-
35
-	/** @var IRequest */
36
-	protected $request;
37
-
38
-	/** @var IMimeTypeDetector */
39
-	protected $mimeTypeDetector;
40
-
41
-	/** @var IStorage */
42
-	protected $storage;
43
-
44
-	/** @var string */
45
-	protected $path;
46
-
47
-	/**
48
-	 * @param IL10N $l
49
-	 * @param IRequest $request
50
-	 * @param IMimeTypeDetector $mimeTypeDetector
51
-	 */
52
-	public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
53
-		parent::__construct($l);
54
-		$this->request = $request;
55
-		$this->mimeTypeDetector = $mimeTypeDetector;
56
-	}
57
-
58
-	/**
59
-	 * @param IStorage $storage
60
-	 * @param string $path
61
-	 */
62
-	public function setFileInfo(IStorage $storage, $path) {
63
-		$this->storage = $storage;
64
-		$this->path = $path;
65
-		if (!isset($this->mimeType[$this->storage->getId()][$this->path])
66
-			|| $this->mimeType[$this->storage->getId()][$this->path] === '') {
67
-			$this->mimeType[$this->storage->getId()][$this->path] = null;
68
-		}
69
-	}
70
-
71
-	/**
72
-	 * @return string
73
-	 */
74
-	protected function getActualValue() {
75
-		if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
76
-			return $this->mimeType[$this->storage->getId()][$this->path];
77
-		}
78
-
79
-		if ($this->isWebDAVRequest()) {
80
-			// Creating a folder
81
-			if ($this->request->getMethod() === 'MKCOL') {
82
-				$this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
83
-				return $this->mimeType[$this->storage->getId()][$this->path];
84
-			}
85
-
86
-			if ($this->request->getMethod() === 'PUT') {
87
-				$path = $this->request->getPathInfo();
88
-				$this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path);
89
-				return $this->mimeType[$this->storage->getId()][$this->path];
90
-			}
91
-		} else if ($this->isPublicWebDAVRequest()) {
92
-			if ($this->request->getMethod() === 'PUT') {
93
-				$path = $this->request->getPathInfo();
94
-				if (strpos($path, '/webdav/') === 0) {
95
-					$path = substr($path, strlen('/webdav'));
96
-				}
97
-				$path = $this->path . $path;
98
-				$this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path);
99
-				return $this->mimeType[$this->storage->getId()][$path];
100
-			}
101
-		}
102
-
103
-		if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
104
-			$files = $this->request->getUploadedFile('files');
105
-			if (isset($files['type'][0])) {
106
-				$mimeType = $files['type'][0];
107
-				if ($this->mimeType === 'application/octet-stream') {
108
-					// Maybe not...
109
-					$mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]);
110
-					if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
111
-						$mimeType = $mimeTypeTest;
112
-					} else {
113
-						$mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]);
114
-						if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
115
-							$mimeType = $mimeTypeTest;
116
-						}
117
-					}
118
-				}
119
-				$this->mimeType[$this->storage->getId()][$this->path] = $mimeType;
120
-				return $mimeType;
121
-			}
122
-		}
123
-
124
-		$this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path);
125
-		if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') {
126
-			$this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath();
127
-		}
128
-
129
-		return $this->mimeType[$this->storage->getId()][$this->path];
130
-	}
131
-
132
-	/**
133
-	 * @return string
134
-	 */
135
-	protected function detectMimetypeFromPath() {
136
-		$mimeType = $this->mimeTypeDetector->detectPath($this->path);
137
-		if ($mimeType !== 'application/octet-stream' && $mimeType !== false) {
138
-			return $mimeType;
139
-		}
140
-
141
-		if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local')
142
-			|| $this->storage->instanceOfStorage('\OC\Files\Storage\Home')
143
-			|| $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) {
144
-			$localFile = $this->storage->getLocalFile($this->path);
145
-			if ($localFile !== false) {
146
-				$mimeType = $this->mimeTypeDetector->detect($localFile);
147
-				if ($mimeType !== false) {
148
-					return $mimeType;
149
-				}
150
-			}
151
-
152
-			return 'application/octet-stream';
153
-		} else {
154
-			$handle = $this->storage->fopen($this->path, 'r');
155
-			$data = fread($handle, 8024);
156
-			fclose($handle);
157
-			$mimeType = $this->mimeTypeDetector->detectString($data);
158
-			if ($mimeType !== false) {
159
-				return $mimeType;
160
-			}
161
-
162
-			return 'application/octet-stream';
163
-		}
164
-	}
165
-
166
-	/**
167
-	 * @return bool
168
-	 */
169
-	protected function isWebDAVRequest() {
170
-		return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
171
-			$this->request->getPathInfo() === '/webdav' ||
172
-			strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
173
-			$this->request->getPathInfo() === '/dav/files' ||
174
-			strpos($this->request->getPathInfo(), '/dav/files/') === 0
175
-		);
176
-	}
177
-
178
-	/**
179
-	 * @return bool
180
-	 */
181
-	protected function isPublicWebDAVRequest() {
182
-		return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && (
183
-			$this->request->getPathInfo() === '/webdav' ||
184
-			strpos($this->request->getPathInfo(), '/webdav/') === 0
185
-		);
186
-	}
32
+    /** @var array */
33
+    protected $mimeType;
34
+
35
+    /** @var IRequest */
36
+    protected $request;
37
+
38
+    /** @var IMimeTypeDetector */
39
+    protected $mimeTypeDetector;
40
+
41
+    /** @var IStorage */
42
+    protected $storage;
43
+
44
+    /** @var string */
45
+    protected $path;
46
+
47
+    /**
48
+     * @param IL10N $l
49
+     * @param IRequest $request
50
+     * @param IMimeTypeDetector $mimeTypeDetector
51
+     */
52
+    public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
53
+        parent::__construct($l);
54
+        $this->request = $request;
55
+        $this->mimeTypeDetector = $mimeTypeDetector;
56
+    }
57
+
58
+    /**
59
+     * @param IStorage $storage
60
+     * @param string $path
61
+     */
62
+    public function setFileInfo(IStorage $storage, $path) {
63
+        $this->storage = $storage;
64
+        $this->path = $path;
65
+        if (!isset($this->mimeType[$this->storage->getId()][$this->path])
66
+            || $this->mimeType[$this->storage->getId()][$this->path] === '') {
67
+            $this->mimeType[$this->storage->getId()][$this->path] = null;
68
+        }
69
+    }
70
+
71
+    /**
72
+     * @return string
73
+     */
74
+    protected function getActualValue() {
75
+        if ($this->mimeType[$this->storage->getId()][$this->path] !== null) {
76
+            return $this->mimeType[$this->storage->getId()][$this->path];
77
+        }
78
+
79
+        if ($this->isWebDAVRequest()) {
80
+            // Creating a folder
81
+            if ($this->request->getMethod() === 'MKCOL') {
82
+                $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory';
83
+                return $this->mimeType[$this->storage->getId()][$this->path];
84
+            }
85
+
86
+            if ($this->request->getMethod() === 'PUT') {
87
+                $path = $this->request->getPathInfo();
88
+                $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path);
89
+                return $this->mimeType[$this->storage->getId()][$this->path];
90
+            }
91
+        } else if ($this->isPublicWebDAVRequest()) {
92
+            if ($this->request->getMethod() === 'PUT') {
93
+                $path = $this->request->getPathInfo();
94
+                if (strpos($path, '/webdav/') === 0) {
95
+                    $path = substr($path, strlen('/webdav'));
96
+                }
97
+                $path = $this->path . $path;
98
+                $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path);
99
+                return $this->mimeType[$this->storage->getId()][$path];
100
+            }
101
+        }
102
+
103
+        if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
104
+            $files = $this->request->getUploadedFile('files');
105
+            if (isset($files['type'][0])) {
106
+                $mimeType = $files['type'][0];
107
+                if ($this->mimeType === 'application/octet-stream') {
108
+                    // Maybe not...
109
+                    $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]);
110
+                    if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
111
+                        $mimeType = $mimeTypeTest;
112
+                    } else {
113
+                        $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]);
114
+                        if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) {
115
+                            $mimeType = $mimeTypeTest;
116
+                        }
117
+                    }
118
+                }
119
+                $this->mimeType[$this->storage->getId()][$this->path] = $mimeType;
120
+                return $mimeType;
121
+            }
122
+        }
123
+
124
+        $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path);
125
+        if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') {
126
+            $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath();
127
+        }
128
+
129
+        return $this->mimeType[$this->storage->getId()][$this->path];
130
+    }
131
+
132
+    /**
133
+     * @return string
134
+     */
135
+    protected function detectMimetypeFromPath() {
136
+        $mimeType = $this->mimeTypeDetector->detectPath($this->path);
137
+        if ($mimeType !== 'application/octet-stream' && $mimeType !== false) {
138
+            return $mimeType;
139
+        }
140
+
141
+        if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local')
142
+            || $this->storage->instanceOfStorage('\OC\Files\Storage\Home')
143
+            || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) {
144
+            $localFile = $this->storage->getLocalFile($this->path);
145
+            if ($localFile !== false) {
146
+                $mimeType = $this->mimeTypeDetector->detect($localFile);
147
+                if ($mimeType !== false) {
148
+                    return $mimeType;
149
+                }
150
+            }
151
+
152
+            return 'application/octet-stream';
153
+        } else {
154
+            $handle = $this->storage->fopen($this->path, 'r');
155
+            $data = fread($handle, 8024);
156
+            fclose($handle);
157
+            $mimeType = $this->mimeTypeDetector->detectString($data);
158
+            if ($mimeType !== false) {
159
+                return $mimeType;
160
+            }
161
+
162
+            return 'application/octet-stream';
163
+        }
164
+    }
165
+
166
+    /**
167
+     * @return bool
168
+     */
169
+    protected function isWebDAVRequest() {
170
+        return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
171
+            $this->request->getPathInfo() === '/webdav' ||
172
+            strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
173
+            $this->request->getPathInfo() === '/dav/files' ||
174
+            strpos($this->request->getPathInfo(), '/dav/files/') === 0
175
+        );
176
+    }
177
+
178
+    /**
179
+     * @return bool
180
+     */
181
+    protected function isPublicWebDAVRequest() {
182
+        return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && (
183
+            $this->request->getPathInfo() === '/webdav' ||
184
+            strpos($this->request->getPathInfo(), '/webdav/') === 0
185
+        );
186
+    }
187 187
 }
Please login to merge, or discard this patch.