photoprismupload/index.php

246 lines
8.5 KiB
PHP
Raw Normal View History

2021-08-24 17:59:30 +00:00
<?php
session_start();
require __DIR__ . '/vendor/autoload.php';
use PhotoPrismUpload\API\PhotoPrism;
2021-11-26 20:15:31 +00:00
$footer = '<footer style="position: fixed;bottom: 0;left: 0;"><a href="/git/phlaym/photoprismupload">Ich bin Open Source</a></footer>';
2021-08-24 17:59:30 +00:00
?>
<html>
<head>
2021-11-26 20:02:49 +00:00
<meta charset="UTF-8">
2021-11-26 19:57:46 +00:00
<meta name="color-scheme" content="dark light">
2021-11-26 20:02:49 +00:00
<title>Photoprism Upload</title>
<meta name="description" content="Eine Seite um Photos zur Photoprism Instanz hochzuladen">
<meta name="author" content="Max Nuding">
<meta http-equiv="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2021-11-26 20:15:31 +00:00
<meta http-equiv=”content-language” content=”de-de”/>
2021-08-26 07:47:56 +00:00
<style>
2021-11-26 19:57:46 +00:00
::root {
background-color: white;
color: black;
color-scheme: light dark;
}
@media screen and (prefers-color-scheme: dark) {
:root {
background-color: rgb(54, 54, 54);
color: white;
}
2021-11-26 20:15:31 +00:00
a {
color: rgb(105, 105, 242);
}
a:visited {
color: rgb(152, 95, 215);
}
2021-11-26 19:57:46 +00:00
}
2021-08-26 07:47:56 +00:00
.form-wrapper {
display: grid;
grid-template-rows: auto auto auto;
grid-auto-columns: auto auto;
max-width: 300px;
}
label[for="album"] {
grid-column: 1;
grid-row: 1;
}
#album {
grid-column: 2;
grid-row: 1;
}
#input {
grid-row: 2;
grid-column: 1/3;
}
.form-wrapper > form:nth-child(1) {
display: inherit;
}
input[type=submit] {
2021-11-26 19:43:53 +00:00
grid-column: 2;
grid-row: 3;
justify-self: right;
}
#error, #fileProgress, #totalProgress, label[for="fileProgress"], label[for="totalProgress"] {
display:none;
grid-column: 1;
}
#uploadForm {
grid-row: 1;
grid-column: 1;
}
#error {
grid-row: 2;
}
label[for="fileProgress"] {
2021-08-26 07:47:56 +00:00
grid-row: 3;
2021-11-26 19:43:53 +00:00
}
#fileProgress {
grid-row: 4;
width: 100%;
}
label[for="totalProgress"]{
grid-row: 5;
}
#totalProgress {
grid-row: 6;
width: 100%;
2021-08-26 07:47:56 +00:00
}
2021-11-26 20:15:31 +00:00
footer {
margin: 8px;
}
2021-08-26 07:47:56 +00:00
</style>
2021-08-24 17:59:30 +00:00
</head>
<body>
2021-08-24 17:59:30 +00:00
<?php
2021-08-26 07:47:56 +00:00
$config = require(__DIR__ . '/config.php');
$api = new PhotoPrism($config);
$albums = [];
try {
$api->login();
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage().'</body></html>');
2021-08-26 07:47:56 +00:00
}
2021-08-24 17:59:30 +00:00
if (!isset($_POST['submit'])) {
2021-08-26 08:56:59 +00:00
if (!isset($_GET['token'])) {
die('Sorry, kein Zugriff</body></html>');
2021-08-26 08:56:59 +00:00
}
$token = $_GET['token'];
$tokens = explode(',', $token);
try {
$albums = $api->getAlbumsByTokens($tokens);
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage().'</body></html>');
2021-08-26 08:56:59 +00:00
}
if (empty($albums) && (empty($config['noAlbumToken']) || !in_array($config['noAlbumToken'], $tokens))) {
die('Falscher Token</body></html>');
2021-08-26 08:56:59 +00:00
}
2021-08-24 17:59:30 +00:00
?>
<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) {
2021-11-26 19:43:53 +00:00
const errorDiv = document.getElementById('error');
2021-08-24 17:59:30 +00:00
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>
2021-08-26 07:47:56 +00:00
<div class="form-wrapper">
2021-11-26 19:43:53 +00:00
<!--<form method="POST" enctype="multipart/form-data" onsubmit="return validateForm();" id="uploadForm"> !-->
<form method="POST" enctype="multipart/form-data" id="uploadForm">
2021-08-26 07:47:56 +00:00
<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';
} ?>
</select>
<!--<label></label>
<input type="text" />!-->
2021-08-26 08:56:59 +00:00
<input multiple type="file" name="files[]" id="input" required/>
2021-08-26 07:47:56 +00:00
<input type="submit" name="submit" value="Upload" />
</form>
2021-11-26 19:43:53 +00:00
<div id="error"></div>
<label for="fileProgress">Datei:</label>
<progress id="fileProgress"></progress>
<label for="totalProgress">Gesamt:</label>
<progress max="0" value="0" id="totalProgress"></progress>
2021-08-26 07:47:56 +00:00
</div>
2021-08-24 17:59:30 +00:00
<script>
2021-11-26 19:43:53 +00:00
const form = document.getElementById('uploadForm');
const submitButton = form.querySelector('input[type=submit]');
const albumInput = form.querySelector('select[name=album]');
const input = document.getElementById('input');
const fileProgress = document.getElementById('fileProgress');
const totalProgress = document.getElementById('totalProgress');
const fileProgressLabel = document.querySelector('label[for=fileProgress]');
const totalProgressLabel = document.querySelector('label[for=totalProgress]');
const errorDiv = document.getElementById('error');
async function postData(url, data = {}, method = 'POST') {
const response = await fetch(url, {
method: method,
body: data
});
return response;
}
form.addEventListener('submit', async function(event) {
event.preventDefault();
errorDiv.innerText = '';
fileProgressLabel.style.display = 'inherit';
totalProgressLabel.style.display = 'inherit';
fileProgress.style.display = 'inherit';
totalProgress.style.display = 'inherit';
let idx = 0;
for (file of fileList) {
console.log('Starting upload', file);
fileProgressLabel.innerText = `Datei: ${file.name}`
totalProgressLabel.innerText = `Gesamt: ${idx} von ${fileList.length} fertig`;
totalProgress.value = idx++;
let formData = new FormData();
formData.set(input.name, file);
formData.set(submitButton.name, submitButton.value);
formData.set(albumInput.name, albumInput.value);
try {
let resp = await postData(form.action, formData, form.method);
} catch(e) {
console.error('Error uploading file', e);
errorDiv.innerHTML += `Fehler beim Upload der Datei ${file.name}: ${e}<br />`;
errorDiv.style.display = 'block';
}
}
totalProgressLabel.innerText = `Gesamt: ${idx} von ${fileList.length} fertig`;
totalProgress.value = idx++;
fileProgress.max = 1;
fileProgress.value = 1;
});
2021-08-24 17:59:30 +00:00
2021-11-26 19:43:53 +00:00
let fileList = [];
2021-08-24 17:59:30 +00:00
input.addEventListener('change', (event) => {
const errorDiv = document.getElementById('error');
2021-11-26 19:43:53 +00:00
const totalProgress = document.getElementById('totalProgress');
2021-08-24 17:59:30 +00:00
errorDiv.innerText = '';
errorDiv.style.display = 'none';
const target = event.target;
let totalSize = 0;
2021-11-26 19:43:53 +00:00
fileList = [];
2021-08-24 17:59:30 +00:00
if (target.files) {
for (file of target.files) {
totalSize += file.size;
2021-11-26 19:43:53 +00:00
fileList.push(file);
2021-08-24 17:59:30 +00:00
}
}
2021-11-26 19:43:53 +00:00
totalProgress.max = fileList.length;
2021-08-24 17:59:30 +00:00
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);
});
</script>
<?php
2021-11-26 20:15:31 +00:00
die($footer.'</body></html>');
2021-08-24 17:59:30 +00:00
}
try {
2021-08-26 07:47:56 +00:00
$api->uploadPhotos($_POST['album']);
2021-08-24 17:59:30 +00:00
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage().'</body></html>');
2021-08-24 17:59:30 +00:00
}
2021-08-26 07:47:56 +00:00
?>
Erfolg! <a href=".">Zurück</a>
2021-11-26 19:43:53 +00:00
</body></html>