upgrade to laravel 12 done
This commit is contained in:
@@ -65,9 +65,21 @@ class ComfyUi implements ApiPluginInterface
|
||||
|
||||
public function getProgress(string $imageUUID): array
|
||||
{
|
||||
$this->logDebug('Getting progress for image.', ['image_uuid' => $imageUUID]);
|
||||
// Implement ComfyUI specific progress check
|
||||
return ['progress' => 0];
|
||||
$this->logDebug('Progress updates are handled via WebSocket.', ['image_uuid' => $imageUUID]);
|
||||
return ['progress' => 0]; // Progress is now handled by WebSocket
|
||||
}
|
||||
|
||||
private function getHistory(string $promptId): array
|
||||
{
|
||||
// This method is no longer used for progress polling, but might be used for final result retrieval
|
||||
$apiUrl = rtrim($this->apiProvider->api_url, '/');
|
||||
$timeout = 60; // seconds
|
||||
$this->logDebug('ComfyUI History API URL:', ['url' => $apiUrl . '/history/' . $promptId, 'timeout' => $timeout]);
|
||||
$response = Http::timeout($timeout)->get($apiUrl . '/history/' . $promptId);
|
||||
if ($response->failed()) {
|
||||
throw new \Exception('Failed to get history from ComfyUI');
|
||||
}
|
||||
return $response->json();
|
||||
}
|
||||
|
||||
public function processImageStyleChange(string $imagePath, string $prompt, string $modelId, ?string $parameters = null): array
|
||||
@@ -85,10 +97,8 @@ class ComfyUi implements ApiPluginInterface
|
||||
$queueResponse = $this->queuePrompt($promptData);
|
||||
$promptId = $queueResponse['prompt_id'];
|
||||
|
||||
// 4. Wait for and get the result
|
||||
$result = $this->waitForResult($promptId);
|
||||
|
||||
return ['base64Data' => $result];
|
||||
// Return the prompt_id for frontend WebSocket tracking
|
||||
return ['prompt_id' => $promptId];
|
||||
}
|
||||
|
||||
private function uploadImage(string $imagePath): array
|
||||
@@ -96,7 +106,7 @@ class ComfyUi implements ApiPluginInterface
|
||||
$this->logInfo('Uploading image to ComfyUI.', ['image_path' => $imagePath]);
|
||||
$response = Http::attach(
|
||||
'image', file_get_contents($imagePath), basename($imagePath)
|
||||
)->timeout(120)->post(rtrim($this->apiProvider->api_url, '/') . '/upload/image', [
|
||||
)->timeout(60)->post(rtrim($this->apiProvider->api_url, '/') . '/upload/image', [
|
||||
'type' => 'input',
|
||||
'overwrite' => 'false',
|
||||
]);
|
||||
@@ -126,7 +136,7 @@ class ComfyUi implements ApiPluginInterface
|
||||
private function queuePrompt(array $promptData): array
|
||||
{
|
||||
$this->logInfo('Queueing prompt in ComfyUI.');
|
||||
$response = Http::timeout(120)->post(rtrim($this->apiProvider->api_url, '/') . '/prompt', ['prompt' => $promptData]);
|
||||
$response = Http::timeout(60)->post(rtrim($this->apiProvider->api_url, '/') . '/prompt', ['prompt' => $promptData]);
|
||||
|
||||
if ($response->failed()) {
|
||||
$this->logError('Failed to queue prompt in ComfyUI.', ['response' => $response->body()]);
|
||||
@@ -136,30 +146,64 @@ class ComfyUi implements ApiPluginInterface
|
||||
return $response->json();
|
||||
}
|
||||
|
||||
private function waitForResult(string $promptId): string
|
||||
public function waitForResult(string $promptId): string
|
||||
{
|
||||
$this->logInfo('Waiting for ComfyUI result.', ['prompt_id' => $promptId]);
|
||||
while (true) {
|
||||
$response = Http::timeout(120)->get($this->apiProvider->api_url . '/history/' . $promptId);
|
||||
$data = $response->json();
|
||||
set_time_limit(120); // Set maximum execution time for this function
|
||||
$this->logInfo('waitForResult: Waiting for ComfyUI result.', ['prompt_id' => $promptId]);
|
||||
$startTime = microtime(true);
|
||||
$timeout = 180; // seconds
|
||||
|
||||
if (!empty($data[$promptId]['outputs'])) {
|
||||
$outputs = $data[$promptId]['outputs'];
|
||||
// Assuming the first output with an image is the one we want
|
||||
foreach ($outputs as $output) {
|
||||
if (isset($output['images'][0]['type']) && $output['images'][0]['type'] === 'output') {
|
||||
$imageUrl = sprintf('%s/view?filename=%s&subfolder=%s&type=output',
|
||||
$this->apiProvider->api_url,
|
||||
$output['images'][0]['filename'],
|
||||
$output['images'][0]['subfolder']
|
||||
);
|
||||
$image_data = file_get_contents($imageUrl);
|
||||
return base64_encode($image_data);
|
||||
}
|
||||
}
|
||||
while (true) {
|
||||
if (microtime(true) - $startTime > $timeout) {
|
||||
$this->logError('waitForResult: ComfyUI result polling timed out.', ['prompt_id' => $promptId]);
|
||||
throw new \Exception('ComfyUI result polling timed out.');
|
||||
}
|
||||
|
||||
sleep(2); // Wait for 2 seconds before polling again
|
||||
try {
|
||||
$response = Http::timeout(60)->get(rtrim($this->apiProvider->api_url, '/') . '/history/' . $promptId);
|
||||
$this->logDebug('waitForResult: History API response status.', ['status' => $response->status(), 'prompt_id' => $promptId]);
|
||||
|
||||
if ($response->failed()) {
|
||||
$this->logError('waitForResult: Failed to get history from ComfyUI.', ['prompt_id' => $promptId, 'response' => $response->body()]);
|
||||
throw new \Exception('Failed to get history from ComfyUI');
|
||||
}
|
||||
|
||||
$data = $response->json();
|
||||
$this->logDebug('waitForResult: History API response data.', ['data' => $data, 'prompt_id' => $promptId]);
|
||||
|
||||
if (isset($data[$promptId]['outputs'])) {
|
||||
$outputs = $data[$promptId]['outputs'];
|
||||
$this->logInfo('waitForResult: Found outputs in history.', ['prompt_id' => $promptId, 'outputs_count' => count($outputs)]);
|
||||
|
||||
foreach ($outputs as $output) {
|
||||
if (isset($output['images'][0]['type']) && $output['images'][0]['type'] === 'output') {
|
||||
$imageUrl = sprintf('%s/view?filename=%s&subfolder=%s&type=output',
|
||||
rtrim($this->apiProvider->api_url, '/'),
|
||||
$output['images'][0]['filename'],
|
||||
$output['images'][0]['subfolder']
|
||||
);
|
||||
$this->logInfo('waitForResult: Constructed image URL.', ['imageUrl' => $imageUrl, 'prompt_id' => $promptId]);
|
||||
|
||||
$imageResponse = Http::timeout(60)->get($imageUrl);
|
||||
$this->logDebug('waitForResult: Image fetch response status.', ['status' => $imageResponse->status(), 'imageUrl' => $imageUrl]);
|
||||
|
||||
if ($imageResponse->failed()) {
|
||||
$this->logError('waitForResult: Failed to retrieve image data from ComfyUI.', ['imageUrl' => $imageUrl, 'response' => $imageResponse->body()]);
|
||||
throw new \Exception('Failed to retrieve image data from ComfyUI');
|
||||
}
|
||||
$this->logInfo('waitForResult: Successfully retrieved image data.', ['prompt_id' => $promptId]);
|
||||
return base64_encode($imageResponse->body());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->logDebug('waitForResult: No outputs found yet for prompt.', ['prompt_id' => $promptId]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->logError('waitForResult: Exception caught during polling.', ['prompt_id' => $promptId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString()]);
|
||||
throw $e; // Re-throw the exception to be caught by ImageController
|
||||
}
|
||||
|
||||
usleep(500000); // Wait for 0.5 seconds before polling again
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
/**
|
||||
* Define the application's command schedule.
|
||||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
// $schedule->command('inspire')->hourly();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the commands for the application.
|
||||
*/
|
||||
protected function commands(): void
|
||||
{
|
||||
$this->load(__DIR__.'/Commands');
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ class CollectionEloquentBuilder extends Builder
|
||||
return $this->collection->firstWhere('id', $id);
|
||||
}
|
||||
|
||||
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
|
||||
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null, $total = null)
|
||||
{
|
||||
$page = $page ?: Paginator::resolveCurrentPage($pageName);
|
||||
|
||||
|
||||
@@ -45,10 +45,11 @@ class StyleResource extends Resource
|
||||
->rows(5),
|
||||
FileUpload::make('preview_image')
|
||||
->label(__('filament.resource.style.form.preview_image'))
|
||||
->required()
|
||||
->image()
|
||||
->disk('public')
|
||||
->directory('style_previews'),
|
||||
->directory('style_previews')
|
||||
->image()
|
||||
->required()
|
||||
->rules(['mimes:jpeg,png,bmp,gif,webp']),
|
||||
Textarea::make('parameters')
|
||||
->label(__('filament.resource.style.form.parameters'))
|
||||
->nullable()
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Models\ApiProvider;
|
||||
use App\Models\Style;
|
||||
use App\Models\Image;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Setting;
|
||||
|
||||
@@ -75,7 +76,7 @@ class ImageController extends Controller
|
||||
public function upload(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'image' => 'required|image|max:10240', // Max 10MB
|
||||
'image' => 'required|image|mimes:jpeg,png,bmp,gif,webp|max:10240', // Max 10MB
|
||||
]);
|
||||
|
||||
$file = $request->file('image');
|
||||
@@ -156,34 +157,16 @@ class ImageController extends Controller
|
||||
$style->parameters
|
||||
);
|
||||
|
||||
$base64Image = $result['base64Data'];
|
||||
$decodedImage = base64_decode(preg_replace('#^data:image/\w+;base64, #i', '', $base64Image));
|
||||
|
||||
$newImageName = 'styled_' . uniqid() . '.png'; // Assuming PNG for now
|
||||
$newImagePathRelative = 'uploads/' . $newImageName; // Path relative to public/storage/
|
||||
$newImageFullPath = public_path('storage/' . $newImagePathRelative); // Full path to save
|
||||
|
||||
// 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' => $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
|
||||
]);
|
||||
// Update the image model with the ComfyUI prompt_id and style_id
|
||||
$image->comfyui_prompt_id = $result['prompt_id'];
|
||||
$image->style_id = $style->id;
|
||||
$image->save();
|
||||
|
||||
// Return the prompt_id for WebSocket tracking
|
||||
return response()->json([
|
||||
'message' => 'Style change successful',
|
||||
'styled_image' => [
|
||||
'id' => $newImage->id,
|
||||
'path' => asset('storage/' . $newImage->path),
|
||||
'is_temp' => $newImage->is_temp,
|
||||
],
|
||||
'message' => 'Style change request sent.',
|
||||
'prompt_id' => $result['prompt_id'],
|
||||
'image_uuid' => $image->uuid, // Pass image UUID for frontend tracking
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
@@ -243,26 +226,99 @@ class ImageController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function getProgress(Request $request)
|
||||
public function fetchStyledImage(string $promptId)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
Log::info('fetchStyledImage called.', ['prompt_id' => $promptId]);
|
||||
try {
|
||||
$plugin = PluginLoader::getPlugin($apiProvider->name);
|
||||
$progress = $plugin->getProgress($image->uuid); // Annahme: Image Model hat eine UUID
|
||||
return response()->json($progress);
|
||||
// Find the image associated with the prompt_id, eagerly loading relationships
|
||||
$image = Image::with(['style.aiModel.apiProviders' => function ($query) {
|
||||
$query->where('enabled', true);
|
||||
}])->where('comfyui_prompt_id', $promptId)->first();
|
||||
|
||||
if (!$image) {
|
||||
Log::warning('fetchStyledImage: Image not found for prompt_id.', ['prompt_id' => $promptId]);
|
||||
return response()->json(['error' => __('api.image_not_found')], 404);
|
||||
}
|
||||
|
||||
Log::info('fetchStyledImage: Image found.', ['image_id' => $image->id, 'image_uuid' => $image->uuid, 'comfyui_prompt_id' => $image->comfyui_prompt_id]);
|
||||
|
||||
// Get the style and API provider associated with the image
|
||||
$style = $image->style;
|
||||
if (!$style) {
|
||||
Log::warning('fetchStyledImage: Style not found for image.', ['image_id' => $image->id]);
|
||||
return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
|
||||
}
|
||||
Log::info('fetchStyledImage: Style found.', ['style_id' => $style->id, 'style_name' => $style->title]);
|
||||
|
||||
if (!$style->aiModel) {
|
||||
Log::warning('fetchStyledImage: AI Model not found for style.', ['style_id' => $style->id]);
|
||||
return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
|
||||
}
|
||||
Log::info('fetchStyledImage: AI Model found.', ['ai_model_id' => $style->aiModel->id, 'ai_model_name' => $style->aiModel->name]);
|
||||
|
||||
if ($style->aiModel->apiProviders->isEmpty()) {
|
||||
Log::warning('fetchStyledImage: No enabled API Providers found for AI Model.', ['ai_model_id' => $style->aiModel->id]);
|
||||
return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
|
||||
}
|
||||
$apiProvider = $style->aiModel->apiProviders->first();
|
||||
Log::info('fetchStyledImage: API Provider found.', ['api_provider_id' => $apiProvider->id, 'api_provider_name' => $apiProvider->name]);
|
||||
|
||||
Log::info('Fetching base64 image from plugin.', ['prompt_id' => $promptId, 'api_provider' => $apiProvider->name]);
|
||||
// Use the plugin to get the final image data (e.g., from ComfyUI's history/view)
|
||||
$plugin = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
|
||||
$base64Image = $plugin->waitForResult($promptId); // Re-purpose waitForResult for final fetch
|
||||
|
||||
if (empty($base64Image)) {
|
||||
Log::error('Received empty base64 image from plugin.', ['prompt_id' => $promptId]);
|
||||
return response()->json(['error' => 'Received empty image data.'], 500);
|
||||
}
|
||||
|
||||
Log::info('Base64 image received. Decoding and saving.');
|
||||
$decodedImage = base64_decode(preg_replace('#^data:image/\w+;base64, #i', '', $base64Image));
|
||||
|
||||
$newImageName = 'styled_' . uniqid() . '.png';
|
||||
$newImagePathRelative = 'uploads/' . $newImageName;
|
||||
$newImageFullPath = public_path('storage/' . $newImagePathRelative);
|
||||
|
||||
if (!File::exists(public_path('storage/uploads'))) {
|
||||
File::makeDirectory(public_path('storage/uploads'), 0755, true);
|
||||
Log::info('Created uploads directory.', ['path' => public_path('storage/uploads')]);
|
||||
}
|
||||
|
||||
File::put($newImageFullPath, $decodedImage); // Save using File facade
|
||||
Log::info('Image saved to disk.', ['path' => $newImageFullPath]);
|
||||
|
||||
$newImage = Image::create([
|
||||
'path' => $newImagePathRelative, // Store relative path
|
||||
'original_image_id' => $image->id,
|
||||
'style_id' => $style->id,
|
||||
'is_temp' => true,
|
||||
]);
|
||||
|
||||
Log::info('New image record created in database.', ['image_id' => $newImage->id, 'path' => $newImage->path]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Styled image fetched successfully',
|
||||
'styled_image' => [
|
||||
'id' => $newImage->id,
|
||||
'path' => asset('storage/' . $newImage->path),
|
||||
'is_temp' => $newImage->is_temp,
|
||||
],
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error in fetchStyledImage: ' . $e->getMessage(), ['exception' => $e]);
|
||||
return response()->json(['error' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getComfyUiUrl()
|
||||
{
|
||||
$apiProvider = ApiProvider::where('plugin', 'comfyui')->where('enabled', true)->first();
|
||||
|
||||
if (!$apiProvider) {
|
||||
return response()->json(['error' => 'No enabled ComfyUI API provider found.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['comfyui_url' => rtrim($apiProvider->api_url, '/')]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http;
|
||||
|
||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||
|
||||
class Kernel extends HttpKernel
|
||||
{
|
||||
/**
|
||||
* The application's global HTTP middleware stack.
|
||||
*
|
||||
* These middleware are run during every request to your application.
|
||||
*
|
||||
* @var array<int, class-string|string>
|
||||
*/
|
||||
protected $middleware = [
|
||||
// \App\Http\Middleware\TrustHosts::class,
|
||||
\App\Http\Middleware\TrustProxies::class,
|
||||
\Illuminate\Http\Middleware\HandleCors::class,
|
||||
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's route middleware groups.
|
||||
*
|
||||
* @var array<string, array<int, class-string|string>>
|
||||
*/
|
||||
protected $middlewareGroups = [
|
||||
'web' => [
|
||||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\SetContentSecurityPolicy::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\App\Http\Middleware\HandleInertiaRequests::class,
|
||||
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The application's middleware aliases.
|
||||
*
|
||||
* Aliases may be used to conveniently assign middleware to routes and groups.
|
||||
*
|
||||
* @var array<string, class-string|string>
|
||||
*/
|
||||
protected $middlewareAliases = [
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
|
||||
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
|
||||
'signed' => \App\Http\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
];
|
||||
}
|
||||
@@ -17,5 +17,11 @@ class Image extends Model
|
||||
'style_id',
|
||||
'is_temp',
|
||||
'is_public',
|
||||
'comfyui_prompt_id',
|
||||
];
|
||||
|
||||
public function style()
|
||||
{
|
||||
return $this->belongsTo(Style::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user