julienmonnerie/kirby/config/fields/mixins/upload.php

98 lines
2.1 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
use Kirby\Cms\Api;
use Kirby\Cms\File;
use Kirby\Exception\Exception;
2025-04-21 18:57:21 +02:00
use Kirby\Exception\InvalidArgumentException;
2022-06-17 17:51:59 +02:00
return [
2022-08-31 15:02:43 +02:00
'props' => [
/**
* Sets the upload options for linked files (since 3.2.0)
*/
'uploads' => function ($uploads = []) {
if ($uploads === false) {
return false;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if (is_string($uploads) === true) {
$uploads = ['template' => $uploads];
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if (is_array($uploads) === false) {
$uploads = [];
}
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
$uploads['accept'] = '*';
if ($preview = $this->image) {
$uploads['preview'] = $preview;
}
if ($template = $uploads['template'] ?? null) {
// get parent object for upload target
$parent = $this->uploadParent($uploads['parent'] ?? null);
if ($parent === null) {
2025-07-11 14:41:34 +02:00
throw new InvalidArgumentException(
message: '"' . $uploads['parent'] . '" could not be resolved as a valid parent for the upload'
);
2025-04-21 18:57:21 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
$file = new File([
'filename' => 'tmp',
2025-04-21 18:57:21 +02:00
'parent' => $parent,
2022-08-31 15:02:43 +02:00
'template' => $template
]);
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
$uploads['accept'] = $file->blueprint()->acceptAttribute();
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $uploads;
},
],
'methods' => [
'upload' => function (Api $api, $params, Closure $map) {
if ($params === false) {
2025-07-11 14:41:34 +02:00
throw new Exception(
message: 'Uploads are disabled for this field'
);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2025-04-21 18:57:21 +02:00
$parent = $this->uploadParent($params['parent'] ?? null);
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $api->upload(function ($source, $filename) use ($parent, $params, $map) {
2023-04-14 16:34:06 +02:00
$props = [
2022-08-31 15:02:43 +02:00
'source' => $source,
'template' => $params['template'] ?? null,
'filename' => $filename,
2023-04-14 16:34:06 +02:00
];
// move the source file from the temp dir
$file = $parent->createFile($props, true);
2022-06-17 17:51:59 +02:00
2022-12-19 14:56:05 +01:00
if ($file instanceof File === false) {
2025-07-11 14:41:34 +02:00
throw new Exception(
message: 'The file could not be uploaded'
);
2022-08-31 15:02:43 +02:00
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $map($file, $parent);
});
2025-04-21 18:57:21 +02:00
},
'uploadParent' => function (string|null $parentQuery = null) {
$parent = $this->model();
if ($parentQuery) {
$parent = $parent->query($parentQuery);
}
if ($parent instanceof File) {
$parent = $parent->parent();
}
return $parent;
2022-08-31 15:02:43 +02:00
}
]
2022-06-17 17:51:59 +02:00
];