2022-06-17 17:51:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// routing pattern to match all models with files
|
|
|
|
$pattern = '(account|pages/[^/]+|site|users/[^/]+)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Files Routes
|
|
|
|
*/
|
|
|
|
return [
|
|
|
|
|
2022-08-31 15:02:43 +02:00
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)/sections/(:any)',
|
|
|
|
'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
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)/fields/(:any)/(:all?)',
|
|
|
|
'method' => 'ALL',
|
|
|
|
'action' => function (string $parent, string $filename, string $fieldName, string $path = null) {
|
|
|
|
if ($file = $this->file($parent, $filename)) {
|
|
|
|
return $this->fieldApi($file, $fieldName, $path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files',
|
|
|
|
'method' => 'GET',
|
|
|
|
'action' => function (string $path) {
|
|
|
|
return $this->parent($path)->files()->sorted();
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files',
|
|
|
|
'method' => 'POST',
|
|
|
|
'action' => function (string $path) {
|
|
|
|
// move_uploaded_file() not working with unit test
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
return $this->upload(function ($source, $filename) use ($path) {
|
2023-04-14 16:34:06 +02:00
|
|
|
$props = [
|
2022-08-31 15:02:43 +02:00
|
|
|
'content' => [
|
|
|
|
'sort' => $this->requestBody('sort')
|
|
|
|
],
|
|
|
|
'source' => $source,
|
|
|
|
'template' => $this->requestBody('template'),
|
|
|
|
'filename' => $filename
|
2023-04-14 16:34:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// move the source file from the temp dir
|
|
|
|
return $this->parent($path)->createFile($props, true);
|
2022-08-31 15:02:43 +02:00
|
|
|
});
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/search',
|
|
|
|
'method' => 'GET|POST',
|
|
|
|
'action' => function (string $path) {
|
|
|
|
$files = $this->parent($path)->files();
|
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
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/sort',
|
|
|
|
'method' => 'PATCH',
|
|
|
|
'action' => function (string $path) {
|
|
|
|
return $this->parent($path)->files()->changeSort(
|
|
|
|
$this->requestBody('files'),
|
|
|
|
$this->requestBody('index')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)',
|
|
|
|
'method' => 'GET',
|
|
|
|
'action' => function (string $path, string $filename) {
|
|
|
|
return $this->file($path, $filename);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)',
|
|
|
|
'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
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)',
|
|
|
|
'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
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)',
|
|
|
|
'method' => 'DELETE',
|
|
|
|
'action' => function (string $path, string $filename) {
|
|
|
|
return $this->file($path, $filename)->delete();
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => $pattern . '/files/(:any)/name',
|
|
|
|
'method' => 'PATCH',
|
|
|
|
'action' => function (string $path, string $filename) {
|
|
|
|
return $this->file($path, $filename)->changeName($this->requestBody('name'));
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pattern' => 'files/search',
|
|
|
|
'method' => 'GET|POST',
|
|
|
|
'action' => function () {
|
|
|
|
$files = $this
|
|
|
|
->site()
|
|
|
|
->index(true)
|
|
|
|
->filter('isReadable', true)
|
|
|
|
->files();
|
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
|
|
|
];
|