Passed
Push — master ( 1e85cb...6741b3 )
by Joas
16:42 queued 18s
created
lib/private/Collaboration/Reference/LinkReferenceProvider.php 2 patches
Indentation   +140 added lines, -140 removed lines patch added patch discarded remove patch
@@ -43,144 +43,144 @@
 block discarded – undo
43 43
 use Psr\Log\LoggerInterface;
44 44
 
45 45
 class LinkReferenceProvider implements IReferenceProvider {
46
-	public const MAX_PREVIEW_SIZE = 1024 * 1024;
47
-
48
-	public const ALLOWED_CONTENT_TYPES = [
49
-		'image/png',
50
-		'image/jpg',
51
-		'image/jpeg',
52
-		'image/gif',
53
-		'image/svg+xml',
54
-		'image/webp'
55
-	];
56
-
57
-	private IClientService $clientService;
58
-	private LoggerInterface $logger;
59
-	private SystemConfig $systemConfig;
60
-	private IAppDataFactory $appDataFactory;
61
-	private IURLGenerator $urlGenerator;
62
-	private Limiter $limiter;
63
-	private IUserSession $userSession;
64
-	private IRequest $request;
65
-
66
-	public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) {
67
-		$this->clientService = $clientService;
68
-		$this->logger = $logger;
69
-		$this->systemConfig = $systemConfig;
70
-		$this->appDataFactory = $appDataFactory;
71
-		$this->urlGenerator = $urlGenerator;
72
-		$this->limiter = $limiter;
73
-		$this->userSession = $userSession;
74
-		$this->request = $request;
75
-	}
76
-
77
-	public function matchReference(string $referenceText): bool {
78
-		if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
79
-			return false;
80
-		}
81
-
82
-		return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
83
-	}
84
-
85
-	public function resolveReference(string $referenceText): ?IReference {
86
-		if ($this->matchReference($referenceText)) {
87
-			$reference = new Reference($referenceText);
88
-			$this->fetchReference($reference);
89
-			return $reference;
90
-		}
91
-
92
-		return null;
93
-	}
94
-
95
-	private function fetchReference(Reference $reference): void {
96
-		try {
97
-			$user = $this->userSession->getUser();
98
-			if ($user) {
99
-				$this->limiter->registerUserRequest('opengraph', 10, 120, $user);
100
-			} else {
101
-				$this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress());
102
-			}
103
-		} catch (RateLimitExceededException $e) {
104
-			return;
105
-		}
106
-
107
-		$client = $this->clientService->newClient();
108
-		try {
109
-			$headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]);
110
-		} catch (\Exception $e) {
111
-			$this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]);
112
-			return;
113
-		}
114
-		$linkContentLength = $headResponse->getHeader('Content-Length');
115
-		if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) {
116
-			$this->logger->debug('Skip resolving links pointing to content length > 5 MB');
117
-			return;
118
-		}
119
-		$linkContentType = $headResponse->getHeader('Content-Type');
120
-		$expectedContentType = 'text/html';
121
-		$suffixedExpectedContentType = $expectedContentType . ';';
122
-		$startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType;
123
-		// check the header begins with the expected content type
124
-		if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) {
125
-			$this->logger->debug('Skip resolving links pointing to content type that is not "text/html"');
126
-			return;
127
-		}
128
-		try {
129
-			$response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
130
-		} catch (\Exception $e) {
131
-			$this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
132
-			return;
133
-		}
134
-
135
-		$responseBody = (string)$response->getBody();
136
-
137
-		// OpenGraph handling
138
-		$consumer = new Consumer();
139
-		$consumer->useFallbackMode = true;
140
-		$object = $consumer->loadHtml($responseBody);
141
-
142
-		$reference->setUrl($reference->getId());
143
-
144
-		if ($object->title) {
145
-			$reference->setTitle($object->title);
146
-		}
147
-
148
-		if ($object->description) {
149
-			$reference->setDescription($object->description);
150
-		}
151
-
152
-		if ($object->images) {
153
-			try {
154
-				$appData = $this->appDataFactory->get('core');
155
-				try {
156
-					$folder = $appData->getFolder('opengraph');
157
-				} catch (NotFoundException $e) {
158
-					$folder = $appData->newFolder('opengraph');
159
-				}
160
-				$response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]);
161
-				$contentType = $response->getHeader('Content-Type');
162
-				$contentLength = $response->getHeader('Content-Length');
163
-
164
-				if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
165
-					$stream = Utils::streamFor($response->getBody());
166
-					$bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
167
-					$reference->setImageContentType($contentType);
168
-					$folder->newFile(md5($reference->getId()), $bodyStream->getContents());
169
-					$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
170
-				}
171
-			} catch (GuzzleException $e) {
172
-				$this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
173
-			} catch (\Throwable $e) {
174
-				$this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
175
-			}
176
-		}
177
-	}
178
-
179
-	public function getCachePrefix(string $referenceId): string {
180
-		return $referenceId;
181
-	}
182
-
183
-	public function getCacheKey(string $referenceId): ?string {
184
-		return null;
185
-	}
46
+    public const MAX_PREVIEW_SIZE = 1024 * 1024;
47
+
48
+    public const ALLOWED_CONTENT_TYPES = [
49
+        'image/png',
50
+        'image/jpg',
51
+        'image/jpeg',
52
+        'image/gif',
53
+        'image/svg+xml',
54
+        'image/webp'
55
+    ];
56
+
57
+    private IClientService $clientService;
58
+    private LoggerInterface $logger;
59
+    private SystemConfig $systemConfig;
60
+    private IAppDataFactory $appDataFactory;
61
+    private IURLGenerator $urlGenerator;
62
+    private Limiter $limiter;
63
+    private IUserSession $userSession;
64
+    private IRequest $request;
65
+
66
+    public function __construct(IClientService $clientService, LoggerInterface $logger, SystemConfig $systemConfig, IAppDataFactory $appDataFactory, IURLGenerator $urlGenerator, Limiter $limiter, IUserSession $userSession, IRequest $request) {
67
+        $this->clientService = $clientService;
68
+        $this->logger = $logger;
69
+        $this->systemConfig = $systemConfig;
70
+        $this->appDataFactory = $appDataFactory;
71
+        $this->urlGenerator = $urlGenerator;
72
+        $this->limiter = $limiter;
73
+        $this->userSession = $userSession;
74
+        $this->request = $request;
75
+    }
76
+
77
+    public function matchReference(string $referenceText): bool {
78
+        if ($this->systemConfig->getValue('reference_opengraph', true) !== true) {
79
+            return false;
80
+        }
81
+
82
+        return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
83
+    }
84
+
85
+    public function resolveReference(string $referenceText): ?IReference {
86
+        if ($this->matchReference($referenceText)) {
87
+            $reference = new Reference($referenceText);
88
+            $this->fetchReference($reference);
89
+            return $reference;
90
+        }
91
+
92
+        return null;
93
+    }
94
+
95
+    private function fetchReference(Reference $reference): void {
96
+        try {
97
+            $user = $this->userSession->getUser();
98
+            if ($user) {
99
+                $this->limiter->registerUserRequest('opengraph', 10, 120, $user);
100
+            } else {
101
+                $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress());
102
+            }
103
+        } catch (RateLimitExceededException $e) {
104
+            return;
105
+        }
106
+
107
+        $client = $this->clientService->newClient();
108
+        try {
109
+            $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]);
110
+        } catch (\Exception $e) {
111
+            $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]);
112
+            return;
113
+        }
114
+        $linkContentLength = $headResponse->getHeader('Content-Length');
115
+        if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) {
116
+            $this->logger->debug('Skip resolving links pointing to content length > 5 MB');
117
+            return;
118
+        }
119
+        $linkContentType = $headResponse->getHeader('Content-Type');
120
+        $expectedContentType = 'text/html';
121
+        $suffixedExpectedContentType = $expectedContentType . ';';
122
+        $startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType;
123
+        // check the header begins with the expected content type
124
+        if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) {
125
+            $this->logger->debug('Skip resolving links pointing to content type that is not "text/html"');
126
+            return;
127
+        }
128
+        try {
129
+            $response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
130
+        } catch (\Exception $e) {
131
+            $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
132
+            return;
133
+        }
134
+
135
+        $responseBody = (string)$response->getBody();
136
+
137
+        // OpenGraph handling
138
+        $consumer = new Consumer();
139
+        $consumer->useFallbackMode = true;
140
+        $object = $consumer->loadHtml($responseBody);
141
+
142
+        $reference->setUrl($reference->getId());
143
+
144
+        if ($object->title) {
145
+            $reference->setTitle($object->title);
146
+        }
147
+
148
+        if ($object->description) {
149
+            $reference->setDescription($object->description);
150
+        }
151
+
152
+        if ($object->images) {
153
+            try {
154
+                $appData = $this->appDataFactory->get('core');
155
+                try {
156
+                    $folder = $appData->getFolder('opengraph');
157
+                } catch (NotFoundException $e) {
158
+                    $folder = $appData->newFolder('opengraph');
159
+                }
160
+                $response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]);
161
+                $contentType = $response->getHeader('Content-Type');
162
+                $contentLength = $response->getHeader('Content-Length');
163
+
164
+                if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) {
165
+                    $stream = Utils::streamFor($response->getBody());
166
+                    $bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0);
167
+                    $reference->setImageContentType($contentType);
168
+                    $folder->newFile(md5($reference->getId()), $bodyStream->getContents());
169
+                    $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
170
+                }
171
+            } catch (GuzzleException $e) {
172
+                $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
173
+            } catch (\Throwable $e) {
174
+                $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
175
+            }
176
+        }
177
+    }
178
+
179
+    public function getCachePrefix(string $referenceId): string {
180
+        return $referenceId;
181
+    }
182
+
183
+    public function getCacheKey(string $referenceId): ?string {
184
+        return null;
185
+    }
186 186
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
 			return false;
80 80
 		}
81 81
 
82
-		return (bool)preg_match(IURLGenerator::URL_REGEX, $referenceText);
82
+		return (bool) preg_match(IURLGenerator::URL_REGEX, $referenceText);
83 83
 	}
84 84
 
85 85
 	public function resolveReference(string $referenceText): ?IReference {
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 
107 107
 		$client = $this->clientService->newClient();
108 108
 		try {
109
-			$headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]);
109
+			$headResponse = $client->head($reference->getId(), ['timeout' => 10]);
110 110
 		} catch (\Exception $e) {
111 111
 			$this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]);
112 112
 			return;
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 		}
119 119
 		$linkContentType = $headResponse->getHeader('Content-Type');
120 120
 		$expectedContentType = 'text/html';
121
-		$suffixedExpectedContentType = $expectedContentType . ';';
121
+		$suffixedExpectedContentType = $expectedContentType.';';
122 122
 		$startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType;
123 123
 		// check the header begins with the expected content type
124 124
 		if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) {
@@ -126,13 +126,13 @@  discard block
 block discarded – undo
126 126
 			return;
127 127
 		}
128 128
 		try {
129
-			$response = $client->get($reference->getId(), [ 'timeout' => 10 ]);
129
+			$response = $client->get($reference->getId(), ['timeout' => 10]);
130 130
 		} catch (\Exception $e) {
131 131
 			$this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]);
132 132
 			return;
133 133
 		}
134 134
 
135
-		$responseBody = (string)$response->getBody();
135
+		$responseBody = (string) $response->getBody();
136 136
 
137 137
 		// OpenGraph handling
138 138
 		$consumer = new Consumer();
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 				} catch (NotFoundException $e) {
158 158
 					$folder = $appData->newFolder('opengraph');
159 159
 				}
160
-				$response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]);
160
+				$response = $client->get($object->images[0]->url, ['timeout' => 10]);
161 161
 				$contentType = $response->getHeader('Content-Type');
162 162
 				$contentLength = $response->getHeader('Content-Length');
163 163
 
@@ -169,9 +169,9 @@  discard block
 block discarded – undo
169 169
 					$reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())]));
170 170
 				}
171 171
 			} catch (GuzzleException $e) {
172
-				$this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
172
+				$this->logger->info('Failed to fetch and store the open graph image for '.$reference->getId(), ['exception' => $e]);
173 173
 			} catch (\Throwable $e) {
174
-				$this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]);
174
+				$this->logger->error('Failed to fetch and store the open graph image for '.$reference->getId(), ['exception' => $e]);
175 175
 			}
176 176
 		}
177 177
 	}
Please login to merge, or discard this patch.