Fixed crash when no albums are available

This commit is contained in:
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

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
**/*/logs
upload.php
config.php
.nova
# Created by https://www.toptal.com/developers/gitignore/api/composer,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=composer,macos

View File

@ -36,7 +36,7 @@ use PhotoPrismUpload\API\PhotoPrism;
}
</style>
</head>
<body></body>
<body>
<?php
$config = require(__DIR__ . '/config.php');
@ -45,22 +45,22 @@ $albums = [];
try {
$api->login();
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
die('Fehler: ' . $e->getMessage().'</body></html>');
}
if (!isset($_POST['submit'])) {
if (!isset($_GET['token'])) {
die('Sorry, kein Zugriff');
die('Sorry, kein Zugriff</body></html>');
}
$token = $_GET['token'];
$tokens = explode(',', $token);
try {
$albums = $api->getAlbumsByTokens($tokens);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
die('Fehler: ' . $e->getMessage().'</body></html>');
}
if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) {
die('Falscher Token');
die('Falscher Token</body></html>');
}
?>
<script>
@ -118,12 +118,13 @@ if (!isset($_POST['submit'])) {
});
</script>
<?php
die();
die('</body></html>');
}
try {
$api->uploadPhotos($_POST['album']);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
die('Fehler: ' . $e->getMessage().'</body></html>');
}
?>
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
{
$this->logger->debug('getAlbumsByToken');
$albums = $this->getAlbums($count, 0);
$visibleAlbums = [];
foreach ($albums as $album) {
$token = $this->getAlbumToken($album);
$album->token = $token;
if (in_array($album->token, $tokens)) {
if ($token != null && in_array($album->token, $tokens)) {
$visibleAlbums[] = $album;
} else {
$this->logger->debug('Skipping album without access: ' . $album->title);
@ -198,7 +199,7 @@ class PhotoPrism
return $visibleAlbums;
}
public function getAlbumToken($album): string
public function getAlbumToken($album): ?string
{
$uid = is_string($album) ? $album : $album->uid;
$res = $this->makeRequest('GET', '/albums/' . $uid . '/links');
@ -207,6 +208,9 @@ class PhotoPrism
throw new NetworkException($response['error']);
}
$this->logger->debug('Token response: ' . $res);
if (!in_array('Token', $response)) {
return null;
}
return $response['Token'];
}