implemented upload

This commit is contained in:
aymm 2021-08-26 07:47:56 +00:00
parent 7bfed5a48b
commit b27a3bbc58
2 changed files with 93 additions and 17 deletions

View File

@ -7,11 +7,49 @@ use PhotoPrismUpload\API\PhotoPrism;
?> ?>
<html> <html>
<head> <head>
<style>
.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;
/* display: block; */
}
#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] {
grid-column: 1/3;
grid-row: 3;
justify-self: center;
}
</style>
</head> </head>
<body></body> <body></body>
<?php <?php
$config = require(__DIR__ . '/config.php');
$api = new PhotoPrism($config);
$albums = [];
try {
$api->login();
$albums = $api->getAlbums();
} catch (\Exception $e) {
die('Fehler: ' . $e->getMessage());
}
if (!isset($_POST['submit'])) { if (!isset($_POST['submit'])) {
?> ?>
<script> <script>
@ -21,7 +59,7 @@ if (!isset($_POST['submit'])) {
const isInvalid = window.tooLarge || window.tooManyFiles; const isInvalid = window.tooLarge || window.tooManyFiles;
console.log('Validating', window.tooLarge, window.tooManyFiles, isInvalid); console.log('Validating', window.tooLarge, window.tooManyFiles, isInvalid);
if (isInvalid) { if (isInvalid) {
const errorDiv = document.getElementById('error'); const errorDiv = document.getElementById('error');m
errorDiv.innerText = ''; errorDiv.innerText = '';
errorDiv.innerText += window.tooLarge ? 'Zu groß, Upload muss weniger als 200MB sein. ' : ''; errorDiv.innerText += window.tooLarge ? 'Zu groß, Upload muss weniger als 200MB sein. ' : '';
errorDiv.innerText += window.tooManyFiles ? 'Zu viele Dateien, maximal 200 erlaubt. ' : ''; errorDiv.innerText += window.tooManyFiles ? 'Zu viele Dateien, maximal 200 erlaubt. ' : '';
@ -30,11 +68,22 @@ if (!isset($_POST['submit'])) {
return !isInvalid; return !isInvalid;
} }
</script> </script>
<div class="form-wrapper">
<form method="POST" enctype="multipart/form-data" onsubmit="return validateForm();"> <form method="POST" enctype="multipart/form-data" onsubmit="return validateForm();">
<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" />!-->
<input multiple type="file" name="files[]" id="input" /> <input multiple type="file" name="files[]" id="input" />
<input type="submit" name="submit" value="Upload" /> <input type="submit" name="submit" value="Upload" />
</form> </form>
<div id="error" style="display:none"></div> <div id="error" style="display:none"></div>
</div>
<script> <script>
const input = document.getElementById('input') const input = document.getElementById('input')
@ -60,12 +109,10 @@ if (!isset($_POST['submit'])) {
<?php <?php
die(); die();
} }
$config = require(__DIR__ . '/config.php');
$api = new PhotoPrism($config);
try { try {
$api->login(); $api->uploadPhotos($_POST['album']);
$api->getAlbums();
} catch (\Exception $e) { } catch (\Exception $e) {
die('Fehler: ' . $e->getMessage()); die('Fehler: ' . $e->getMessage());
} }
?>
Erfolg! <a href=".">Zurück</a>

View File

@ -76,14 +76,18 @@ class PhotoPrism
$result = null; $result = null;
try { try {
if (is_array($data) && $method !== 'post-raw') { if (is_array($data) && $method !== 'post-raw') {
$query_data = $content_type === 'application/json' ? json_encode($data) : http_build_query($data); if ($content_type === 'application/json') {
$query_data = json_encode($data);
} elseif ($content_type === 'multipart/form-data') {
$query_data = $data;
} else {
$query_data = http_build_query($data);
}
} }
$ch = curl_init(); $ch = curl_init();
if ($method === 'get' && !empty($query_data)) { if ($method === 'get' && !empty($query_data)) {
$url .= '?'.$query_data; $url .= '?'.$query_data;
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_data);
} }
if ($method !== 'get') { if ($method !== 'get') {
$headers[] = 'Content-Type: '.$content_type; $headers[] = 'Content-Type: '.$content_type;
@ -95,7 +99,7 @@ class PhotoPrism
$this->logger->info($method .' request to ' . $url); $this->logger->info($method .' request to ' . $url);
$this->logger->debug('Headers ' . json_encode($headers)); $this->logger->debug('Headers ' . json_encode($headers));
$this->logger->debug('postfields data ' . json_encode($data)); $this->logger->debug('postfields data ' . json_encode($data));
$this->logger->debug('postfields ' . $query_data); $this->logger->debug('postfields ' . json_encode($query_data));
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $method !== 'get'); curl_setopt($ch, CURLOPT_POST, $method !== 'get');
@ -103,6 +107,9 @@ class PhotoPrism
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if ($method !== 'get' && !empty($query_data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_data);
}
$output = curl_exec($ch); $output = curl_exec($ch);
$request = curl_getinfo($ch, CURLINFO_HEADER_OUT); $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
@ -173,5 +180,27 @@ class PhotoPrism
return $albums; return $albums;
} }
#https://photos.phlaym.net/api/v1/albums?q=&count=1000&offset=0&type=album public function uploadPhotos(?string $album = null)
{
$path = time();
$url = '/upload/'.$path;
$import_url = '/import'.$url;
foreach ($_FILES['files']['tmp_name'] as $key => $value) {
$file_tmpname = $_FILES['files']['tmp_name'][$key];
$this->logger->info('Uploading ' . $file_tmpname . ' to ' . $url);
$filename = basename($_FILES['files']['name'][$key]);
$cFile = curl_file_create($file_tmpname, $_FILES['files']['type'][$key], $filename);
$data = ['files' => $cFile];
$res = $this->makeRequest('POST', $url, $data, 'multipart/form-data');
$this->logger->info('Upload result: ' . $res);
}
$this->logger->info('Importing files');
$albums = empty($album) ? [] : [$album];
$import_data = ["move" => true, "albums" => $albums];
$res = $this->makeRequest('POST', $import_url, $import_data);
$this->logger->info('Import result: ' . $res);
}
} }