Skip to content

Commit b19e2af

Browse files
committed
feat: add function getUserAgent to providers
The function getUserAgent uses the app informations to construct the string "unsplash/<version> (<website>; <contact>)" to be used as user agent for provider requests.
1 parent 6793bfc commit b19e2af

8 files changed

Lines changed: 51 additions & 17 deletions

File tree

lib/Provider/BingWallpaperDaily.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ public function getRandomImageUrlBySearchTerm($search, $size)
8484
}
8585

8686
// Return default image if no Bing image is found
87-
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size);
87+
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size);
8888
}
89-
}
89+
}

lib/Provider/UnsplashAPI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getRandomImageUrlBySearchTerm($search, $size): string
120120
if($token === '' && $this->requiresAuth()) {
121121
// If the token is empty, return the default image.
122122
$this->logger->alert("Unsplash API: the provided token was blank!");
123-
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size);
123+
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size);
124124
}
125125

126126
$url = "https://api.unsplash.com/photos/random?client_id=" . $this->getToken() . "&count=1&query=" . $search;

lib/Provider/WallhavenCC.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getRandomImageUrlBySearchTerm($search, $size)
7979
$this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName());
8080
}
8181

82-
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size);
82+
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size);
8383
}
8484

8585
public function getCachedImageURL(): string

lib/Provider/WikimediaCommons.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getRandomImageUrlBySearchTerm($search, $size)
7575
$this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName());
7676
}
7777

78-
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size);
78+
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size);
7979
}
8080

8181
public function getCachedImageURL(): string

lib/Provider/WikimediaCommonsDaily.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getRandomImageUrlBySearchTerm($search, $size)
6969
$this->logger->alert("Your searchterms likely did not yield results for: ".$this->getName());
7070
}
7171

72-
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, "Nextcloud"))->getRandomImageUrl($size);
72+
return (new NextcloudImage($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "Nextcloud"))->getRandomImageUrl($size);
7373
}
7474

7575
public function getCachedImageURL(): string

lib/ProviderHandler/Provider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace OCA\Unsplash\ProviderHandler;
2626

27+
use OCP\App\IAppManager;
2728
use OCP\Files\IAppData;
2829
use OCP\IConfig;
2930
use Psr\Log\LoggerInterface;
@@ -61,6 +62,7 @@ public function __construct(
6162
protected LoggerInterface $logger,
6263
protected IConfig $config,
6364
protected IAppData $appData,
65+
protected IAppManager $appManager,
6466
protected string $providerName,
6567
)
6668
{
@@ -245,4 +247,28 @@ protected function getImageFolder()
245247
return $rootFolder;
246248
}
247249

250+
/**
251+
*
252+
* This returns the user agent string ('unsplash/<version> (<website>; <mail>)').
253+
* Some image providers require a non-empty user agent, e.g. WikiMedia
254+
*
255+
* @return string
256+
*/
257+
protected function getUserAgent()
258+
{
259+
$info = $this->appManager->getAppInfo($this->appName);
260+
$name = $this->appName;
261+
$version = $this->appManager->getAppVersion($name);
262+
$website = $info['website'];
263+
$mail = "";
264+
265+
foreach ($info['author'] as $author) {
266+
if(!isset($author['@attributes']['mail'])) {
267+
continue;
268+
}
269+
$mail = "; ".$author['@attributes']['mail'];
270+
}
271+
272+
return $name."/".$version." (".$website.$mail.")";
273+
}
248274
}

lib/ProviderHandler/ProviderDefinitions.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\Unsplash\Provider\WallhavenCC;
3030
use OCA\Unsplash\Provider\WikimediaCommons;
3131
use OCA\Unsplash\Provider\WikimediaCommonsDaily;
32+
use OCP\App\IAppManager;
3233
use OCP\Files\IAppData;
3334
use OCP\IConfig;
3435
use Psr\Log\LoggerInterface;
@@ -63,22 +64,23 @@ class ProviderDefinitions
6364
* @param IConfig $settings
6465
* @param IAppData $appData
6566
*/
66-
function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData)
67+
function __construct($appName, LoggerInterface $logger, IConfig $config, IAppData $appData, IAppManager $appManager)
6768
{
6869

6970
$this->appName = $appName;
7071
$this->config = $config;
7172
$this->appData = $appData;
7273
$this->logger = $logger;
74+
$this->appManager = $appManager;
7375

7476
$tmp = [];
7577
//add all provider to this array. The logic takes care of the rest.
76-
$tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, "UnsplashAPI");
77-
$tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, "Nextcloud Image");
78-
$tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, "WikimediaCommons");
79-
$tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, "WikimediaCommons - Picture of the Day");
80-
$tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, "WallhavenCC");
81-
$tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, "Bing Wallpaper - Picture of the Day");
78+
$tmp[] = new UnsplashAPI($this->appName, $logger, $this->config, $appData, $appManager, "UnsplashAPI");
79+
$tmp[] = new NextcloudImage($this->appName, $logger, $this->config, $appData, $appManager, "Nextcloud Image");
80+
$tmp[] = new WikimediaCommons($this->appName, $logger, $this->config, $appData, $appManager, "WikimediaCommons");
81+
$tmp[] = new WikimediaCommonsDaily($this->appName, $logger, $this->config, $appData, $appManager, "WikimediaCommons - Picture of the Day");
82+
$tmp[] = new WallhavenCC($this->appName, $logger, $this->config, $appData, $appManager, "WallhavenCC");
83+
$tmp[] = new BingWallpaperDaily($this->appName, $logger, $this->config, $appData, $appManager, "Bing Wallpaper - Picture of the Day");
8284

8385
foreach ($tmp as &$value) {
8486
$this->definitions[$value->getName()] = $value;
@@ -96,11 +98,11 @@ function getProviderByName($name): Provider
9698

9799
if (!array_key_exists($name, $this->definitions)) {
98100
$this->logger->warning("Selected provider '{$name}' could not be found. Using Default. Please select an existing provider in the settings!");
99-
return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day");
101+
return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "WikimediaCommons - Picture of the Day");
100102
}
101103
$provider = $this->definitions[$name];
102104
if ($provider == null) {
103-
return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, "WikimediaCommons - Picture of the Day");
105+
return new WikimediaCommonsDaily($this->appName, $this->logger, $this->config, $this->appData, $this->appManager, "WikimediaCommons - Picture of the Day");
104106
}
105107
return $this->definitions[$name];
106108
}

lib/Services/SettingsService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use OCA\Unsplash\ProviderHandler\CachedProvider;
1010
use OCA\Unsplash\ProviderHandler\Provider;
1111
use OCA\Unsplash\ProviderHandler\ProviderDefinitions;
12+
use OCP\App\IAppManager;
1213
use OCP\Files\IAppData;
1314
use OCP\IConfig;
1415
use Psr\Log\LoggerInterface;
@@ -63,6 +64,11 @@ class SettingsService
6364
*/
6465
private $defaults;
6566

67+
/**
68+
* @var IAppManager
69+
*/
70+
protected $appManager;
71+
6672
/**
6773
* FaviconService constructor.
6874
*
@@ -73,7 +79,7 @@ class SettingsService
7379
* @param Defaults $defaults
7480
* @param LoggerInterface $logger
7581
*/
76-
public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger)
82+
public function __construct($userId, $appName, IConfig $config, IAppData $appData, \OC_Defaults $defaults, LoggerInterface $logger, IAppManager $appManager)
7783
{
7884
$this->config = $config;
7985
$this->userId = $userId;
@@ -82,7 +88,7 @@ public function __construct($userId, $appName, IConfig $config, IAppData $appDat
8288
}
8389
$this->appName = $appName;
8490

85-
$this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData);
91+
$this->providerDefinitions = new ProviderDefinitions($this->appName, $logger, $this->config, $appData, $appManager);
8692
$this->defaults = $defaults;
8793
}
8894

0 commit comments

Comments
 (0)