mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-14 15:37:53 +00:00
feat(#102): added support for Alias plugin
This commit is contained in:
parent
0d6079db8b
commit
3f98a70ad9
5 changed files with 222 additions and 32 deletions
|
|
@ -613,7 +613,7 @@ Route::post('plugin_settings/{uuid}/image', function (Request $request, string $
|
|||
}
|
||||
|
||||
// Generate a new UUID for each image upload to prevent device caching
|
||||
$imageUuid = \Illuminate\Support\Str::uuid()->toString();
|
||||
$imageUuid = Str::uuid()->toString();
|
||||
$filename = $imageUuid.'.'.$extension;
|
||||
$path = 'images/generated/'.$filename;
|
||||
|
||||
|
|
@ -678,3 +678,90 @@ Route::post('plugin_settings/{trmnlp_id}/archive', function (Request $request, s
|
|||
],
|
||||
]);
|
||||
})->middleware('auth:sanctum');
|
||||
|
||||
Route::get('/display/{uuid}/alias', function (Request $request, string $uuid) {
|
||||
$plugin = Plugin::where('uuid', $uuid)->firstOrFail();
|
||||
|
||||
// Check if alias is active
|
||||
if (! $plugin->alias) {
|
||||
return response()->json([
|
||||
'message' => 'Alias is not active for this plugin',
|
||||
], 403);
|
||||
}
|
||||
|
||||
// Get device model name from query parameter, default to 'og_png'
|
||||
$deviceModelName = $request->query('device-model', 'og_png');
|
||||
$deviceModel = DeviceModel::where('name', $deviceModelName)->first();
|
||||
|
||||
if (! $deviceModel) {
|
||||
return response()->json([
|
||||
'message' => "Device model '{$deviceModelName}' not found",
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Check if we can use cached image (only for og_png and if data is not stale)
|
||||
$useCache = $deviceModelName === 'og_png' && ! $plugin->isDataStale() && $plugin->current_image !== null;
|
||||
|
||||
if ($useCache) {
|
||||
// Return cached image
|
||||
$imageUuid = $plugin->current_image;
|
||||
$fileExtension = $deviceModel->mime_type === 'image/bmp' ? 'bmp' : 'png';
|
||||
$imagePath = 'images/generated/'.$imageUuid.'.'.$fileExtension;
|
||||
|
||||
// Check if image exists, otherwise fall back to generation
|
||||
if (Storage::disk('public')->exists($imagePath)) {
|
||||
return response()->file(Storage::disk('public')->path($imagePath), [
|
||||
'Content-Type' => $deviceModel->mime_type,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate new image
|
||||
try {
|
||||
// Update data if needed
|
||||
if ($plugin->isDataStale()) {
|
||||
$plugin->updateDataPayload();
|
||||
$plugin->refresh();
|
||||
}
|
||||
|
||||
// Load device model with palette relationship
|
||||
$deviceModel->load('palette');
|
||||
|
||||
// Create a virtual device for rendering (Plugin::render needs a Device object)
|
||||
$virtualDevice = new Device();
|
||||
$virtualDevice->setRelation('deviceModel', $deviceModel);
|
||||
$virtualDevice->setRelation('user', $plugin->user);
|
||||
$virtualDevice->setRelation('palette', $deviceModel->palette);
|
||||
|
||||
// Render the plugin markup
|
||||
$markup = $plugin->render(device: $virtualDevice);
|
||||
|
||||
// Generate image using the new method that doesn't require a device
|
||||
$imageUuid = ImageGenerationService::generateImageFromModel(
|
||||
markup: $markup,
|
||||
deviceModel: $deviceModel,
|
||||
user: $plugin->user,
|
||||
palette: $deviceModel->palette
|
||||
);
|
||||
|
||||
// Update plugin cache if using og_png
|
||||
if ($deviceModelName === 'og_png') {
|
||||
$plugin->update(['current_image' => $imageUuid]);
|
||||
}
|
||||
|
||||
// Return the generated image
|
||||
$fileExtension = $deviceModel->mime_type === 'image/bmp' ? 'bmp' : 'png';
|
||||
$imagePath = Storage::disk('public')->path('images/generated/'.$imageUuid.'.'.$fileExtension);
|
||||
|
||||
return response()->file($imagePath, [
|
||||
'Content-Type' => $deviceModel->mime_type,
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
Log::error("Failed to generate alias image for plugin {$plugin->id} ({$plugin->name}): ".$e->getMessage());
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Failed to generate image',
|
||||
'error' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
})->name('api.display.alias');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue