-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathScreenshotMachineBookmarkPreviewer.php
More file actions
98 lines (83 loc) · 2.17 KB
/
Copy pathScreenshotMachineBookmarkPreviewer.php
File metadata and controls
98 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Service\Previewers;
use Exception;
use OCA\Bookmarks\Contract\IBookmarkPreviewer;
use OCA\Bookmarks\Contract\IImage;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Image;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
class ScreenshotMachineBookmarkPreviewer implements IBookmarkPreviewer {
public const CACHE_PREFIX = 'bookmarks.ScreenshotMachinePreviewService';
public const HTTP_TIMEOUT = 10 * 1000;
/**
* @var string
*/
private $apiKey;
/**
* @var IClient
*/
private $client;
/** @var LoggerInterface */
private $logger;
/**
* @var int
*/
private $width = 800;
/**
* @var int
*/
private $height = 800;
public function __construct(IConfig $config, IClientService $clientService, LoggerInterface $logger) {
$this->apiKey = $config->getAppValue('bookmarks', 'previews.screenshotmachine.key', '');
$this->client = $clientService->newClient();
$this->logger = $logger;
}
/**
* @param Bookmark|null $bookmark
*
* @return Image|null
*/
public function getImage($bookmark, $cacheOnly = false): ?IImage {
if (!isset($bookmark) || $cacheOnly) {
return null;
}
if ($this->apiKey === '') {
return null;
}
$url = $bookmark->getUrl();
// Fetch image from remote server
return $this->fetchImage($url);
}
/**
* @param $url
* @return Image|null
*/
public function fetchImage($url): ?Image {
try {
// get it
$response = $this->client->get(
'https://api.screenshotmachine.com/?key=' . $this->apiKey . '&dimension=' . $this->width . 'x' . $this->height . '&device=desktop&delay=2000&format=jpg&url=' . $url,
[
'timeout' => self::HTTP_TIMEOUT,
]
);
// Some HTTP Error occurred :/
if ($response->getStatusCode() !== 200) {
return null;
}
$body = $response->getBody();
} catch (Exception $e) {
$this->logger->debug($e->getMessage(), ['app' => 'bookmarks']);
return null;
}
return new Image('image/jpeg', $body);
}
}