the RunwareAI Plugin is working now

This commit is contained in:
2025-07-30 23:24:47 +02:00
parent 07c6786bda
commit 47860b4b7d
35 changed files with 544 additions and 218 deletions

View File

@@ -8,18 +8,48 @@ use App\Api\Plugins\PluginLoader;
use App\Models\ApiProvider;
use App\Models\Style;
use App\Models\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
class ImageController extends Controller
{
public function index()
{
$images = Storage::disk('public')->files('uploads');
$publicUploadsPath = public_path('storage/uploads');
// Ensure the directory exists
if (!File::exists($publicUploadsPath)) {
File::makeDirectory($publicUploadsPath, 0755, true);
}
// Get files from the public/storage/uploads directory
$diskFiles = File::files($publicUploadsPath);
$diskImagePaths = [];
foreach ($diskFiles as $file) {
// Store path relative to public/storage/
$diskImagePaths[] = 'uploads/' . $file->getFilename();
}
$dbImagePaths = Image::pluck('path')->toArray();
// Add images from disk that are not in the database
$imagesToAdd = array_diff($diskImagePaths, $dbImagePaths);
foreach ($imagesToAdd as $path) {
Image::create(['path' => $path]);
}
// Remove images from database that are not on disk
$imagesToRemove = array_diff($dbImagePaths, $diskImagePaths);
Image::whereIn('path', $imagesToRemove)->delete();
// Fetch all images from the database after synchronization
$images = Image::orderBy('updated_at', 'desc')->get();
$formattedImages = [];
foreach ($images as $image) {
$formattedImages[] = [
'path' => Storage::url($image),
'name' => basename($image),
'image_id' => $image->id,
'path' => asset('storage/' . $image->path),
'name' => basename($image->path),
'is_temp' => (bool) $image->is_temp,
];
}
return response()->json($formattedImages);
@@ -31,21 +61,39 @@ class ImageController extends Controller
'image' => 'required|image|max:10240', // Max 10MB
]);
$path = $request->file('image')->store('uploads', 'public');
$file = $request->file('image');
$fileName = uniqid() . '.' . $file->getClientOriginalExtension();
$destinationPath = public_path('storage/uploads');
// Ensure the directory exists
if (!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0755, true);
}
$file->move($destinationPath, $fileName);
$relativePath = 'uploads/' . $fileName; // Path relative to public/storage/
$image = Image::create([
'path' => $path,
'path' => $relativePath,
]);
return response()->json([
'message' => __('api.image_uploaded_successfully'),
'image_id' => $image->id,
'path' => Storage::url($path),
'path' => asset('storage/' . $relativePath),
]);
}
public function styleChangeRequest(Request $request)
{
// Same-origin check
$appUrl = config('app.url');
$referer = $request->headers->get('referer');
if ($referer && parse_url($referer, PHP_URL_HOST) !== parse_url($appUrl, PHP_URL_HOST)) {
return response()->json(['error' => 'Unauthorized: Request must originate from the same domain.'], 403);
}
$request->validate([
'image_id' => 'required|exists:images,id',
'style_id' => 'required|exists:styles,id',
@@ -69,32 +117,29 @@ class ImageController extends Controller
}
$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.');
}
$result = $plugin->processImageStyleChange(
public_path('storage/' . $image->path),
$style->prompt,
$style->aiModel->model_id,
$style->parameters
);
$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;
$newImagePathRelative = 'uploads/' . $newImageName; // Path relative to public/storage/
$newImageFullPath = public_path('storage/' . $newImagePathRelative); // Full path to save
Storage::disk('public')->put($newImagePath, $decodedImage);
// Ensure the directory exists
if (!File::exists(public_path('storage/uploads'))) {
File::makeDirectory(public_path('storage/uploads'), 0755, true);
}
File::put($newImageFullPath, $decodedImage); // Save using File facade
$newImage = Image::create([
'path' => $newImagePath,
'path' => $newImagePathRelative, // Store relative path
'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
@@ -104,7 +149,7 @@ class ImageController extends Controller
'message' => 'Style change successful',
'styled_image' => [
'id' => $newImage->id,
'path' => Storage::url($newImage->path),
'path' => asset('storage/' . $newImage->path),
'is_temp' => $newImage->is_temp,
],
]);
@@ -133,44 +178,9 @@ class ImageController extends Controller
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);
// Delete from the public/storage directory
File::delete(public_path('storage/' . $image->path));
$image->delete();
return response()->json(['message' => __('api.image_deleted_successfully')]);
} catch (\Exception $e) {
@@ -222,4 +232,5 @@ class ImageController extends Controller
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
}

View File

@@ -10,7 +10,8 @@ class StyleController extends Controller
{
public function index()
{
$styles = Style::where('enabled', true)
$styles = Style::with(['aiModel.apiProviders'])
->where('enabled', true)
->whereHas('aiModel', function ($query) {
$query->where('enabled', true);
$query->whereHas('apiProviders', function ($query) {
@@ -19,6 +20,10 @@ class StyleController extends Controller
})
->get();
if ($styles->isEmpty()) {
return response()->json(['message' => __('api.no_styles_available')], 404);
}
return response()->json($styles);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Setting;
use Inertia\Inertia;
use Illuminate\Support\Facades\Lang;
@@ -12,9 +13,11 @@ class HomeController extends Controller
{
$locale = app()->getLocale();
$translations = Lang::get('messages', [], $locale);
$galleryHeading = Setting::where('key', 'gallery_heading')->first()->value ?? 'Style Gallery';
return Inertia::render('Home', [
'translations' => $translations,
'galleryHeading' => $galleryHeading,
]);
}
}