Skip to content

Commit

Permalink
Merge pull request #1834 from bolt/feature/upload-location
Browse files Browse the repository at this point in the history
Use configured `upload_location` for images and files in ContentTypes
  • Loading branch information
I-Valchev authored Sep 10, 2020
2 parents b5201aa + 6a07824 commit 414064c
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 40 deletions.
2 changes: 2 additions & 0 deletions config/bolt/config.yaml
Original file line number Diff line number Diff line change
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
13 changes: 9 additions & 4 deletions src/Configuration/Parser/ContentTypesParser.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 %}

0 comments on commit 414064c

Please sign in to comment.