98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Integrations\Media;
|
|
|
|
use LIT\MCP\Abilities\Ability;
|
|
use LIT\MCP\Limits;
|
|
use WP_Error;
|
|
|
|
/**
|
|
* Uploads a Base64-encoded file into the media library (spec §6.3), up to the configured
|
|
* upload limit (at most Limits::INLINE_MAX_MB, the request carries the whole file).
|
|
* Checks and cleanup: MediaImporter; permission and result: MediaUploads.
|
|
*/
|
|
final class MediaUploadAbility implements Ability
|
|
{
|
|
public function __construct(
|
|
private MediaImporter $importer,
|
|
private MediaUploads $uploads,
|
|
private Limits $limits
|
|
) {
|
|
}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'lit/media-upload';
|
|
}
|
|
|
|
public function label(): string
|
|
{
|
|
return 'Medium hochladen';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return sprintf(
|
|
'Lädt eine Datei (Bild, PDF, MP4, MP3) als Base64 in die Mediathek, höchstens %d MB; optional mit '
|
|
. 'Titel, Alt-Text und Zuordnung zu einem Eintrag (post_id). Nicht idempotent: jeder Aufruf legt '
|
|
. 'ein neues Medium an. Größere Dateien per URL mit lit/media-upload-from-url importieren.',
|
|
$this->limits->inlineMegabytes()
|
|
);
|
|
}
|
|
|
|
public function inputSchema(): array
|
|
{
|
|
return [
|
|
'type' => 'object',
|
|
'properties' => array_merge(
|
|
[
|
|
'filename' => ['type' => 'string', 'minLength' => 1, 'description' => 'Dateiname mit Endung.'],
|
|
'data' => [
|
|
'type' => 'string',
|
|
'minLength' => 1,
|
|
'description' => 'Dateiinhalt als Base64 (ohne data:-Präfix).',
|
|
],
|
|
],
|
|
MediaUploads::metaProperties()
|
|
),
|
|
'required' => ['filename', 'data'],
|
|
'additionalProperties' => false,
|
|
];
|
|
}
|
|
|
|
public function outputSchema(): ?array
|
|
{
|
|
return MediaUploads::outputSchema();
|
|
}
|
|
|
|
public function annotations(): array
|
|
{
|
|
return ['readonly' => false, 'destructive' => false, 'idempotent' => false];
|
|
}
|
|
|
|
public function isDirect(): bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function permission(array $input): bool|WP_Error
|
|
{
|
|
return $this->uploads->permission($input);
|
|
}
|
|
|
|
public function execute(array $input): mixed
|
|
{
|
|
return $this->uploads->run(
|
|
$this->name(),
|
|
fn (): int|WP_Error => $this->importer->fromBase64(
|
|
(string) $input['data'],
|
|
(string) $input['filename'],
|
|
MediaUploads::meta($input)
|
|
),
|
|
'Datei "%s" hochgeladen (ID %d)'
|
|
);
|
|
}
|
|
}
|