225 lines
7.6 KiB
PHP
225 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Api\Plugins\PluginLoader;
|
|
use App\Models\ApiProvider;
|
|
use App\Models\Style;
|
|
use App\Models\Image;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ImageController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$images = Storage::disk('public')->files('uploads');
|
|
$formattedImages = [];
|
|
foreach ($images as $image) {
|
|
$formattedImages[] = [
|
|
'path' => Storage::url($image),
|
|
'name' => basename($image),
|
|
];
|
|
}
|
|
return response()->json($formattedImages);
|
|
}
|
|
|
|
public function upload(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image' => 'required|image|max:10240', // Max 10MB
|
|
]);
|
|
|
|
$path = $request->file('image')->store('uploads', 'public');
|
|
|
|
$image = Image::create([
|
|
'path' => $path,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => __('api.image_uploaded_successfully'),
|
|
'image_id' => $image->id,
|
|
'path' => Storage::url($path),
|
|
]);
|
|
}
|
|
|
|
public function styleChangeRequest(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image_id' => 'required|exists:images,id',
|
|
'style_id' => 'required|exists:styles,id',
|
|
]);
|
|
|
|
$image = Image::find($request->image_id);
|
|
$style = Style::with(['aiModel' => function ($query) {
|
|
$query->where('enabled', true)->with(['apiProviders' => function ($query) {
|
|
$query->where('enabled', true);
|
|
}]);
|
|
}])->find($request->style_id);
|
|
|
|
if (!$style || !$style->aiModel || $style->aiModel->apiProviders->isEmpty()) {
|
|
return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
|
|
}
|
|
|
|
try {
|
|
$apiProvider = $style->aiModel->apiProviders->first(); // Get the first enabled API provider
|
|
if (!$apiProvider) {
|
|
return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
|
|
}
|
|
$plugin = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
|
|
|
|
// Step 1: Upload the original image
|
|
$originalImagePath = Storage::disk('public')->path($image->path);
|
|
$uploadResult = $plugin->upload($originalImagePath);
|
|
|
|
if (!isset($uploadResult['imageUUID'])) {
|
|
throw new \Exception('Image upload to AI service failed or returned no UUID.');
|
|
}
|
|
$seedImageUUID = $uploadResult['imageUUID'];
|
|
|
|
// Step 2: Request style change using the uploaded image's UUID
|
|
$result = $plugin->styleChangeRequest($style->prompt, $seedImageUUID, $style->parameters);
|
|
|
|
if (!isset($result['base64Data'])) {
|
|
throw new \Exception('AI service did not return base64 image data.');
|
|
}
|
|
|
|
$base64Image = $result['base64Data'];
|
|
$decodedImage = base64_decode(preg_replace('#^data:image/\w+;base64, #i', '', $base64Image));
|
|
|
|
$newImageName = 'styled_' . uniqid() . '.png'; // Assuming PNG for now
|
|
$newImagePath = 'uploads/' . $newImageName;
|
|
|
|
Storage::disk('public')->put($newImagePath, $decodedImage);
|
|
|
|
$newImage = Image::create([
|
|
'path' => $newImagePath,
|
|
'original_image_id' => $image->id, // Link to original image
|
|
'style_id' => $style->id, // Link to applied style
|
|
'is_temp' => true, // Mark as temporary until user keeps it
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Style change successful',
|
|
'styled_image' => [
|
|
'id' => $newImage->id,
|
|
'path' => Storage::url($newImage->path),
|
|
'is_temp' => $newImage->is_temp,
|
|
],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function keepImage(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image_id' => 'required|exists:images,id',
|
|
]);
|
|
|
|
$image = Image::find($request->image_id);
|
|
|
|
if (!$image) {
|
|
return response()->json(['error' => __('api.image_not_found')], 404);
|
|
}
|
|
|
|
$image->is_temp = false;
|
|
$image->save();
|
|
|
|
return response()->json(['message' => __('api.image_kept_successfully')]);
|
|
}
|
|
|
|
public function deleteImage(Image $image)
|
|
{
|
|
// Ensure the image is temporary or belongs to the authenticated user if not temporary
|
|
// For simplicity, we'll allow deletion of any image passed for now.
|
|
// In a real app, you'd add authorization checks here.
|
|
|
|
try {
|
|
Storage::disk('public')->delete($image->path);
|
|
$image->delete();
|
|
return response()->json(['message' => __('api.image_deleted_successfully')]);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function keepImage(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image_id' => 'required|exists:images,id',
|
|
]);
|
|
|
|
$image = Image::find($request->image_id);
|
|
|
|
if (!$image) {
|
|
return response()->json(['error' => __('api.image_not_found')], 404);
|
|
}
|
|
|
|
try {
|
|
$image->is_temp = false;
|
|
$image->save();
|
|
return response()->json(['message' => __('api.image_kept_successfully')]);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function deleteImage(Image $image)
|
|
{
|
|
try {
|
|
Storage::disk('public')->delete($image->path);
|
|
$image->delete();
|
|
return response()->json(['message' => __('api.image_deleted_successfully')]);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function getStatus(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image_id' => 'required|exists:images,id',
|
|
'api_provider_name' => 'required|string',
|
|
]);
|
|
|
|
$image = Image::find($request->image_id);
|
|
$apiProvider = ApiProvider::where('name', $request->api_provider_name)->first();
|
|
|
|
if (!$image || !$apiProvider) {
|
|
return response()->json(['error' => __('api.image_or_provider_not_found')], 404);
|
|
}
|
|
|
|
try {
|
|
$plugin = PluginLoader::getPlugin($apiProvider->name);
|
|
$status = $plugin->getStatus($image->uuid); // Annahme: Image Model hat eine UUID
|
|
return response()->json($status);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function getProgress(Request $request)
|
|
{
|
|
$request->validate([
|
|
'image_id' => 'required|exists:images,id',
|
|
'api_provider_name' => 'required|string',
|
|
]);
|
|
|
|
$image = Image::find($request->image_id);
|
|
$apiProvider = ApiProvider::where('name', $request->api_provider_name)->first();
|
|
|
|
if (!$image || !$apiProvider) {
|
|
return response()->json(['error' => __('api.image_or_provider_not_found')], 404);
|
|
}
|
|
|
|
try {
|
|
$plugin = PluginLoader::getPlugin($apiProvider->name);
|
|
$progress = $plugin->getProgress($image->uuid); // Annahme: Image Model hat eine UUID
|
|
return response()->json($progress);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
} |