Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions formwork/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ fields:
files:
allowedExtensions: []
metadataExtension: .meta.yaml
paths:
site: '${%ROOT_PATH%}/site/files'
uploads:
path: '${%ROOT_PATH%}/site/files'
baseDestinations:
Expand Down
4 changes: 4 additions & 0 deletions formwork/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
'path' => '/site/templates/assets/{file:all}/',
'action' => 'Formwork\Controllers\AssetsController@template',
],
'files' => [
'path' => '/files/{name}/',
'action' => 'Formwork\Controllers\FilesController@file',
],
'tag.pagination' => [
'path' => '/{page:all}/tag/{tagName:slug}/page/{paginationPage:number}/',
'action' => 'Formwork\Controllers\PageController@load',
Expand Down
34 changes: 34 additions & 0 deletions formwork/src/Cms/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Formwork\Cms;

use Formwork\Config\Config;
use Formwork\Files\FileCollection;
use Formwork\Files\FileFactory;
use Formwork\Languages\Languages;
use Formwork\Metadata\MetadataCollection;
use Formwork\Model\Model;
Expand All @@ -20,6 +22,7 @@
use Formwork\Users\Users;
use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
use Formwork\Utils\Str;
use Stringable;

class Site extends Model implements Stringable
Expand Down Expand Up @@ -104,6 +107,11 @@ class Site extends Model implements Stringable
*/
protected array $routeAliases;

/**
* Site files
*/
protected FileCollection $files;

/**
* @param array<string, mixed> $data
*/
Expand Down Expand Up @@ -468,6 +476,32 @@ public function load(): void
$this->loadRouteAliases();
}

/**
* Get site files
*/
public function files(): FileCollection
{
if (isset($this->files)) {
return $this->files;
}

$files = [];

$path = $this->config->get('system.files.paths.site');

foreach (FileSystem::listFiles($path) as $file) {
$extension = '.' . FileSystem::extension($file);
if (Str::endsWith($file, $this->config->get('system.files.metadataExtension'))) {
continue;
}
if (in_array($extension, $this->config->get('system.files.allowedExtensions'), true)) {
$files[] = $this->app->getService(FileFactory::class)->make(FileSystem::joinPaths($path, $file));
}
}

return $this->files = new FileCollection($files);
}

/**
* @param array<string, mixed> $metadata
*/
Expand Down
25 changes: 25 additions & 0 deletions formwork/src/Controllers/FilesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Formwork\Controllers;

use Formwork\Http\FileResponse;
use Formwork\Http\Response;
use Formwork\Router\RouteParams;
use Formwork\Utils\FileSystem;

final class FilesController extends AbstractController
{
/**
* FilesController@file action
*/
public function file(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.files.paths.site'), $routeParams->get('name'));

if (FileSystem::isFile($path, assertExists: false)) {
return new FileResponse($path);
}

return $this->forward(PageController::class, 'error');
}
}
10 changes: 5 additions & 5 deletions formwork/src/Fields/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function setValues(mixed $data, mixed $default = null): static
*/
public function setValuesFromRequest(Request $request, mixed $default = null): static
{
return $this->setValues([
...$request->query()->toArray(),
...$request->input()->toArray(),
...$request->files()->toArray(),
], $default);
return $this->setValues(array_merge_recursive(
$request->query()->toArray(),
$request->input()->toArray(),
$request->files()->toArray()
), $default);
}
}
6 changes: 6 additions & 0 deletions formwork/src/Files/FileUriGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function generate(File $file): string
{
$path = $file->path();

if (Str::startsWith($path, FileSystem::normalizePath($this->config->get('system.files.paths.site')))) {
$name = basename($path);
$uriPath = $this->router->generate('files', compact('name'));
return $this->site->uri($uriPath, includeLanguage: false);
}

if (Str::startsWith($path, FileSystem::normalizePath($this->config->get('system.images.processPath')))) {
$id = basename(dirname($path));
$name = basename($path);
Expand Down
12 changes: 1 addition & 11 deletions formwork/src/Http/FilesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ public function isEmpty(): bool
*/
public function getAll(): array
{
$result = [];
foreach ($this->data as $value) {
if (is_array($value)) {
foreach ($value as $v) {
$result[] = $v;
}
} else {
$result[] = $value;
}
}
return $result;
return Arr::flatten($this->data);
}
}
16 changes: 11 additions & 5 deletions formwork/src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,17 +520,23 @@ protected function prepareFiles(array $files): FilesData
foreach ($files as $fieldName => $data) {
if (is_array($data['name'])) {
foreach (array_keys($data['name']) as $i) {
/**
* @var array<string, list<UploadedFile>> $result
*/
$result[$fieldName][] = new UploadedFile($fieldName, [
$props = [
'name' => $data['name'][$i],
'full_path' => $data['full_path'][$i] ?? '',
'type' => $data['type'][$i] ?? '',
'tmp_name' => $data['tmp_name'][$i] ?? '',
'error' => $data['error'][$i] ?? '',
'size' => $data['size'][$i] ?? '',
]);
];

if (is_array($data['name'][$i])) {
$result[$fieldName] = $this->prepareFiles([$i => $props])->toArray();
} else {
/**
* @var array<string, list<UploadedFile>> $result
*/
$result[$fieldName][] = new UploadedFile($fieldName, $props);
}
}
} else {
$result[$fieldName] = new UploadedFile($fieldName, $data);
Expand Down
6 changes: 6 additions & 0 deletions formwork/src/Panel/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ protected function defaults(): array
'permissions' => 'pages',
'badge' => $this->site->descendants()->count(),
],
'files' => [
'label' => $this->translate('panel.files.files'),
'uri' => '/files/',
'permissions' => 'files',
'badge' => null,
],
'statistics' => [
'label' => $this->translate('panel.statistics.statistics'),
'uri' => '/statistics/',
Expand Down
Loading