Passed
Push — master ( 1e4504...aec8dc )
by Julius
15:38 queued 14s
created
apps/files_sharing/lib/Controller/PublicPreviewController.php 1 patch
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -40,150 +40,150 @@
 block discarded – undo
40 40
 
41 41
 class PublicPreviewController extends PublicShareController {
42 42
 
43
-	/** @var ShareManager */
44
-	private $shareManager;
45
-
46
-	/** @var IPreview */
47
-	private $previewManager;
48
-
49
-	/** @var IShare */
50
-	private $share;
51
-
52
-	public function __construct(string $appName,
53
-								IRequest $request,
54
-								ShareManager $shareManger,
55
-								ISession $session,
56
-								IPreview $previewManager) {
57
-		parent::__construct($appName, $request, $session);
58
-
59
-		$this->shareManager = $shareManger;
60
-		$this->previewManager = $previewManager;
61
-	}
62
-
63
-	protected function getPasswordHash(): string {
64
-		return $this->share->getPassword();
65
-	}
66
-
67
-	public function isValidToken(): bool {
68
-		try {
69
-			$this->share = $this->shareManager->getShareByToken($this->getToken());
70
-			return true;
71
-		} catch (ShareNotFound $e) {
72
-			return false;
73
-		}
74
-	}
75
-
76
-	protected function isPasswordProtected(): bool {
77
-		return $this->share->getPassword() !== null;
78
-	}
79
-
80
-
81
-	/**
82
-	 * @PublicPage
83
-	 * @NoCSRFRequired
84
-	 *
85
-	 * @param string $file
86
-	 * @param int $x
87
-	 * @param int $y
88
-	 * @param bool $a
89
-	 * @return DataResponse|FileDisplayResponse
90
-	 */
91
-	public function getPreview(
92
-		string $token,
93
-		string $file = '',
94
-		int $x = 32,
95
-		int $y = 32,
96
-		$a = false
97
-	) {
98
-		if ($token === '' || $x === 0 || $y === 0) {
99
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
100
-		}
101
-
102
-		try {
103
-			$share = $this->shareManager->getShareByToken($token);
104
-		} catch (ShareNotFound $e) {
105
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
106
-		}
107
-
108
-		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
109
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
110
-		}
111
-
112
-		$attributes = $share->getAttributes();
113
-		if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
114
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
115
-		}
116
-
117
-		try {
118
-			$node = $share->getNode();
119
-			if ($node instanceof Folder) {
120
-				$file = $node->get($file);
121
-			} else {
122
-				$file = $node;
123
-			}
124
-
125
-			$f = $this->previewManager->getPreview($file, $x, $y, !$a);
126
-			$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
127
-			$response->cacheFor(3600 * 24);
128
-			return $response;
129
-		} catch (NotFoundException $e) {
130
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
131
-		} catch (\InvalidArgumentException $e) {
132
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
133
-		}
134
-	}
135
-
136
-	/**
137
-	 * @PublicPage
138
-	 * @NoCSRFRequired
139
-	 * @NoSameSiteCookieRequired
140
-	 *
141
-	 * @param $token
142
-	 * @return DataResponse|FileDisplayResponse
143
-	 */
144
-	public function directLink(string $token) {
145
-		// No token no image
146
-		if ($token === '') {
147
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
148
-		}
149
-
150
-		// No share no image
151
-		try {
152
-			$share = $this->shareManager->getShareByToken($token);
153
-		} catch (ShareNotFound $e) {
154
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
155
-		}
156
-
157
-		// No permissions no image
158
-		if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
159
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
160
-		}
161
-
162
-		// Password protected shares have no direct link!
163
-		if ($share->getPassword() !== null) {
164
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
165
-		}
166
-
167
-		$attributes = $share->getAttributes();
168
-		if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
169
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
170
-		}
171
-
172
-		try {
173
-			$node = $share->getNode();
174
-			if ($node instanceof Folder) {
175
-				// Direct link only works for single files
176
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
177
-			}
178
-
179
-			$f = $this->previewManager->getPreview($node, -1, -1, false);
180
-			$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
181
-			$response->cacheFor(3600 * 24);
182
-			return $response;
183
-		} catch (NotFoundException $e) {
184
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
185
-		} catch (\InvalidArgumentException $e) {
186
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
187
-		}
188
-	}
43
+    /** @var ShareManager */
44
+    private $shareManager;
45
+
46
+    /** @var IPreview */
47
+    private $previewManager;
48
+
49
+    /** @var IShare */
50
+    private $share;
51
+
52
+    public function __construct(string $appName,
53
+                                IRequest $request,
54
+                                ShareManager $shareManger,
55
+                                ISession $session,
56
+                                IPreview $previewManager) {
57
+        parent::__construct($appName, $request, $session);
58
+
59
+        $this->shareManager = $shareManger;
60
+        $this->previewManager = $previewManager;
61
+    }
62
+
63
+    protected function getPasswordHash(): string {
64
+        return $this->share->getPassword();
65
+    }
66
+
67
+    public function isValidToken(): bool {
68
+        try {
69
+            $this->share = $this->shareManager->getShareByToken($this->getToken());
70
+            return true;
71
+        } catch (ShareNotFound $e) {
72
+            return false;
73
+        }
74
+    }
75
+
76
+    protected function isPasswordProtected(): bool {
77
+        return $this->share->getPassword() !== null;
78
+    }
79
+
80
+
81
+    /**
82
+     * @PublicPage
83
+     * @NoCSRFRequired
84
+     *
85
+     * @param string $file
86
+     * @param int $x
87
+     * @param int $y
88
+     * @param bool $a
89
+     * @return DataResponse|FileDisplayResponse
90
+     */
91
+    public function getPreview(
92
+        string $token,
93
+        string $file = '',
94
+        int $x = 32,
95
+        int $y = 32,
96
+        $a = false
97
+    ) {
98
+        if ($token === '' || $x === 0 || $y === 0) {
99
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
100
+        }
101
+
102
+        try {
103
+            $share = $this->shareManager->getShareByToken($token);
104
+        } catch (ShareNotFound $e) {
105
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
106
+        }
107
+
108
+        if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
109
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
110
+        }
111
+
112
+        $attributes = $share->getAttributes();
113
+        if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
114
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
115
+        }
116
+
117
+        try {
118
+            $node = $share->getNode();
119
+            if ($node instanceof Folder) {
120
+                $file = $node->get($file);
121
+            } else {
122
+                $file = $node;
123
+            }
124
+
125
+            $f = $this->previewManager->getPreview($file, $x, $y, !$a);
126
+            $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
127
+            $response->cacheFor(3600 * 24);
128
+            return $response;
129
+        } catch (NotFoundException $e) {
130
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
131
+        } catch (\InvalidArgumentException $e) {
132
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
133
+        }
134
+    }
135
+
136
+    /**
137
+     * @PublicPage
138
+     * @NoCSRFRequired
139
+     * @NoSameSiteCookieRequired
140
+     *
141
+     * @param $token
142
+     * @return DataResponse|FileDisplayResponse
143
+     */
144
+    public function directLink(string $token) {
145
+        // No token no image
146
+        if ($token === '') {
147
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
148
+        }
149
+
150
+        // No share no image
151
+        try {
152
+            $share = $this->shareManager->getShareByToken($token);
153
+        } catch (ShareNotFound $e) {
154
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
155
+        }
156
+
157
+        // No permissions no image
158
+        if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
159
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
160
+        }
161
+
162
+        // Password protected shares have no direct link!
163
+        if ($share->getPassword() !== null) {
164
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
165
+        }
166
+
167
+        $attributes = $share->getAttributes();
168
+        if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
169
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
170
+        }
171
+
172
+        try {
173
+            $node = $share->getNode();
174
+            if ($node instanceof Folder) {
175
+                // Direct link only works for single files
176
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
177
+            }
178
+
179
+            $f = $this->previewManager->getPreview($node, -1, -1, false);
180
+            $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
181
+            $response->cacheFor(3600 * 24);
182
+            return $response;
183
+        } catch (NotFoundException $e) {
184
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
185
+        } catch (\InvalidArgumentException $e) {
186
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
187
+        }
188
+    }
189 189
 }
Please login to merge, or discard this patch.
core/Controller/PreviewController.php 1 patch
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -40,117 +40,117 @@
 block discarded – undo
40 40
 use OCP\IRequest;
41 41
 
42 42
 class PreviewController extends Controller {
43
-	private string $userId;
44
-	private IRootFolder $root;
45
-	private IPreview $preview;
46
-
47
-	public function __construct(string $appName,
48
-								IRequest $request,
49
-								IPreview $preview,
50
-								IRootFolder $root,
51
-								?string $userId
52
-	) {
53
-		parent::__construct($appName, $request);
54
-
55
-		$this->preview = $preview;
56
-		$this->root = $root;
57
-		$this->userId = $userId;
58
-	}
59
-
60
-	/**
61
-	 * @NoAdminRequired
62
-	 * @NoCSRFRequired
63
-	 *
64
-	 * @return DataResponse|FileDisplayResponse
65
-	 */
66
-	public function getPreview(
67
-		string $file = '',
68
-		int $x = 32,
69
-		int $y = 32,
70
-		bool $a = false,
71
-		bool $forceIcon = true,
72
-		string $mode = 'fill'): Http\Response {
73
-		if ($file === '' || $x === 0 || $y === 0) {
74
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
75
-		}
76
-
77
-		try {
78
-			$userFolder = $this->root->getUserFolder($this->userId);
79
-			$node = $userFolder->get($file);
80
-		} catch (NotFoundException $e) {
81
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
82
-		}
83
-
84
-		return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
85
-	}
86
-
87
-	/**
88
-	 * @NoAdminRequired
89
-	 * @NoCSRFRequired
90
-	 *
91
-	 * @return DataResponse|FileDisplayResponse
92
-	 */
93
-	public function getPreviewByFileId(
94
-		int $fileId = -1,
95
-		int $x = 32,
96
-		int $y = 32,
97
-		bool $a = false,
98
-		bool $forceIcon = true,
99
-		string $mode = 'fill') {
100
-		if ($fileId === -1 || $x === 0 || $y === 0) {
101
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
-		}
103
-
104
-		$userFolder = $this->root->getUserFolder($this->userId);
105
-		$nodes = $userFolder->getById($fileId);
106
-
107
-		if (\count($nodes) === 0) {
108
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
109
-		}
110
-
111
-		$node = array_pop($nodes);
112
-
113
-		return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
114
-	}
115
-
116
-	/**
117
-	 * @return DataResponse|FileDisplayResponse
118
-	 */
119
-	private function fetchPreview(
120
-		Node $node,
121
-		int $x,
122
-		int $y,
123
-		bool $a,
124
-		bool $forceIcon,
125
-		string $mode) : Http\Response {
126
-		if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
127
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
128
-		}
129
-		if (!$node->isReadable()) {
130
-			return new DataResponse([], Http::STATUS_FORBIDDEN);
131
-		}
132
-
133
-		$storage = $node->getStorage();
134
-		if ($storage->instanceOfStorage(SharedStorage::class)) {
135
-			/** @var SharedStorage $storage */
136
-			$share = $storage->getShare();
137
-			$attributes = $share->getAttributes();
138
-			if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
139
-				return new DataResponse([], Http::STATUS_FORBIDDEN);
140
-			}
141
-		}
142
-
143
-		try {
144
-			$f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
145
-			$response = new FileDisplayResponse($f, Http::STATUS_OK, [
146
-				'Content-Type' => $f->getMimeType(),
147
-			]);
148
-			$response->cacheFor(3600 * 24, false, true);
149
-			return $response;
150
-		} catch (NotFoundException $e) {
151
-			return new DataResponse([], Http::STATUS_NOT_FOUND);
152
-		} catch (\InvalidArgumentException $e) {
153
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
154
-		}
155
-	}
43
+    private string $userId;
44
+    private IRootFolder $root;
45
+    private IPreview $preview;
46
+
47
+    public function __construct(string $appName,
48
+                                IRequest $request,
49
+                                IPreview $preview,
50
+                                IRootFolder $root,
51
+                                ?string $userId
52
+    ) {
53
+        parent::__construct($appName, $request);
54
+
55
+        $this->preview = $preview;
56
+        $this->root = $root;
57
+        $this->userId = $userId;
58
+    }
59
+
60
+    /**
61
+     * @NoAdminRequired
62
+     * @NoCSRFRequired
63
+     *
64
+     * @return DataResponse|FileDisplayResponse
65
+     */
66
+    public function getPreview(
67
+        string $file = '',
68
+        int $x = 32,
69
+        int $y = 32,
70
+        bool $a = false,
71
+        bool $forceIcon = true,
72
+        string $mode = 'fill'): Http\Response {
73
+        if ($file === '' || $x === 0 || $y === 0) {
74
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
75
+        }
76
+
77
+        try {
78
+            $userFolder = $this->root->getUserFolder($this->userId);
79
+            $node = $userFolder->get($file);
80
+        } catch (NotFoundException $e) {
81
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
82
+        }
83
+
84
+        return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
85
+    }
86
+
87
+    /**
88
+     * @NoAdminRequired
89
+     * @NoCSRFRequired
90
+     *
91
+     * @return DataResponse|FileDisplayResponse
92
+     */
93
+    public function getPreviewByFileId(
94
+        int $fileId = -1,
95
+        int $x = 32,
96
+        int $y = 32,
97
+        bool $a = false,
98
+        bool $forceIcon = true,
99
+        string $mode = 'fill') {
100
+        if ($fileId === -1 || $x === 0 || $y === 0) {
101
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
102
+        }
103
+
104
+        $userFolder = $this->root->getUserFolder($this->userId);
105
+        $nodes = $userFolder->getById($fileId);
106
+
107
+        if (\count($nodes) === 0) {
108
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
109
+        }
110
+
111
+        $node = array_pop($nodes);
112
+
113
+        return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
114
+    }
115
+
116
+    /**
117
+     * @return DataResponse|FileDisplayResponse
118
+     */
119
+    private function fetchPreview(
120
+        Node $node,
121
+        int $x,
122
+        int $y,
123
+        bool $a,
124
+        bool $forceIcon,
125
+        string $mode) : Http\Response {
126
+        if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
127
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
128
+        }
129
+        if (!$node->isReadable()) {
130
+            return new DataResponse([], Http::STATUS_FORBIDDEN);
131
+        }
132
+
133
+        $storage = $node->getStorage();
134
+        if ($storage->instanceOfStorage(SharedStorage::class)) {
135
+            /** @var SharedStorage $storage */
136
+            $share = $storage->getShare();
137
+            $attributes = $share->getAttributes();
138
+            if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
139
+                return new DataResponse([], Http::STATUS_FORBIDDEN);
140
+            }
141
+        }
142
+
143
+        try {
144
+            $f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
145
+            $response = new FileDisplayResponse($f, Http::STATUS_OK, [
146
+                'Content-Type' => $f->getMimeType(),
147
+            ]);
148
+            $response->cacheFor(3600 * 24, false, true);
149
+            return $response;
150
+        } catch (NotFoundException $e) {
151
+            return new DataResponse([], Http::STATUS_NOT_FOUND);
152
+        } catch (\InvalidArgumentException $e) {
153
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
154
+        }
155
+    }
156 156
 }
Please login to merge, or discard this patch.