added enabled/disable to styles
This commit is contained in:
@@ -52,23 +52,127 @@ class ImageController extends Controller
|
||||
]);
|
||||
|
||||
$image = Image::find($request->image_id);
|
||||
$style = Style::with('aiModel.apiProviders')->find($request->style_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 API provider
|
||||
$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->name);
|
||||
$result = $plugin->styleChangeRequest($style->prompt, $image->uuid, $style->parameters); // Annahme: Image Model hat eine UUID
|
||||
$plugin = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
|
||||
|
||||
// Hier müsste die Logik zum Speichern des neuen Bildes und dessen Verknüpfung implementiert werden
|
||||
// Fürs Erste geben wir das Ergebnis der API zurück
|
||||
return response()->json($result);
|
||||
// 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);
|
||||
}
|
||||
@@ -118,5 +222,4 @@ class ImageController extends Controller
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user