Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use configured upload_location for images and files in ContentTypes #1834

Merged
merged 7 commits into from Sep 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -72,7 +72,7 @@ script:
# run API tests
- make behat-api-quiet
# run JS e2e tests
- make behat-js-quiet
- travis_retry make behat-js-quiet
# Upload Behat logs
- ./vendor/bin/upload-textfiles "var/log/behat-reports/*.log"

2 changes: 2 additions & 0 deletions config/bolt/config.yaml
Expand Up @@ -180,6 +180,8 @@ accept_media_types: [ gif, jpg, jpeg, png, svg, pdf, mp3, tiff ]
#`post_max_size` and `upload_max_filesize` in `php.ini`.
accept_upload_size: 8M

upload_location: "{year}/{month}/{contenttype}/"

# Options to use with curl requests.
# For all options, check the official curl documentation here https://curl.haxx.se/libcurl/c/curl_easy_setopt.html
curl_options:
Expand Down
2 changes: 1 addition & 1 deletion ecs.php
Expand Up @@ -46,7 +46,7 @@
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set('sets', ['clean-code', 'common', 'php-70', 'php-71', 'psr-12', 'symfony', 'symfony-risky']);
$parameters->set('sets', ['clean-code', 'common', 'php70', 'php71', 'psr12', 'symfony', 'symfony-risky']);

$parameters->set('paths', [
__DIR__ . '/src',
Expand Down
13 changes: 9 additions & 4 deletions src/Configuration/Parser/ContentTypesParser.php
Expand Up @@ -274,23 +274,28 @@ private function parseField($key, &$field, $acceptFileTypes, &$currentGroup): vo
throw new ConfigurationException($error);
}

// If field is a "file" type, make sure the 'extensions' are set, and it's an array.
// If field is a "file" type, make sure the 'extensions' are set.
if ($field['type'] === 'file' || $field['type'] === 'filelist') {
if (empty($field['extensions'])) {
$field['extensions'] = $acceptFileTypes;
}

$field['extensions'] = (array) $field['extensions'];
}

// If field is an "image" type, make sure the 'extensions' are set, and it's an array.
// If field is an "image" type, make sure the 'extensions' are set.
if ($field['type'] === 'image' || $field['type'] === 'imagelist') {
if (empty($field['extensions'])) {
$extensions = new Collection(['gif', 'jpg', 'jpeg', 'png', 'svg']);
$field['extensions'] = $extensions->intersect($acceptFileTypes)->toArray();
}
}

// Image and File fields should have 'extensions' as an array, and a defined upload location
if (in_array($field['type'], ['file', 'filelist', 'image', 'imagelist'], true)) {
$field['extensions'] = (array) $field['extensions'];

if (empty($field['upload'])) {
$field['upload'] = $this->generalConfig->get('upload_location');
}
}

// Make indexed arrays into associative for select fields
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/Parser/GeneralParser.php
Expand Up @@ -132,6 +132,7 @@ protected function getDefaultConfig(): array
'accept_file_types' => explode(',', 'twig,html,js,css,scss,gif,jpg,jpeg,png,ico,zip,tgz,txt,md,doc,docx,pdf,epub,xls,xlsx,csv,ppt,pptx,mp3,ogg,wav,m4a,mp4,m4v,ogv,wmv,avi,webm,svg'),
'accept_media_types' => explode(',', 'gif,jpg,jpeg,png,svg,pdf,mp3,tiff'),
'accept_upload_size' => '8M',
'upload_location' => '{year}/{month}/{contenttype}/',
'branding' => [
'name' => 'Bolt',
'path' => '/bolt',
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Backend/Async/UploadController.php
Expand Up @@ -76,7 +76,7 @@ public function handleUpload(Request $request): JsonResponse

$uploadHandler = new Handler($target, [
Handler::OPTION_AUTOCONFIRM => true,
Handler::OPTION_OVERWRITE => true,
Handler::OPTION_OVERWRITE => false,
]);

$acceptedFileTypes = array_merge($this->config->getMediaTypes()->toArray(), $this->config->getFileTypes()->toArray());
Expand Down
16 changes: 16 additions & 0 deletions src/Twig/HtmlExtension.php
Expand Up @@ -58,6 +58,7 @@ public function getFilters(): array
return [
new TwigFilter('markdown', [$this, 'markdown'], $safe),
new TwigFilter('shy', [$this, 'shy'], $safe),
new TwigFilter('placeholders', [$this, 'placeholders'], $safe),
];
}

Expand Down Expand Up @@ -101,4 +102,19 @@ public function absoluteLink(string $link): string
{
return Html::makeAbsoluteLink($link);
}

public function placeholders(string $string, array $replacements = []): string
{
$baseReplacements = [
'year' => date('Y'),
'month' => date('m'),
'day' => date('D'),
'date' => date('Y-m-D'),
'random' => bin2hex(random_bytes(4)),
];

$replacements = array_merge($baseReplacements, $replacements);

return Str::placeholders($string, $replacements, true);
}
}
3 changes: 2 additions & 1 deletion templates/_macro/_macro.html.twig
Expand Up @@ -39,7 +39,7 @@

{% endapply %}{% endmacro %}

{% macro generate_collection_fields(collectionField, fields, compileTemplates) %}{% apply spaceless %}
{% macro generate_collection_fields(collectionField, fields, record, compileTemplates) %}{% apply spaceless %}
{% set fieldsHtml = [] %}
{% for item_field in fields %}
{% set collectionItemName = 'collections[' ~ collectionField.name ~ '][' ~ item_field.definition.name ~ ']' %}
Expand All @@ -60,6 +60,7 @@
'collection_name': collectionField.name,
'collection_label': collectionField.definition.label,
'hash': hash,
'record': record
} %}

{% if item_field.type != 'set' %}
Expand Down
4 changes: 2 additions & 2 deletions templates/_partials/fields/collection.html.twig
Expand Up @@ -8,12 +8,12 @@
{% set limit = field.definition.get('limit')|default(200) %}

{# get the html for all collection field already in the database #}
{% set existing_fields %}{{ macro.generate_collection_fields(field, field.value, false) }}{% endset %}
{% set existing_fields %}{{ macro.generate_collection_fields(field, field.value, record, false) }}{% endset %}

{# get the html template for the collection fields defined in the field definition #}
{% set templated_fields = "" %}
{% if field.templates is defined %}
{% set templated_fields %}{{ macro.generate_collection_fields(field, field.templates, true) }}{% endset %}
{% set templated_fields %}{{ macro.generate_collection_fields(field, field.templates, record, true) }}{% endset %}
{% endif %}

<editor-collection
Expand Down
2 changes: 1 addition & 1 deletion templates/_partials/fields/file.html.twig
Expand Up @@ -7,7 +7,7 @@
{% endset %}

{% block field %}
{% set setPath = field.definition.get('upload')|default('') %}
{% set setPath = field.definition.get('upload')|placeholders({'random': 4, 'contenttype': record.contenttype}) %}
{% set directory = path('bolt_async_upload', {'location': location|default('files'), 'path': setPath}) %}
{% set filelist = path('bolt_async_filelisting', {'location': location|default('files') }) %}
{% set labels = {
Expand Down
2 changes: 1 addition & 1 deletion templates/_partials/fields/filelist.html.twig
Expand Up @@ -7,7 +7,7 @@
{% endset %}

{% block field %}
{% set setPath = field.definition.get('upload')|default('') %}
{% set setPath = field.definition.get('upload')|placeholders({'random': 4, 'contenttype': record.contenttype}) %}
{% set directory = path('bolt_async_upload', {'location': location|default('files'), 'path': setPath}) %}
{% set filelist = path('bolt_async_filelisting', {'location': location|default('files') }) %}
{% set labels = {
Expand Down
2 changes: 1 addition & 1 deletion templates/_partials/fields/image.html.twig
Expand Up @@ -8,7 +8,7 @@

{% block field %}

{% set setPath = field.definition.get('upload')|default('') %}
{% set setPath = field.definition.get('upload')|placeholders({'random': 4, 'contenttype': record.contenttype}) %}
{% set directory = path('bolt_async_upload', {'location': location|default('files'), 'path': setPath}) %}
{% set filelist = path('bolt_async_filelisting', {'location': location|default('files'), 'type': 'images' }) %}
{% set labels = {
Expand Down
57 changes: 28 additions & 29 deletions templates/_partials/fields/imagelist.html.twig
Expand Up @@ -7,35 +7,34 @@
{% endset %}

{% block field %}
{% set setPath = field.definition.get('upload')|placeholders({'random': 4, 'contenttype': record.contenttype}) %}
{% set directory = path('bolt_async_upload', {'location': location|default('files'), 'path': setPath}) %}
{% set filelist = path('bolt_async_filelisting', {'location': location|default('files'), 'type': 'images' }) %}
{% set labels = {
'button_upload': 'image.button_upload'|trans,
'button_from_library': 'image.button_from_library'|trans,
'placeholder_filename': 'image.placeholder_filename'|trans,
'placeholder_alt_text': 'image.placeholder_alt_text'|trans,
'placeholder_title': 'image.placeholder_title'|trans,
'button_remove': 'image.button_remove'|trans,
'button_edit_attributes': 'image.button_edit_attributes'|trans,
'button_move_up': 'image.button_up'|trans,
'button_move_down': 'image.button_down'|trans,
'add_new_image': 'image.add_new_image'|trans,
}|json_encode %}
{% set limit = field.definition.get('limit')|default(200) %}

{% set setPath = field.definition.get('upload')|default('') %}
{% set directory = path('bolt_async_upload', {'location': location|default('files'), 'path': setPath}) %}
{% set filelist = path('bolt_async_filelisting', {'location': location|default('files'), 'type': 'images' }) %}
{% set labels = {
'button_upload': 'image.button_upload'|trans,
'button_from_library': 'image.button_from_library'|trans,
'placeholder_filename': 'image.placeholder_filename'|trans,
'placeholder_alt_text': 'image.placeholder_alt_text'|trans,
'placeholder_title': 'image.placeholder_title'|trans,
'button_remove': 'image.button_remove'|trans,
'button_edit_attributes': 'image.button_edit_attributes'|trans,
'button_move_up': 'image.button_up'|trans,
'button_move_down': 'image.button_down'|trans,
'add_new_image': 'image.add_new_image'|trans,
}|json_encode %}
{% set limit = field.definition.get('limit')|default(200) %}

<editor-imagelist
:images='{{ field.jsonvalue }}'
:name='{{ name|json_encode }}'
:directory='{{ directory|json_encode }}'
:filelist='{{ filelist|json_encode }}'
:csrf-token='{{ csrf_token('upload')|json_encode }}'
:labels='{{ labels }}'
:extensions='{{ extensions|json_encode }}'
:attributes-link='{{ path('bolt_media_new')|json_encode }}'
:limit='{{ limit|json_encode }}'
:readonly='{{ readonly|json_encode }}'
></editor-imagelist>
<editor-imagelist
:images='{{ field.jsonvalue }}'
:name='{{ name|json_encode }}'
:directory='{{ directory|json_encode }}'
:filelist='{{ filelist|json_encode }}'
:csrf-token='{{ csrf_token('upload')|json_encode }}'
:labels='{{ labels }}'
:extensions='{{ extensions|json_encode }}'
:attributes-link='{{ path('bolt_media_new')|json_encode }}'
:limit='{{ limit|json_encode }}'
:readonly='{{ readonly|json_encode }}'
></editor-imagelist>

{% endblock %}
2 changes: 1 addition & 1 deletion templates/helpers/_taxonomylinks.html.twig
Expand Up @@ -9,7 +9,7 @@
{% endif %}
</em>
{% for taxonomy in taxonomies %}
<a href="{{ taxonomy.link }}" class="taxonomy-{{ type }}">{{ taxonomy.name }}</a>{% if not loop.last %}, {% endif %}
<a href="{{ taxonomy|link }}" class="taxonomy-{{ type }}">{{ taxonomy.name }}</a>{% if not loop.last %}, {% endif %}
{% else %}
{{ __('general.phrase.none') }}
{% endfor %}
Expand Down