Fixed crash when no albums are available

main
Max Nuding 2021-11-26 17:44:38 +01:00
parent c65bc123d3
commit 56127a16a0
Signed by: phlaym
GPG Key ID: A06651BAB6777237
3 changed files with 17 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
**/*/logs **/*/logs
upload.php upload.php
config.php config.php
.nova
# Created by https://www.toptal.com/developers/gitignore/api/composer,macos # Created by https://www.toptal.com/developers/gitignore/api/composer,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=composer,macos # Edit at https://www.toptal.com/developers/gitignore?templates=composer,macos
@ -42,4 +43,4 @@ Network Trash Folder
Temporary Items Temporary Items
.apdisk .apdisk
# End of https://www.toptal.com/developers/gitignore/api/composer,macos # End of https://www.toptal.com/developers/gitignore/api/composer,macos

View File

@ -36,7 +36,7 @@ use PhotoPrismUpload\API\PhotoPrism;
} }
</style> </style>
</head> </head>
<body></body> <body>
<?php <?php
$config = require(__DIR__ . '/config.php'); $config = require(__DIR__ . '/config.php');
@ -45,22 +45,22 @@ $albums = [];
try { try {
$api->login(); $api->login();
} catch (\Exception $e) { } catch (\Exception $e) {
die('Fehler: ' . $e->getMessage()); die('Fehler: ' . $e->getMessage().'</body></html>');
} }
if (!isset($_POST['submit'])) { if (!isset($_POST['submit'])) {
if (!isset($_GET['token'])) { if (!isset($_GET['token'])) {
die('Sorry, kein Zugriff'); die('Sorry, kein Zugriff</body></html>');
} }
$token = $_GET['token']; $token = $_GET['token'];
$tokens = explode(',', $token); $tokens = explode(',', $token);
try { try {
$albums = $api->getAlbumsByTokens($tokens); $albums = $api->getAlbumsByTokens($tokens);
} catch (\Exception $e) { } catch (\Exception $e) {
die('Fehler: ' . $e->getMessage()); die('Fehler: ' . $e->getMessage().'</body></html>');
} }
if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) { if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) {
die('Falscher Token'); die('Falscher Token</body></html>');
} }
?> ?>
<script> <script>
@ -118,12 +118,13 @@ if (!isset($_POST['submit'])) {
}); });
</script> </script>
<?php <?php
die(); die('</body></html>');
} }
try { try {
$api->uploadPhotos($_POST['album']); $api->uploadPhotos($_POST['album']);
} catch (\Exception $e) { } catch (\Exception $e) {
die('Fehler: ' . $e->getMessage()); die('Fehler: ' . $e->getMessage().'</body></html>');
} }
?> ?>
Erfolg! <a href=".">Zurück</a> Erfolg! <a href=".">Zurück</a>
</body></html>

View File

@ -182,12 +182,13 @@ class PhotoPrism
public function getAlbumsByTokens(array $tokens, int $count = 1000, int $offset = 0): array public function getAlbumsByTokens(array $tokens, int $count = 1000, int $offset = 0): array
{ {
$this->logger->debug('getAlbumsByToken');
$albums = $this->getAlbums($count, 0); $albums = $this->getAlbums($count, 0);
$visibleAlbums = []; $visibleAlbums = [];
foreach ($albums as $album) { foreach ($albums as $album) {
$token = $this->getAlbumToken($album); $token = $this->getAlbumToken($album);
$album->token = $token; $album->token = $token;
if (in_array($album->token, $tokens)) { if ($token != null && in_array($album->token, $tokens)) {
$visibleAlbums[] = $album; $visibleAlbums[] = $album;
} else { } else {
$this->logger->debug('Skipping album without access: ' . $album->title); $this->logger->debug('Skipping album without access: ' . $album->title);
@ -198,7 +199,7 @@ class PhotoPrism
return $visibleAlbums; return $visibleAlbums;
} }
public function getAlbumToken($album): string public function getAlbumToken($album): ?string
{ {
$uid = is_string($album) ? $album : $album->uid; $uid = is_string($album) ? $album : $album->uid;
$res = $this->makeRequest('GET', '/albums/' . $uid . '/links'); $res = $this->makeRequest('GET', '/albums/' . $uid . '/links');
@ -207,6 +208,9 @@ class PhotoPrism
throw new NetworkException($response['error']); throw new NetworkException($response['error']);
} }
$this->logger->debug('Token response: ' . $res); $this->logger->debug('Token response: ' . $res);
if (!in_array('Token', $response)) {
return null;
}
return $response['Token']; return $response['Token'];
} }