Compare commits

10 Commits

Author SHA1 Message Date
1ec2be5925 Fixed missing file type validation 2021-11-27 13:05:26 +01:00
047088b622 removed unnnedded call 2021-11-27 12:53:48 +01:00
29b429e239 Added documentation 2021-11-27 12:52:29 +01:00
ae4eb30a73 Improved formatting 2021-11-27 10:59:29 +01:00
19db5f3575 Resolved #3 Added link to selected album 2021-11-27 10:51:30 +01:00
5ef5012224 Fixed alignment 2021-11-27 09:24:10 +01:00
f8e72356a3 resolved #2 implemented automatic album selection 2021-11-27 09:12:01 +01:00
1247d3c969 fixed nova configuration 2021-11-27 09:00:55 +01:00
0b613eb8cf Added footer everywhere 2021-11-27 08:55:44 +01:00
966e511140 Fixed validation 2021-11-27 08:01:15 +01:00
6 changed files with 306 additions and 55 deletions

View File

@ -1,4 +1,6 @@
{
"workspace.art_style" : 0,
"workspace.name" : "PhotoPrismUpload"
"workspace.name" : "PhotoPrismUpload",
"workspace.preview_type" : "custom",
"workspace.preview_url" : "http:\/\/phlaym.net\/photoupload\/?token=1g29eaapq6"
}

View File

@ -3,5 +3,7 @@
return [
'username' => '',
'password' => ''
'noAlbumToken' => ''
'noAlbumToken' => '',
'fileUploadLimitMb' => 1024,
'maximumNumberOfFilesPerUpload' => 200
];

168
index.php
View File

@ -1,9 +1,15 @@
<?php
session_start();
/** require autoloading to manage namespaces */
require __DIR__ . '/vendor/autoload.php';
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>
<head>
@ -36,12 +42,12 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
.form-wrapper {
display: grid;
grid-template-rows: auto auto auto;
grid-auto-columns: auto auto;
max-width: 300px;
grid-auto-columns: minmax(auto, 300px) auto;
}
label[for="album"] {
grid-column: 1;
grid-row: 1;
align-self: center;
}
#album {
grid-column: 2;
@ -59,7 +65,11 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
grid-row: 3;
justify-self: right;
}
#error, #fileProgress, #totalProgress, label[for="fileProgress"], label[for="totalProgress"] {
#error,
#fileProgress,
#totalProgress,
label[for="fileProgress"],
label[for="totalProgress"] {
display:none;
grid-column: 1;
}
@ -69,6 +79,7 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
}
#error {
grid-row: 2;
grid-column: 1/3;
}
label[for="fileProgress"] {
grid-row: 3;
@ -84,6 +95,9 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
grid-row: 6;
width: 100%;
}
#viewAlbum {
grid-row: 7;
}
footer {
margin: 8px;
}
@ -92,58 +106,68 @@ $footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phla
<body>
<?php
/** @var array $config configuration options */
$config = require(__DIR__ . '/config.php');
/** @var PhotoPrism $api API object to interface with PhotoPrism */
$api = new PhotoPrism($config);
/** @var Album[] $albums List of PhotoPrism albums */
$albums = [];
try {
$api->login();
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage().'</body></html>');
die('Fehler: ' . $e->getMessage().$footer.'</body></html>');
}
if (!isset($_POST['submit'])) {
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'];
/** @var string[] $tokens List of album tokens */
$tokens = explode(',', $token);
/** @var string $album_url URL path to the selected album */
$album_url = '/';
try {
$albums = $api->getAlbumsByTokens($tokens);
} 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))) {
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">
<!--<form method="POST" enctype="multipart/form-data" onsubmit="return validateForm();" id="uploadForm"> !-->
<form method="POST" enctype="multipart/form-data" id="uploadForm">
<label for="album">Zu Album hinzufügen</label>
<select name="album" id="album">
<option value="">---</option>
<?php foreach ($albums as $album) {
echo '<option value="' . $album->uid . '">' . $album->title . '</option>\n';
} ?>
<option value="" data-url="/">---</option>
<?php
/** @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>
<!--<label></label>
<input type="text" />!-->
<input multiple type="file" name="files[]" id="input" required/>
<input type="submit" name="submit" value="Upload" />
</form>
@ -152,8 +176,11 @@ if (!isset($_POST['submit'])) {
<progress id="fileProgress"></progress>
<label for="totalProgress">Gesamt:</label>
<progress max="0" value="0" id="totalProgress"></progress>
<a href="<?=$album_url;?>" target="_blank" id="viewAlbum">Album ansehen</a>
</div>
<script>
window.tooLarge = false;
window.tooManyFiles = false;
const form = document.getElementById('uploadForm');
const submitButton = form.querySelector('input[type=submit]');
const albumInput = form.querySelector('select[name=album]');
@ -163,6 +190,7 @@ if (!isset($_POST['submit'])) {
const fileProgressLabel = document.querySelector('label[for=fileProgress]');
const totalProgressLabel = document.querySelector('label[for=totalProgress]');
const errorDiv = document.getElementById('error');
const albumAnchor = document.getElementById('viewAlbum');
async function postData(url, data = {}, method = 'POST') {
const response = await fetch(url, {
@ -172,8 +200,31 @@ if (!isset($_POST['submit'])) {
return response;
}
function validateFileType(file) {
if (file.type && (file.type.startsWith('image/') || file.type.startsWith('video/'))) {
return true;
}
const parts = file.name.split('.');
const extension = parts.length > 0 ? parts[parts.length-1] : '';
if (['jpg', 'jpeg', 'png', 'heic', 'heif', 'mov', 'mp4', 'mkv'].includes(extension)) {
return true;
}
console.warn('Invalid file type', extension);
return false;
}
albumInput.addEventListener('change', (event) => {
console.log(event);
albumAnchor.href = `https://photos.phlaym.net${albumInput.selectedOptions[0].dataset.url}`;
});
form.addEventListener('submit', async function(event) {
event.preventDefault();
const isInvalid = window.tooLarge || window.tooManyFiles;
if (isInvalid) {
console.error('Aborting upload! Too many fiels or fiels too large');
return;
}
errorDiv.innerText = '';
fileProgressLabel.style.display = 'inherit';
@ -210,35 +261,72 @@ if (!isset($_POST['submit'])) {
let fileList = [];
input.addEventListener('change', (event) => {
const maxFileSize = <?=$config['fileUploadLimitMb'];?>;
const maxAmountOfFiles = <?=$config['maximumNumberOfFilesPerUpload'];?>;
const errorDiv = document.getElementById('error');
const totalProgress = document.getElementById('totalProgress');
errorDiv.innerText = '';
errorDiv.style.display = 'none';
submitButton.disabled = false;
const target = event.target;
let totalSize = 0;
fileList = [];
const filesTooLarge = [];
if (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);
} else if(validateFileType(file)) {
fileList.push(file);
}
}
}
totalProgress.max = fileList.length;
const sizeInMb = totalSize / 1000 / 1000;
window.tooLarge = sizeInMb >= 200;
console.log('Total size:', totalSize, 'too large: ', window.tooLarge);
window.tooManyFiles = target.files.length > 200;
console.log('Total files:', target.files.length, 'too many: ', window.tooManyFiles);
window.tooManyFiles = fileList.length > maxAmountOfFiles;
if (window.tooManyFiles) {
errorDiv.style.display = 'block';
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ß und wird beim Upload ignoriert: ${names}.
Jede Datei darf max. ${maxFileSize} MB groß sein.```;
}
if (!fileList.length) {
submitButton.disabled = true;
errorDiv.innerHTML += 'Keine gültigen Bilder oder Videos gefunden';
}
});
</script>
<?php
die($footer.'</body></html>');
die($footer . '</body></html>');
}
try {
$api->uploadPhotos($_POST['album']);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage().'</body></html>');
die('Fehler: ' . $footer . $e->getMessage() .'</body></html>');
}
?>
Erfolg! <a href=".">Zurück</a>

39
src/API/LoggerFactory.php Normal file
View 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;
}
}

View File

@ -9,31 +9,61 @@ use PhotoPrismUpload\Exceptions\NetworkException;
use PhotoPrismUpload\Exceptions\AuthenticationException;
use PhotoPrismUpload\Entities\Album;
/**
* The main API class to interface with 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 = '';
/** @var string|null $session_id Session id of the currently logged in user */
protected ?string $session_id = null;
/** @var array $config Configuration options */
protected array $config;
/** @var LoggerInterface $logger Logger object */
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(
array $config,
?string $log_path = null
) {
$this->api_url = $this->base_url.'/api/v1';
$this->config = $config;
$this->logger = new Logger('PhotoPrismUpload');
if (empty($log_path)) {
$log_path = __DIR__.'/logs/log.log';
}
$handler = new RotatingFileHandler($log_path, 5, Logger::DEBUG, true);
$this->logger->pushHandler($handler);
LoggerFactory::addHandler(new RotatingFileHandler($log_path, 5, Logger::DEBUG, true));
$this->logger = LoggerFactory::create('PhotoPrismUpload');
if (isset($_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
{
$response = explode("\r\n\r\n", $response, 2);
@ -63,6 +93,19 @@ class PhotoPrism
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(
string $method,
string $path,
@ -112,7 +155,7 @@ class PhotoPrism
}
$output = curl_exec($ch);
$request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
// $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($output === false) {
@ -120,7 +163,10 @@ class PhotoPrism
}
if (empty($output) || $output === false) {
$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;
}
if ($http_status === 0) {
@ -131,13 +177,22 @@ class PhotoPrism
}
$result = $this->parseHeaders($output);
} 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);
}
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) {
$this->logger->info('Skipping login, already logged in');
@ -158,6 +213,15 @@ class PhotoPrism
$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
{
$data = [
@ -180,10 +244,19 @@ class PhotoPrism
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
{
$this->logger->debug('getAlbumsByToken');
$albums = $this->getAlbums($count, 0);
$albums = $this->getAlbums($count, $offset);
$visibleAlbums = [];
foreach ($albums as $album) {
$token = $this->getAlbumToken($album);
@ -199,10 +272,19 @@ class PhotoPrism
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
{
$uid = is_string($album) ? $album : $album->uid;
$res = $this->makeRequest('GET', '/albums/' . $uid . '/links');
/** @var array $response */
$response = json_decode($res, true)[0];
if (!empty($response['error'])) {
throw new NetworkException($response['error']);
@ -214,12 +296,18 @@ class PhotoPrism
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();
$url = '/upload/'.$path;
$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];
$this->logger->info('Uploading ' . $file_tmpname . ' to ' . $url);
$filename = basename($_FILES['files']['name'][$key]);
@ -231,6 +319,7 @@ class PhotoPrism
}
$this->logger->info('Importing files');
/** @var string[] $albums */
$albums = empty($album) ? [] : [$album];
$import_data = ["move" => true, "albums" => $albums];

View File

@ -1,20 +1,51 @@
<?php
namespace PhotoPrismUpload\Entities;
use Monolog\Logger;
use PhotoPrismUpload\API\LoggerFactory;
/** A PhotoPrism Album */
class Album
{
/** @var string $uid Unique Id of the album */
public string $uid = '';
/** @var string $slug URL slug of the album */
public string $slug = '';
/** @var string $title Title of the album */
public string $title = '';
/** @var string|null $token Secret token of the album. Needs to be set by the API */
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(
array $response
) {
$this->uid = $response['UID'];
$this->slug = $response['Slug'];
$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}";
}
}