From dbbd23ba1d06cf1d954e0bd65e3f82effb901e26 Mon Sep 17 00:00:00 2001 From: notapirate Date: Wed, 10 Sep 2025 21:19:08 +0200 Subject: [PATCH] feat: use IClientService for image requests - replace curl and get_file_contents with IClientService functions Signed-off-by: notapirate --- lib/Provider/BingWallpaperDaily.php | 13 ++++----- lib/Provider/UnsplashAPI.php | 2 +- lib/Provider/WallhavenCC.php | 12 ++++----- lib/Provider/WikimediaCommons.php | 11 ++++---- lib/Provider/WikimediaCommonsDaily.php | 11 ++++---- lib/ProviderHandler/Provider.php | 3 +++ lib/ProviderHandler/ProviderDefinitions.php | 30 +++++++++++++-------- lib/Services/SettingsService.php | 6 +++-- 8 files changed, 49 insertions(+), 39 deletions(-) diff --git a/lib/Provider/BingWallpaperDaily.php b/lib/Provider/BingWallpaperDaily.php index 45ef888..a36520b 100644 --- a/lib/Provider/BingWallpaperDaily.php +++ b/lib/Provider/BingWallpaperDaily.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class BingWallpaperDaily extends Provider @@ -74,9 +73,11 @@ public function getRandomImageUrl($size) public function getRandomImageUrlBySearchTerm($search, $size) { // Fetch the daily image JSON from Bing - $bing_daily_image_json = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'); - if ($bing_daily_image_json !== false) { - $matches = json_decode($bing_daily_image_json); + $client = $this->clientService->newClient(); + $response = $client->get('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'); + $body = $response->getBody(); + if ($body !== false) { + $matches = json_decode($body); if (isset($matches->images[0]->url)) { // If unable to encode, return the image URL return 'https://www.bing.com' . $matches->images[0]->url; @@ -84,6 +85,6 @@ public function getRandomImageUrlBySearchTerm($search, $size) } // Return default image if no Bing image is found - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "Nextcloud"))->getRandomImageUrl($size); } -} \ No newline at end of file +} diff --git a/lib/Provider/UnsplashAPI.php b/lib/Provider/UnsplashAPI.php index f011604..539a65f 100644 --- a/lib/Provider/UnsplashAPI.php +++ b/lib/Provider/UnsplashAPI.php @@ -120,7 +120,7 @@ public function getRandomImageUrlBySearchTerm($search, $size): string if($token === '' && $this->requiresAuth()) { // If the token is empty, return the default image. $this->logger->alert("Unsplash API: the provided token was blank!"); - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "Nextcloud"))->getRandomImageUrl($size); } $url = "https://api.unsplash.com/photos/random?client_id=" . $this->getToken() . "&count=1&query=" . $search; diff --git a/lib/Provider/WallhavenCC.php b/lib/Provider/WallhavenCC.php index 3f56026..73ab85f 100644 --- a/lib/Provider/WallhavenCC.php +++ b/lib/Provider/WallhavenCC.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WallhavenCC extends Provider @@ -66,11 +65,10 @@ public function getRandomImageUrlBySearchTerm($search, $size) break; } - - $curl = curl_init('https://wallhaven.cc/api/v1/search?sorting=random&ratios=16x9,16x10&resolutions=' . $resolution . '&q=' . $search); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $client = $this->clientService->newClient(); + $response = $client->get('https://wallhaven.cc/api/v1/search?sorting=random&ratios=16x9,16x10&resolutions=' . $resolution . '&q=' . $search); + $body = $response->getBody(); + $json = json_decode($body, true); try { $images = $json['data'][array_rand($json['data'])]; @@ -79,7 +77,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommons.php b/lib/Provider/WikimediaCommons.php index f084d81..1c35a0b 100644 --- a/lib/Provider/WikimediaCommons.php +++ b/lib/Provider/WikimediaCommons.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WikimediaCommons extends Provider @@ -63,10 +62,10 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&iiprop=url'; $url .= '&format=json'; - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $client = $this->clientService->newClient(); + $response = $client->get($url); + $body = $response->getBody(); + $json = json_decode($body, true); try { $images = $json['query']['pages'][array_rand($json['query']['pages'])]; @@ -75,7 +74,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/Provider/WikimediaCommonsDaily.php b/lib/Provider/WikimediaCommonsDaily.php index 2ca37c2..109a544 100644 --- a/lib/Provider/WikimediaCommonsDaily.php +++ b/lib/Provider/WikimediaCommonsDaily.php @@ -22,7 +22,6 @@ namespace OCA\Unsplash\Provider; -use OC\AppFramework\Http\Request; use OCA\Unsplash\ProviderHandler\Provider; class WikimediaCommonsDaily extends Provider @@ -57,10 +56,10 @@ public function getRandomImageUrlBySearchTerm($search, $size) $url .= '&iiprop=url'; $url .= '&format=json'; - $curl = curl_init($url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - $response = curl_exec($curl); - $json = json_decode($response, true); + $client = $this->clientService->newClient(); + $response = $client->get($url); + $body = $response->getBody(); + $json = json_decode($body, true); try { $images = $json['query']['pages'][array_rand($json['query']['pages'])]; @@ -69,7 +68,7 @@ public function getRandomImageUrlBySearchTerm($search, $size) $this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName()); } - return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size); + return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "Nextcloud"))->getRandomImageUrl($size); } public function getCachedImageURL(): string diff --git a/lib/ProviderHandler/Provider.php b/lib/ProviderHandler/Provider.php index 278c448..4d7410b 100644 --- a/lib/ProviderHandler/Provider.php +++ b/lib/ProviderHandler/Provider.php @@ -25,6 +25,7 @@ namespace OCA\Unsplash\ProviderHandler; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; use OCP\Files\NotFoundException; @@ -54,6 +55,7 @@ abstract class Provider * @param $appName * @param LoggerInterface $logger * @param IConfig $config + * @param IClientService $clientService * @param $pName */ public function __construct( @@ -61,6 +63,7 @@ public function __construct( protected LoggerInterface $logger, protected IConfig $config, protected IAppData $appData, + protected IClientService $clientService, protected string $providerName, ) { diff --git a/lib/ProviderHandler/ProviderDefinitions.php b/lib/ProviderHandler/ProviderDefinitions.php index cf8e1ee..c78ad8c 100644 --- a/lib/ProviderHandler/ProviderDefinitions.php +++ b/lib/ProviderHandler/ProviderDefinitions.php @@ -30,6 +30,7 @@ use OCA\Unsplash\Provider\WikimediaCommons; use OCA\Unsplash\Provider\WikimediaCommonsDaily; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -45,15 +46,20 @@ class ProviderDefinitions * @var string */ protected $appName; + + /** @var LoggerInterface */ + private $logger; + /** * @var definitions This variable contains all available provider */ protected $definitions = []; + /** @var IAppData */ private $appData; - /** @var LoggerInterface */ - private $logger; + /** @var IClientService */ + private $clientService; /** * ProviderDefinitions constructor. @@ -62,23 +68,25 @@ class ProviderDefinitions * @param LoggerInterface $logger * @param IConfig $settings * @param IAppData $appData + * @param IClientService $clientService */ - function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData) + function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData, IClientService $clientService) { $this->appName = $appName; $this->config = $config; $this->appData = $appData; $this->logger = $logger; + $this->clientService = $clientService; $tmp = []; //add all provider to this array. The logic takes care of the rest. - $tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, "UnsplashAPI"); - $tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, "Nextcloud Image"); - $tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, "WikimediaCommons"); - $tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, "WikimediaCommons - Picture of the Day"); - $tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, "WallhavenCC"); - $tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, "Bing Wallpaper - Picture of the Day"); + $tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, $clientService, "UnsplashAPI"); + $tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, $clientService, "Nextcloud Image"); + $tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, $clientService, "WikimediaCommons"); + $tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, $clientService, "WikimediaCommons - Picture of the Day"); + $tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, $clientService, "WallhavenCC"); + $tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, $clientService, "Bing Wallpaper - Picture of the Day"); foreach ($tmp as &$value) { $this->definitions[$value->getName()] = $value; @@ -96,11 +104,11 @@ function getProviderByName($name): Provider if (!array_key_exists($name, $this->definitions)) { $this->logger->warning("Selected provider '{$name}' could not be found. Using Default. Please select an existing provider in the settings!"); - return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day"); + return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "WikimediaCommons - Picture of the Day"); } $provider = $this->definitions[$name]; if ($provider == null) { - return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day"); + return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->clientService, "WikimediaCommons - Picture of the Day"); } return $this->definitions[$name]; } diff --git a/lib/Services/SettingsService.php b/lib/Services/SettingsService.php index 4a19e71..ecedd7a 100644 --- a/lib/Services/SettingsService.php +++ b/lib/Services/SettingsService.php @@ -10,6 +10,7 @@ use OCA\Unsplash\ProviderHandler\Provider; use OCA\Unsplash\ProviderHandler\ProviderDefinitions; use OCP\Files\IAppData; +use OCP\Http\Client\IClientService; use OCP\IConfig; use Psr\Log\LoggerInterface; @@ -70,10 +71,11 @@ class SettingsService * @param $appName * @param IConfig $config * @param IAppData $config + * @param IClientService $clientService * @param Defaults $defaults * @param LoggerInterface $logger */ - public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger) + public function __construct($userId, $appName, IConfig $config, IAppData $appData, IClientService $clientService, \OC_Defaults $defaults, LoggerInterface $logger) { $this->config = $config; $this->userId = $userId; @@ -82,7 +84,7 @@ public function __construct($userId, $appName, IConfig $config, IAppData $appDat } $this->appName = $appName; - $this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData); + $this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData, $clientService); $this->defaults = $defaults; }