feat: migrate frame upload to php backend and ntfy
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Check if request is POST
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method not allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$uploadDir = __DIR__ . '/uploads/';
|
||||
if (!is_dir($uploadDir)) {
|
||||
// Attempt to create if not exists (though Docker should map it)
|
||||
@mkdir($uploadDir, 0755, true);
|
||||
}
|
||||
|
||||
// Check if file is uploaded
|
||||
if (!isset($_FILES['background_image']) || $_FILES['background_image']['error'] !== UPLOAD_ERR_OK) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Keine Datei hochgeladen oder Ein Fehler ist aufgetreten.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$file = $_FILES['background_image'];
|
||||
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : 'Keine E-Mail angegeben';
|
||||
|
||||
// Validate file type
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($finfo, $file['tmp_name']);
|
||||
finfo_close($finfo);
|
||||
|
||||
if ($mimeType !== 'image/png') {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Nur PNG-Dateien sind erlaubt.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Generate unique filename
|
||||
$filename = uniqid('frame_') . '_' . time() . '.png';
|
||||
$destination = $uploadDir . $filename;
|
||||
|
||||
// Move uploaded file
|
||||
if (move_uploaded_file($file['tmp_name'], $destination)) {
|
||||
// Build public URL
|
||||
// Assuming the app is accessible at https://schnappix.orfel.de
|
||||
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
|
||||
$host = $_SERVER['HTTP_HOST'];
|
||||
$publicUrl = $protocol . '://' . $host . '/uploads/' . $filename;
|
||||
|
||||
// Send notification to Ntfy
|
||||
$ntfyUrl = 'https://ntfy.orfel.de/Schnappix-Frame-Upload';
|
||||
$message = "Ein neuer Rahmen wurde hochgeladen!\n\nVon: $email\nBild-URL: $publicUrl";
|
||||
|
||||
$ch = curl_init($ntfyUrl);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Title: Neuer Schnappix Hintergrund',
|
||||
'Tags: frame_with_picture,tada',
|
||||
'Click: ' . $publicUrl,
|
||||
'Attach: ' . $publicUrl
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Datei erfolgreich hochgeladen.',
|
||||
'url' => $publicUrl
|
||||
]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Fehler beim Speichern der Datei.']);
|
||||
}
|
||||
Reference in New Issue
Block a user