julienmonnerie/kirby/config/api/routes/files.php

147 lines
3.7 KiB
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
// routing pattern to match all models with files
2025-04-21 18:57:21 +02:00
$filePattern = '(account/|pages/[^/]+/|site/|users/[^/]+/|)files/(:any)';
$parentPattern = '(account|pages/[^/]+|site|users/[^/]+)/files';
2022-06-17 17:51:59 +02:00
/**
* Files Routes
*/
return [
2022-08-31 15:02:43 +02:00
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern . '/fields/(:any)/(:all?)',
'method' => 'ALL',
'action' => function (string $parent, string $filename, string $fieldName, string|null $path = null) {
if ($file = $this->file($parent, $filename)) {
return $this->fieldApi($file, $fieldName, $path);
}
}
],
[
'pattern' => $filePattern . '/sections/(:any)',
2022-08-31 15:02:43 +02:00
'method' => 'GET',
'action' => function (string $path, string $filename, string $sectionName) {
2022-12-19 14:56:05 +01:00
return $this->file($path, $filename)->blueprint()->section($sectionName)?->toResponse();
2022-08-31 15:02:43 +02:00
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern . '/sections/(:any)/(:all?)',
2022-08-31 15:02:43 +02:00
'method' => 'ALL',
2025-04-21 18:57:21 +02:00
'action' => function (string $parent, string $filename, string $sectionName, string|null $path = null) {
2022-08-31 15:02:43 +02:00
if ($file = $this->file($parent, $filename)) {
2025-04-21 18:57:21 +02:00
return $this->sectionApi($file, $sectionName, $path);
2022-08-31 15:02:43 +02:00
}
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $parentPattern,
2022-08-31 15:02:43 +02:00
'method' => 'GET',
'action' => function (string $path) {
2025-04-21 18:57:21 +02:00
return $this->files($path)->sorted();
2022-08-31 15:02:43 +02:00
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $parentPattern,
2022-08-31 15:02:43 +02:00
'method' => 'POST',
'action' => function (string $path) {
// move_uploaded_file() not working with unit test
// @codeCoverageIgnoreStart
return $this->upload(function ($source, $filename) use ($path) {
2025-07-11 14:41:34 +02:00
// move the source file to the content folder
2025-04-21 18:57:21 +02:00
return $this->parent($path)->createFile([
2022-08-31 15:02:43 +02:00
'content' => [
'sort' => $this->requestBody('sort')
],
'source' => $source,
'template' => $this->requestBody('template'),
'filename' => $filename
2025-04-21 18:57:21 +02:00
], true);
2022-08-31 15:02:43 +02:00
});
// @codeCoverageIgnoreEnd
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $parentPattern . '/search',
2022-08-31 15:02:43 +02:00
'method' => 'GET|POST',
'action' => function (string $path) {
2025-04-21 18:57:21 +02:00
$files = $this->files($path);
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($this->requestMethod() === 'GET') {
return $files->search($this->requestQuery('q'));
}
2022-12-19 14:56:05 +01:00
return $files->query($this->requestBody());
2022-08-31 15:02:43 +02:00
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $parentPattern . '/sort',
2022-08-31 15:02:43 +02:00
'method' => 'PATCH',
'action' => function (string $path) {
2025-04-21 18:57:21 +02:00
return $this->files($path)->changeSort(
2022-08-31 15:02:43 +02:00
$this->requestBody('files'),
$this->requestBody('index')
);
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern,
2022-08-31 15:02:43 +02:00
'method' => 'GET',
'action' => function (string $path, string $filename) {
return $this->file($path, $filename);
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern,
2022-08-31 15:02:43 +02:00
'method' => 'PATCH',
'action' => function (string $path, string $filename) {
2022-12-19 14:56:05 +01:00
return $this->file($path, $filename)->update(
$this->requestBody(),
$this->language(),
true
);
2022-08-31 15:02:43 +02:00
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern,
2022-08-31 15:02:43 +02:00
'method' => 'POST',
'action' => function (string $path, string $filename) {
2023-04-14 16:34:06 +02:00
// move the source file from the temp dir
2022-12-19 14:56:05 +01:00
return $this->upload(
2023-04-14 16:34:06 +02:00
fn ($source) => $this->file($path, $filename)->replace($source, true)
2022-12-19 14:56:05 +01:00
);
2022-08-31 15:02:43 +02:00
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern,
2022-08-31 15:02:43 +02:00
'method' => 'DELETE',
'action' => function (string $path, string $filename) {
return $this->file($path, $filename)->delete();
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $filePattern . '/name',
2022-08-31 15:02:43 +02:00
'method' => 'PATCH',
'action' => function (string $path, string $filename) {
return $this->file($path, $filename)->changeName($this->requestBody('name'));
}
],
[
2025-04-21 18:57:21 +02:00
'pattern' => $parentPattern . '/search',
2022-08-31 15:02:43 +02:00
'method' => 'GET|POST',
'action' => function () {
$files = $this
->site()
->index(true)
2025-04-21 18:57:21 +02:00
->filter('isListable', true)
->files()
->filter('isListable', true);
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
if ($this->requestMethod() === 'GET') {
return $files->search($this->requestQuery('q'));
}
2022-12-19 14:56:05 +01:00
return $files->query($this->requestBody());
2022-08-31 15:02:43 +02:00
}
],
2022-06-17 17:51:59 +02:00
];