media-library search, MIME-type and virtual-folder filters, persisted

folder and alt-text editing, and safe defaults for legacy uploads
This commit is contained in:
2026-11-30 16:51:14 +01:00
parent 1ec2d9c8e1
commit e1767dcd2c
7 changed files with 654 additions and 9 deletions
@@ -40,6 +40,46 @@ class UploadRepository extends BaseRepository
'original_file' => $data['original_file'],
'mime_type' => $data['mime_type'],
'server_file' => $data['server_file'],
'alt_text' => '',
'folder' => '',
]);
}
/**
* Update supported media-library metadata for the upload with the given public ID.
*/
public function updateMetadata(string $publicId, array $metadata): bool
{
$fieldLimits = [
'alt_text' => 1000,
'folder' => 255,
];
if ($publicId === '' || strlen($publicId) > 50 || empty($metadata)) {
return false;
}
$validatedMetadata = [];
foreach ($metadata as $field => $value) {
if (! isset($fieldLimits[$field]) || ! is_string($value) || ! $this->isValidMetadataValue($value, $fieldLimits[$field])) {
return false;
}
$validatedMetadata[$field] = $field === 'folder' ? trim($value) : $value;
}
$uploads = $this->findWhere('public_id', $publicId);
if (empty($uploads)) {
return false;
}
return $this->update($uploads[0], $validatedMetadata);
}
private function isValidMetadataValue(string $value, int $maxLength): bool
{
if (strlen($value) > $maxLength * 4 || ! preg_match('//u', $value)) {
return false;
}
return preg_match_all('/./us', $value) <= $maxLength;
}
}