Added tokens

main
aymm 2021-08-26 10:56:59 +02:00
parent b27a3bbc58
commit c65bc123d3
Signed by: phlaym
GPG Key ID: A06651BAB6777237
4 changed files with 47 additions and 3 deletions

View File

@ -3,4 +3,5 @@
return [
'username' => '',
'password' => ''
'noAlbumToken' => ''
];

View File

@ -17,7 +17,6 @@ use PhotoPrismUpload\API\PhotoPrism;
label[for="album"] {
grid-column: 1;
grid-row: 1;
/* display: block; */
}
#album {
grid-column: 2;
@ -45,12 +44,24 @@ $api = new PhotoPrism($config);
$albums = [];
try {
$api->login();
$albums = $api->getAlbums();
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
}
if (!isset($_POST['submit'])) {
if (!isset($_GET['token'])) {
die('Sorry, kein Zugriff');
}
$token = $_GET['token'];
$tokens = explode(',', $token);
try {
$albums = $api->getAlbumsByTokens($tokens);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
}
if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) {
die('Falscher Token');
}
?>
<script>
window.tooLarge = false;
@ -79,7 +90,7 @@ if (!isset($_POST['submit'])) {
</select>
<!--<label></label>
<input type="text" />!-->
<input multiple type="file" name="files[]" id="input" />
<input multiple type="file" name="files[]" id="input" required/>
<input type="submit" name="submit" value="Upload" />
</form>
<div id="error" style="display:none"></div>

View File

@ -180,6 +180,36 @@ class PhotoPrism
return $albums;
}
public function getAlbumsByTokens(array $tokens, int $count = 1000, int $offset = 0): array
{
$albums = $this->getAlbums($count, 0);
$visibleAlbums = [];
foreach ($albums as $album) {
$token = $this->getAlbumToken($album);
$album->token = $token;
if (in_array($album->token, $tokens)) {
$visibleAlbums[] = $album;
} else {
$this->logger->debug('Skipping album without access: ' . $album->title);
}
}
$this->logger->debug('getAlbumsByToken' . json_encode($visibleAlbums));
return $visibleAlbums;
}
public function getAlbumToken($album): string
{
$uid = is_string($album) ? $album : $album->uid;
$res = $this->makeRequest('GET', '/albums/' . $uid . '/links');
$response = json_decode($res, true)[0];
if (!empty($response['error'])) {
throw new NetworkException($response['error']);
}
$this->logger->debug('Token response: ' . $res);
return $response['Token'];
}
public function uploadPhotos(?string $album = null)
{
$path = time();

View File

@ -8,6 +8,8 @@ class Album
public string $uid = '';
public string $slug = '';
public string $title = '';
public ?string $token = null;
public function __construct(
array $response
) {