Compare commits
8 Commits
1.0.1
...
29b429e239
Author | SHA1 | Date | |
---|---|---|---|
29b429e239
|
|||
ae4eb30a73
|
|||
19db5f3575
|
|||
5ef5012224
|
|||
f8e72356a3
|
|||
1247d3c969
|
|||
0b613eb8cf
|
|||
966e511140
|
@ -1,4 +1,6 @@
|
|||||||
{
|
{
|
||||||
"workspace.art_style" : 0,
|
"workspace.art_style" : 0,
|
||||||
"workspace.name" : "PhotoPrismUpload"
|
"workspace.name" : "PhotoPrismUpload",
|
||||||
|
"workspace.preview_type" : "custom",
|
||||||
|
"workspace.preview_url" : "http:\/\/phlaym.net\/photoupload\/?token=1g29eaapq6"
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,7 @@
|
|||||||
return [
|
return [
|
||||||
'username' => '',
|
'username' => '',
|
||||||
'password' => ''
|
'password' => ''
|
||||||
'noAlbumToken' => ''
|
'noAlbumToken' => '',
|
||||||
|
'fileUploadLimitMb' => 1024,
|
||||||
|
'maximumNumberOfFilesPerUpload' => 200
|
||||||
];
|
];
|
||||||
|
150
index.php
150
index.php
@ -1,9 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
|
/** require autoloading to manage namespaces */
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
use PhotoPrismUpload\API\PhotoPrism;
|
use PhotoPrismUpload\API\PhotoPrism;
|
||||||
$footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phlaym/photoprismupload">Ich bin Open Source</a></footer>';
|
use PhotoPrismUpload\Entities\Album;
|
||||||
|
|
||||||
|
/** @var string $footer Footer text which links to the Gitea repo */
|
||||||
|
$footer = '<footer style="position: fixed;bottom: 0;left: 0;">'
|
||||||
|
.'<a href="/git/phlaym/photoprismupload">Ich bin Open Source</a></footer>';
|
||||||
?>
|
?>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@ -36,12 +42,12 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
|
|||||||
.form-wrapper {
|
.form-wrapper {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-rows: auto auto auto;
|
grid-template-rows: auto auto auto;
|
||||||
grid-auto-columns: auto auto;
|
grid-auto-columns: minmax(auto, 300px) auto;
|
||||||
max-width: 300px;
|
|
||||||
}
|
}
|
||||||
label[for="album"] {
|
label[for="album"] {
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
grid-row: 1;
|
grid-row: 1;
|
||||||
|
align-self: center;
|
||||||
}
|
}
|
||||||
#album {
|
#album {
|
||||||
grid-column: 2;
|
grid-column: 2;
|
||||||
@ -59,7 +65,11 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
|
|||||||
grid-row: 3;
|
grid-row: 3;
|
||||||
justify-self: right;
|
justify-self: right;
|
||||||
}
|
}
|
||||||
#error, #fileProgress, #totalProgress, label[for="fileProgress"], label[for="totalProgress"] {
|
#error,
|
||||||
|
#fileProgress,
|
||||||
|
#totalProgress,
|
||||||
|
label[for="fileProgress"],
|
||||||
|
label[for="totalProgress"] {
|
||||||
display:none;
|
display:none;
|
||||||
grid-column: 1;
|
grid-column: 1;
|
||||||
}
|
}
|
||||||
@ -69,6 +79,7 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
|
|||||||
}
|
}
|
||||||
#error {
|
#error {
|
||||||
grid-row: 2;
|
grid-row: 2;
|
||||||
|
grid-column: 1/3;
|
||||||
}
|
}
|
||||||
label[for="fileProgress"] {
|
label[for="fileProgress"] {
|
||||||
grid-row: 3;
|
grid-row: 3;
|
||||||
@ -84,6 +95,9 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
|
|||||||
grid-row: 6;
|
grid-row: 6;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
#viewAlbum {
|
||||||
|
grid-row: 7;
|
||||||
|
}
|
||||||
footer {
|
footer {
|
||||||
margin: 8px;
|
margin: 8px;
|
||||||
}
|
}
|
||||||
@ -92,58 +106,68 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
|
|||||||
<body>
|
<body>
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/** @var array $config configuration options */
|
||||||
$config = require(__DIR__ . '/config.php');
|
$config = require(__DIR__ . '/config.php');
|
||||||
|
|
||||||
|
/** @var PhotoPrism $api API object to interface with PhotoPrism */
|
||||||
$api = new PhotoPrism($config);
|
$api = new PhotoPrism($config);
|
||||||
|
|
||||||
|
/** @var Album[] $albums List of PhotoPrism albums */
|
||||||
$albums = [];
|
$albums = [];
|
||||||
try {
|
try {
|
||||||
$api->login();
|
$api->login();
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
die('Fehler: ' . $e->getMessage().'</body></html>');
|
die('Fehler: ' . $e->getMessage().$footer.'</body></html>');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($_POST['submit'])) {
|
if (!isset($_POST['submit'])) {
|
||||||
if (!isset($_GET['token'])) {
|
if (!isset($_GET['token'])) {
|
||||||
die('Sorry, kein Zugriff</body></html>');
|
die('Sorry, kein Zugriff' . $footer . '</body></html>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var string $token Tokens for which album(s) are visible in the dropdown */
|
||||||
$token = $_GET['token'];
|
$token = $_GET['token'];
|
||||||
|
|
||||||
|
/** @var string[] $tokens List of album tokens */
|
||||||
$tokens = explode(',', $token);
|
$tokens = explode(',', $token);
|
||||||
|
|
||||||
|
/** @var string $album_url URL path to the selected album */
|
||||||
|
$album_url = '/';
|
||||||
try {
|
try {
|
||||||
$albums = $api->getAlbumsByTokens($tokens);
|
$albums = $api->getAlbumsByTokens($tokens);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
die('Fehler: ' . $e->getMessage().'</body></html>');
|
die('Fehler: ' . $footer . $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</body></html>');
|
die('Falscher Token' . $footer . '</body></html>');
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
<script>
|
|
||||||
window.tooLarge = false;
|
|
||||||
window.tooManyFiles = false;
|
|
||||||
function validateForm() {
|
|
||||||
const isInvalid = window.tooLarge || window.tooManyFiles;
|
|
||||||
console.log('Validating', window.tooLarge, window.tooManyFiles, isInvalid);
|
|
||||||
if (isInvalid) {
|
|
||||||
const errorDiv = document.getElementById('error');
|
|
||||||
errorDiv.innerText = '';
|
|
||||||
errorDiv.innerText += window.tooLarge ? 'Zu groß, Upload muss weniger als 200MB sein. ' : '';
|
|
||||||
errorDiv.innerText += window.tooManyFiles ? 'Zu viele Dateien, maximal 200 erlaubt. ' : '';
|
|
||||||
errorDiv.style.display = 'block';
|
|
||||||
}
|
|
||||||
return !isInvalid;
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
<div class="form-wrapper">
|
<div class="form-wrapper">
|
||||||
<!--<form method="POST" enctype="multipart/form-data" onsubmit="return validateForm();" id="uploadForm"> !-->
|
|
||||||
<form method="POST" enctype="multipart/form-data" id="uploadForm">
|
<form method="POST" enctype="multipart/form-data" id="uploadForm">
|
||||||
<label for="album">Zu Album hinzufügen</label>
|
<label for="album">Zu Album hinzufügen</label>
|
||||||
<select name="album" id="album">
|
<select name="album" id="album">
|
||||||
<option value="">---</option>
|
<option value="" data-url="/">---</option>
|
||||||
<?php foreach ($albums as $album) {
|
<?php
|
||||||
echo '<option value="' . $album->uid . '">' . $album->title . '</option>\n';
|
/** @var Album $album Current PhotoPrism albums */
|
||||||
} ?>
|
foreach ($albums as $album) {
|
||||||
|
/** @var string $selected Selected attribute of the option */
|
||||||
|
$selected = $album->token === $token ? ' selected ' : '';
|
||||||
|
if ($album->token === $token) {
|
||||||
|
$album_url = $album->getUrlPath() ?? '/';
|
||||||
|
}
|
||||||
|
echo '<option value="'
|
||||||
|
. $album->uid
|
||||||
|
. '"'
|
||||||
|
. $selected
|
||||||
|
. 'data-url='
|
||||||
|
. ($album->getUrlPath() ?? '/')
|
||||||
|
. '>'
|
||||||
|
. $album->title
|
||||||
|
. '</option>\n';
|
||||||
|
}
|
||||||
|
$album_url = "https://photos.phlaym.net{$album_url}";
|
||||||
|
?>
|
||||||
</select>
|
</select>
|
||||||
<!--<label></label>
|
|
||||||
<input type="text" />!-->
|
|
||||||
<input multiple type="file" name="files[]" id="input" required/>
|
<input multiple type="file" name="files[]" id="input" required/>
|
||||||
<input type="submit" name="submit" value="Upload" />
|
<input type="submit" name="submit" value="Upload" />
|
||||||
</form>
|
</form>
|
||||||
@ -152,8 +176,11 @@ if (!isset($_POST['submit'])) {
|
|||||||
<progress id="fileProgress"></progress>
|
<progress id="fileProgress"></progress>
|
||||||
<label for="totalProgress">Gesamt:</label>
|
<label for="totalProgress">Gesamt:</label>
|
||||||
<progress max="0" value="0" id="totalProgress"></progress>
|
<progress max="0" value="0" id="totalProgress"></progress>
|
||||||
|
<a href="<?=$album_url;?>" target="_blank" id="viewAlbum">Album ansehen</a>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
|
window.tooLarge = false;
|
||||||
|
window.tooManyFiles = false;
|
||||||
const form = document.getElementById('uploadForm');
|
const form = document.getElementById('uploadForm');
|
||||||
const submitButton = form.querySelector('input[type=submit]');
|
const submitButton = form.querySelector('input[type=submit]');
|
||||||
const albumInput = form.querySelector('select[name=album]');
|
const albumInput = form.querySelector('select[name=album]');
|
||||||
@ -163,6 +190,7 @@ if (!isset($_POST['submit'])) {
|
|||||||
const fileProgressLabel = document.querySelector('label[for=fileProgress]');
|
const fileProgressLabel = document.querySelector('label[for=fileProgress]');
|
||||||
const totalProgressLabel = document.querySelector('label[for=totalProgress]');
|
const totalProgressLabel = document.querySelector('label[for=totalProgress]');
|
||||||
const errorDiv = document.getElementById('error');
|
const errorDiv = document.getElementById('error');
|
||||||
|
const albumAnchor = document.getElementById('viewAlbum');
|
||||||
|
|
||||||
async function postData(url, data = {}, method = 'POST') {
|
async function postData(url, data = {}, method = 'POST') {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
@ -172,8 +200,18 @@ if (!isset($_POST['submit'])) {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
albumInput.addEventListener('change', (event) => {
|
||||||
|
console.log(event);
|
||||||
|
albumAnchor.href = `https://photos.phlaym.net${albumInput.selectedOptions[0].dataset.url}`;
|
||||||
|
});
|
||||||
|
|
||||||
form.addEventListener('submit', async function(event) {
|
form.addEventListener('submit', async function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
const isInvalid = window.tooLarge || window.tooManyFiles;
|
||||||
|
if (isInvalid) {
|
||||||
|
console.error('Aborting upload! Too many fiels or fiels too large');
|
||||||
|
return;
|
||||||
|
}
|
||||||
errorDiv.innerText = '';
|
errorDiv.innerText = '';
|
||||||
|
|
||||||
fileProgressLabel.style.display = 'inherit';
|
fileProgressLabel.style.display = 'inherit';
|
||||||
@ -210,35 +248,67 @@ if (!isset($_POST['submit'])) {
|
|||||||
|
|
||||||
let fileList = [];
|
let fileList = [];
|
||||||
input.addEventListener('change', (event) => {
|
input.addEventListener('change', (event) => {
|
||||||
|
const maxFileSize = <?=$config['fileUploadLimitMb'];?>;
|
||||||
|
const maxAmountOfFiles = <?=$config['maximumNumberOfFilesPerUpload'];?>;
|
||||||
const errorDiv = document.getElementById('error');
|
const errorDiv = document.getElementById('error');
|
||||||
const totalProgress = document.getElementById('totalProgress');
|
const totalProgress = document.getElementById('totalProgress');
|
||||||
|
|
||||||
errorDiv.innerText = '';
|
errorDiv.innerText = '';
|
||||||
errorDiv.style.display = 'none';
|
errorDiv.style.display = 'none';
|
||||||
|
submitButton.disabled = false;
|
||||||
|
|
||||||
const target = event.target;
|
const target = event.target;
|
||||||
let totalSize = 0;
|
|
||||||
fileList = [];
|
fileList = [];
|
||||||
|
const filesTooLarge = [];
|
||||||
if (target.files) {
|
if (target.files) {
|
||||||
for (file of target.files) {
|
for (file of target.files) {
|
||||||
totalSize += file.size;
|
const sizeInMb = file.size / 1024 / 1024;
|
||||||
|
if (sizeInMb >= maxFileSize) {
|
||||||
|
filesTooLarge.push(file.name);
|
||||||
|
console.warn(
|
||||||
|
'File',
|
||||||
|
file.name,
|
||||||
|
'is',
|
||||||
|
sizeInMb,
|
||||||
|
'MB big, which is over the limit of',
|
||||||
|
maxFileSize);
|
||||||
|
}
|
||||||
fileList.push(file);
|
fileList.push(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalProgress.max = fileList.length;
|
totalProgress.max = fileList.length;
|
||||||
const sizeInMb = totalSize / 1000 / 1000;
|
|
||||||
window.tooLarge = sizeInMb >= 200;
|
window.tooManyFiles = fileList.length > maxAmountOfFiles;
|
||||||
console.log('Total size:', totalSize, 'too large: ', window.tooLarge);
|
if (window.tooManyFiles) {
|
||||||
window.tooManyFiles = target.files.length > 200;
|
errorDiv.style.display = 'block';
|
||||||
console.log('Total files:', target.files.length, 'too many: ', window.tooManyFiles);
|
errorDiv.innerHTML += ```
|
||||||
|
Das sind zu viele Dateien, du darfst max.
|
||||||
|
${maxAmountOfFiles} Dateien gleichzeitig hochladen. ```;
|
||||||
|
submitButton.disabled = true;
|
||||||
|
console.warn('Total files:', target.files.length, '. Too many!');
|
||||||
|
}
|
||||||
|
|
||||||
|
window.tooLarge = filesTooLarge.length > 0;
|
||||||
|
if (window.tooLarge) {
|
||||||
|
const names = filesTooLarge.join(', ')
|
||||||
|
errorDiv.style.display = 'block';
|
||||||
|
const pluralizedMessage = filesTooLarge.length > 1
|
||||||
|
? 'Die folgenden Dateien sind'
|
||||||
|
: 'Die folgende Datei ist';
|
||||||
|
errorDiv.innerHTML += ```
|
||||||
|
${pluralizedMessage} zu groß: ${names}.
|
||||||
|
Jede Datei darf max. ${maxFileSize} MB groß sein.```;
|
||||||
|
submitButton.disabled = true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<?php
|
<?php
|
||||||
die($footer.'</body></html>');
|
die($footer . '</body></html>');
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$api->uploadPhotos($_POST['album']);
|
$api->uploadPhotos($_POST['album']);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
die('Fehler: ' . $e->getMessage().'</body></html>');
|
die('Fehler: ' . $footer . $e->getMessage() .'</body></html>');
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
Erfolg! <a href=".">Zurück</a>
|
Erfolg! <a href=".">Zurück</a>
|
||||||
|
39
src/API/LoggerFactory.php
Normal file
39
src/API/LoggerFactory.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
namespace PhotoPrismUpload\API;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
use Monolog\Handler\HandlerInterface;
|
||||||
|
|
||||||
|
/** Simple factory to create a logger without needing to set stream handlers every time */
|
||||||
|
class LoggerFactory
|
||||||
|
{
|
||||||
|
private static $handlers = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new handler which is automatically added to the list of handlers
|
||||||
|
* for all _future_ loggers
|
||||||
|
*
|
||||||
|
* @param HandlerInterface $handler The handler to add
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function addHandler(HandlerInterface $handler): void
|
||||||
|
{
|
||||||
|
self::$handlers[] = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Logger with the specified name
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
*
|
||||||
|
* @return Logger
|
||||||
|
*/
|
||||||
|
public static function create(string $name): Logger
|
||||||
|
{
|
||||||
|
$l = new Logger($name);
|
||||||
|
$l->setHandlers(self::$handlers);
|
||||||
|
$l->info('Initialized');
|
||||||
|
return $l;
|
||||||
|
}
|
||||||
|
}
|
@ -9,31 +9,61 @@ use PhotoPrismUpload\Exceptions\NetworkException;
|
|||||||
use PhotoPrismUpload\Exceptions\AuthenticationException;
|
use PhotoPrismUpload\Exceptions\AuthenticationException;
|
||||||
use PhotoPrismUpload\Entities\Album;
|
use PhotoPrismUpload\Entities\Album;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main API class to interface with PhotoPrism
|
||||||
|
*/
|
||||||
class PhotoPrism
|
class PhotoPrism
|
||||||
{
|
{
|
||||||
protected string $base_url = 'https://photos.phlaym.net';
|
/** @var string $base_url Base URL of the PhotoPrism instance */
|
||||||
|
public string $base_url = 'https://photos.phlaym.net';
|
||||||
|
|
||||||
|
/** @var string $api_url API URL of the PhotoPrism instance */
|
||||||
protected string $api_url = '';
|
protected string $api_url = '';
|
||||||
|
|
||||||
|
/** @var string|null $session_id Session id of the currently logged in user */
|
||||||
protected ?string $session_id = null;
|
protected ?string $session_id = null;
|
||||||
|
|
||||||
|
/** @var array $config Configuration options */
|
||||||
protected array $config;
|
protected array $config;
|
||||||
|
|
||||||
|
/** @var LoggerInterface $logger Logger object */
|
||||||
protected LoggerInterface $logger;
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Photoprism API object from the configuration
|
||||||
|
*
|
||||||
|
* @param array $config Configuration dictionary
|
||||||
|
* @param string|null $log_path Path where the log files end up in.
|
||||||
|
* Will be set to `$log_path = __DIR__.'/logs/log.log';` if empty.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
array $config,
|
array $config,
|
||||||
?string $log_path = null
|
?string $log_path = null
|
||||||
) {
|
) {
|
||||||
$this->api_url = $this->base_url.'/api/v1';
|
$this->api_url = $this->base_url.'/api/v1';
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->logger = new Logger('PhotoPrismUpload');
|
|
||||||
if (empty($log_path)) {
|
if (empty($log_path)) {
|
||||||
$log_path = __DIR__.'/logs/log.log';
|
$log_path = __DIR__.'/logs/log.log';
|
||||||
}
|
}
|
||||||
$handler = new RotatingFileHandler($log_path, 5, Logger::DEBUG, true);
|
LoggerFactory::addHandler(new RotatingFileHandler($log_path, 5, Logger::DEBUG, true));
|
||||||
$this->logger->pushHandler($handler);
|
$this->logger = LoggerFactory::create('PhotoPrismUpload');
|
||||||
if (isset($_SESSION['pp_sessionid'])) {
|
if (isset($_SESSION['pp_sessionid'])) {
|
||||||
$this->session_id = $_SESSION['pp_sessionid'];
|
$this->session_id = $_SESSION['pp_sessionid'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse headers from a cURL HTTP response.
|
||||||
|
* Returns an array with the keys `headers` and `content`.
|
||||||
|
* The former is an array containing the headers (header name as key, value as value).
|
||||||
|
* The latter is a string with the body of the response
|
||||||
|
*
|
||||||
|
* @param string $response The complete response, containing the headers and body
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
private function parseHeaders(string $response): array
|
private function parseHeaders(string $response): array
|
||||||
{
|
{
|
||||||
$response = explode("\r\n\r\n", $response, 2);
|
$response = explode("\r\n\r\n", $response, 2);
|
||||||
@ -63,6 +93,19 @@ class PhotoPrism
|
|||||||
return ['headers' => $header_arr, 'content' => $content];
|
return ['headers' => $header_arr, 'content' => $content];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a HTTP request using cURL
|
||||||
|
* Returns the body of the response
|
||||||
|
*
|
||||||
|
* @param string $method -The HTTP method to use
|
||||||
|
* @param string $path The HTTP request path without the API URL
|
||||||
|
* @param array $data Request data to send
|
||||||
|
* @param string $content_type Content-Type to use
|
||||||
|
*
|
||||||
|
* @throws NetworkException on failure
|
||||||
|
*
|
||||||
|
* @return string The response body
|
||||||
|
*/
|
||||||
private function makeRequest(
|
private function makeRequest(
|
||||||
string $method,
|
string $method,
|
||||||
string $path,
|
string $path,
|
||||||
@ -120,7 +163,10 @@ class PhotoPrism
|
|||||||
}
|
}
|
||||||
if (empty($output) || $output === false) {
|
if (empty($output) || $output === false) {
|
||||||
$e = new NetworkException("No answer from" . $url, 0);
|
$e = new NetworkException("No answer from" . $url, 0);
|
||||||
$this->logger->error("Error sending request", ['Exception' => $e]);
|
$this->logger->error(
|
||||||
|
"Error sending request. No answer from server",
|
||||||
|
['Exception' => print_r($e, true)]
|
||||||
|
);
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
if ($http_status === 0) {
|
if ($http_status === 0) {
|
||||||
@ -131,13 +177,22 @@ class PhotoPrism
|
|||||||
}
|
}
|
||||||
$result = $this->parseHeaders($output);
|
$result = $this->parseHeaders($output);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->error("Error sending request", ['Exception' => $e]);
|
$this->logger->error("Error sending request", ['Exception' => print_r($e, true)]);
|
||||||
throw new NetworkException("Error sending request to " . $url, 0, $e);
|
throw new NetworkException("Error sending request to " . $url, 0, $e);
|
||||||
}
|
}
|
||||||
return $result['content'];
|
return $result['content'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function login(bool $force = false)
|
/**
|
||||||
|
* Log in to PhotoPrism.
|
||||||
|
* If already logged in nothing happens.
|
||||||
|
* No check whether the session is still valid is performed
|
||||||
|
|
||||||
|
* @throws AuthenticationException on failure
|
||||||
|
*
|
||||||
|
* @param bool $force -Force re-login even if already logged in
|
||||||
|
*/
|
||||||
|
public function login(bool $force = false): void
|
||||||
{
|
{
|
||||||
if (!empty($this->session_id) && !$force) {
|
if (!empty($this->session_id) && !$force) {
|
||||||
$this->logger->info('Skipping login, already logged in');
|
$this->logger->info('Skipping login, already logged in');
|
||||||
@ -158,6 +213,15 @@ class PhotoPrism
|
|||||||
$this->logger->debug('Session ID: ' . $this->session_id);
|
$this->logger->debug('Session ID: ' . $this->session_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches albums from PhotoPrism
|
||||||
|
|
||||||
|
* @throws NetworkException on failure
|
||||||
|
*
|
||||||
|
* @param int $count -Maximum amount of albums to fetch
|
||||||
|
* @param int $offset -Number of albums to skip
|
||||||
|
* @return Album[]
|
||||||
|
*/
|
||||||
public function getAlbums(int $count = 1000, int $offset = 0): array
|
public function getAlbums(int $count = 1000, int $offset = 0): array
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
@ -180,10 +244,19 @@ class PhotoPrism
|
|||||||
return $albums;
|
return $albums;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches albums from PhotoPrism which can be viewed with the provided tokens
|
||||||
|
|
||||||
|
* @throws NetworkException on failure
|
||||||
|
* @param string[] $tokens -A list of tokens by which the albms are filtered
|
||||||
|
* @param int $count -Maximum amount of albums to fetch
|
||||||
|
* @param int $offset -Number of albums to skip
|
||||||
|
* @return Album[]
|
||||||
|
*/
|
||||||
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');
|
$this->logger->debug('getAlbumsByToken');
|
||||||
$albums = $this->getAlbums($count, 0);
|
$albums = $this->getAlbums($count, $offset);
|
||||||
$visibleAlbums = [];
|
$visibleAlbums = [];
|
||||||
foreach ($albums as $album) {
|
foreach ($albums as $album) {
|
||||||
$token = $this->getAlbumToken($album);
|
$token = $this->getAlbumToken($album);
|
||||||
@ -199,10 +272,19 @@ class PhotoPrism
|
|||||||
return $visibleAlbums;
|
return $visibleAlbums;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the secret token of an album
|
||||||
|
|
||||||
|
* @throws NetworkException on failure
|
||||||
|
*
|
||||||
|
* @param Album $album -The album which's toke should be fetched
|
||||||
|
* @return string|null Album token or null if the album is private
|
||||||
|
*/
|
||||||
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');
|
||||||
|
/** @var array $response */
|
||||||
$response = json_decode($res, true)[0];
|
$response = json_decode($res, true)[0];
|
||||||
if (!empty($response['error'])) {
|
if (!empty($response['error'])) {
|
||||||
throw new NetworkException($response['error']);
|
throw new NetworkException($response['error']);
|
||||||
@ -214,12 +296,18 @@ class PhotoPrism
|
|||||||
return $response['Token'];
|
return $response['Token'];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function uploadPhotos(?string $album = null)
|
/**
|
||||||
|
* Upload photos, optionally add them to a specific album
|
||||||
|
|
||||||
|
* @throws NetworkException on failure
|
||||||
|
* @param string|null $album -The album uid to which the photos should be aded
|
||||||
|
*/
|
||||||
|
public function uploadPhotos(?string $album = null): void
|
||||||
{
|
{
|
||||||
$path = time();
|
$path = time();
|
||||||
$url = '/upload/'.$path;
|
$url = '/upload/'.$path;
|
||||||
$import_url = '/import'.$url;
|
$import_url = '/import'.$url;
|
||||||
foreach ($_FILES['files']['tmp_name'] as $key => $value) {
|
foreach (array_keys($_FILES['files']['tmp_name']) as $key) {
|
||||||
$file_tmpname = $_FILES['files']['tmp_name'][$key];
|
$file_tmpname = $_FILES['files']['tmp_name'][$key];
|
||||||
$this->logger->info('Uploading ' . $file_tmpname . ' to ' . $url);
|
$this->logger->info('Uploading ' . $file_tmpname . ' to ' . $url);
|
||||||
$filename = basename($_FILES['files']['name'][$key]);
|
$filename = basename($_FILES['files']['name'][$key]);
|
||||||
@ -231,6 +319,7 @@ class PhotoPrism
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->info('Importing files');
|
$this->logger->info('Importing files');
|
||||||
|
/** @var string[] $albums */
|
||||||
$albums = empty($album) ? [] : [$album];
|
$albums = empty($album) ? [] : [$album];
|
||||||
|
|
||||||
$import_data = ["move" => true, "albums" => $albums];
|
$import_data = ["move" => true, "albums" => $albums];
|
||||||
|
@ -1,20 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace PhotoPrismUpload\Entities;
|
namespace PhotoPrismUpload\Entities;
|
||||||
|
|
||||||
use Monolog\Logger;
|
use PhotoPrismUpload\API\LoggerFactory;
|
||||||
|
|
||||||
|
/** A PhotoPrism Album */
|
||||||
class Album
|
class Album
|
||||||
{
|
{
|
||||||
|
/** @var string $uid Unique Id of the album */
|
||||||
public string $uid = '';
|
public string $uid = '';
|
||||||
|
|
||||||
|
/** @var string $slug URL slug of the album */
|
||||||
public string $slug = '';
|
public string $slug = '';
|
||||||
|
|
||||||
|
/** @var string $title Title of the album */
|
||||||
public string $title = '';
|
public string $title = '';
|
||||||
|
|
||||||
|
/** @var string|null $token Secret token of the album. Needs to be set by the API */
|
||||||
public ?string $token = null;
|
public ?string $token = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new album from the api response
|
||||||
|
*
|
||||||
|
* @param array $response Photoprism API response containing an album object
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
array $response
|
array $response
|
||||||
) {
|
) {
|
||||||
$this->uid = $response['UID'];
|
$this->uid = $response['UID'];
|
||||||
$this->slug = $response['Slug'];
|
$this->slug = $response['Slug'];
|
||||||
$this->title = $response['Title'];
|
$this->title = $response['Title'];
|
||||||
|
$this->logger = LoggerFactory::create('PhotoPrismUpload.Album');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the URL path for this album.
|
||||||
|
* Starts with a leading /
|
||||||
|
* Returns null if the album's token is not set
|
||||||
|
*
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getUrlPath(): ?string
|
||||||
|
{
|
||||||
|
if (empty($this->token)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return "/s/{$this->token}/{$this->slug}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user