mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-27 04:08:57 +02:00
Use new settings systems for attachments settings
This commit is contained in:
parent
4876068cce
commit
26d83af298
12 changed files with 130 additions and 50 deletions
|
@ -28,6 +28,7 @@ use App\Services\Attachments\BuiltinAttachmentsFinder;
|
|||
use App\Services\Misc\GitVersionInfo;
|
||||
use App\Services\Misc\DBInfoHelper;
|
||||
use App\Services\System\UpdateAvailableManager;
|
||||
use App\Settings\AppSettings;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
@ -45,7 +46,8 @@ class ToolsController extends AbstractController
|
|||
|
||||
#[Route(path: '/server_infos', name: 'tools_server_infos')]
|
||||
public function systemInfos(GitVersionInfo $versionInfo, DBInfoHelper $DBInfoHelper,
|
||||
AttachmentSubmitHandler $attachmentSubmitHandler, UpdateAvailableManager $updateAvailableManager): Response
|
||||
AttachmentSubmitHandler $attachmentSubmitHandler, UpdateAvailableManager $updateAvailableManager,
|
||||
AppSettings $settings): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('@system.server_infos');
|
||||
|
||||
|
@ -66,10 +68,10 @@ class ToolsController extends AbstractController
|
|||
'is_debug' => $this->getParameter('kernel.debug'),
|
||||
'email_sender' => $this->getParameter('partdb.mail.sender_email'),
|
||||
'email_sender_name' => $this->getParameter('partdb.mail.sender_name'),
|
||||
'allow_attachments_downloads' => $this->getParameter('partdb.attachments.allow_downloads'),
|
||||
'allow_attachments_downloads' => $settings->system->attachments->allowDownloads,
|
||||
'detailed_error_pages' => $this->getParameter('partdb.error_pages.show_help'),
|
||||
'error_page_admin_email' => $this->getParameter('partdb.error_pages.admin_email'),
|
||||
'configured_max_file_size' => $this->getParameter('partdb.attachments.max_file_size'),
|
||||
'configured_max_file_size' => $settings->system->attachments->maxFileSize,
|
||||
'effective_max_file_size' => $attachmentSubmitHandler->getMaximumAllowedUploadSize(),
|
||||
'saml_enabled' => $this->getParameter('partdb.saml.enabled'),
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Settings\SystemSettings\AttachmentsSettings;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
|
@ -54,9 +55,7 @@ class AttachmentFormType extends AbstractType
|
|||
protected Security $security,
|
||||
protected AttachmentSubmitHandler $submitHandler,
|
||||
protected TranslatorInterface $translator,
|
||||
protected bool $allow_attachments_download,
|
||||
protected bool $download_by_default,
|
||||
protected string $max_file_size
|
||||
protected AttachmentsSettings $settings,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -108,7 +107,7 @@ class AttachmentFormType extends AbstractType
|
|||
'required' => false,
|
||||
'label' => 'attachment.edit.download_url',
|
||||
'mapped' => false,
|
||||
'disabled' => !$this->allow_attachments_download,
|
||||
'disabled' => !$this->settings->allowDownloads,
|
||||
]);
|
||||
|
||||
$builder->add('file', FileType::class, [
|
||||
|
@ -177,7 +176,7 @@ class AttachmentFormType extends AbstractType
|
|||
|
||||
//If the attachment should be downloaded by default (and is download allowed at all), register a listener,
|
||||
// which sets the downloadURL checkbox to true for new attachments
|
||||
if ($this->download_by_default && $this->allow_attachments_download) {
|
||||
if ($this->settings->downloadByDefault && $this->settings->allowDownloads) {
|
||||
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event): void {
|
||||
$form = $event->getForm();
|
||||
$attachment = $form->getData();
|
||||
|
@ -204,7 +203,7 @@ class AttachmentFormType extends AbstractType
|
|||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Attachment::class,
|
||||
'max_file_size' => $this->max_file_size,
|
||||
'max_file_size' => $this->settings->maxFileSize,
|
||||
'allow_builtins' => true,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ use App\Entity\Attachments\StorageLocationAttachment;
|
|||
use App\Entity\Attachments\SupplierAttachment;
|
||||
use App\Entity\Attachments\UserAttachment;
|
||||
use App\Exceptions\AttachmentDownloadException;
|
||||
use App\Settings\SystemSettings\AttachmentsSettings;
|
||||
use Hshn\Base64EncodedFile\HttpFoundation\File\Base64EncodedFile;
|
||||
use Hshn\Base64EncodedFile\HttpFoundation\File\UploadedBase64EncodedFile;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
|
@ -64,12 +65,13 @@ class AttachmentSubmitHandler
|
|||
'asp', 'cgi', 'py', 'pl', 'exe', 'aspx', 'js', 'mjs', 'jsp', 'css', 'jar', 'html', 'htm', 'shtm', 'shtml', 'htaccess',
|
||||
'htpasswd', ''];
|
||||
|
||||
public function __construct(protected AttachmentPathResolver $pathResolver, protected bool $allow_attachments_downloads,
|
||||
protected HttpClientInterface $httpClient, protected MimeTypesInterface $mimeTypes,
|
||||
protected FileTypeFilterTools $filterTools, /**
|
||||
* @var string The user configured maximum upload size. This is a string like "10M" or "1G" and will be converted to
|
||||
*/
|
||||
protected string $max_upload_size)
|
||||
public function __construct(
|
||||
protected AttachmentPathResolver $pathResolver,
|
||||
protected HttpClientInterface $httpClient,
|
||||
protected MimeTypesInterface $mimeTypes,
|
||||
protected FileTypeFilterTools $filterTools,
|
||||
protected AttachmentsSettings $settings,
|
||||
)
|
||||
{
|
||||
//The mapping used to determine which folder will be used for an attachment type
|
||||
$this->folder_mapping = [
|
||||
|
@ -334,7 +336,7 @@ class AttachmentSubmitHandler
|
|||
protected function downloadURL(Attachment $attachment, bool $secureAttachment): Attachment
|
||||
{
|
||||
//Check if we are allowed to download files
|
||||
if (!$this->allow_attachments_downloads) {
|
||||
if (!$this->settings->allowDownloads) {
|
||||
throw new RuntimeException('Download of attachments is not allowed!');
|
||||
}
|
||||
|
||||
|
@ -472,7 +474,7 @@ class AttachmentSubmitHandler
|
|||
$this->max_upload_size_bytes = min(
|
||||
$this->parseFileSizeString(ini_get('post_max_size')),
|
||||
$this->parseFileSizeString(ini_get('upload_max_filesize')),
|
||||
$this->parseFileSizeString($this->max_upload_size),
|
||||
$this->parseFileSizeString($this->settings->maxFileSize)
|
||||
);
|
||||
|
||||
return $this->max_upload_size_bytes;
|
||||
|
|
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||
namespace App\Settings;
|
||||
|
||||
use App\Settings\InfoProviderSystem\InfoProviderSettings;
|
||||
use App\Settings\SystemSettings\AttachmentsSettings;
|
||||
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
|
||||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsTrait;
|
||||
|
@ -33,9 +34,11 @@ class AppSettings
|
|||
{
|
||||
use SettingsTrait;
|
||||
|
||||
#[EmbeddedSettings()]
|
||||
public ?InfoProviderSettings $infoProviders = null;
|
||||
|
||||
#[EmbeddedSettings()]
|
||||
public ?TestSettings $test = null;
|
||||
public ?SystemSettings $system;
|
||||
|
||||
|
||||
#[EmbeddedSettings()]
|
||||
public ?InfoProviderSettings $infoProviders = null;
|
||||
}
|
|
@ -23,18 +23,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Settings;
|
||||
|
||||
use App\Settings\SystemSettings\AttachmentsSettings;
|
||||
use Jbtronics\SettingsBundle\Settings\EmbeddedSettings;
|
||||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
|
||||
|
||||
#[Settings]
|
||||
class TestSettings
|
||||
class SystemSettings
|
||||
{
|
||||
#[SettingsParameter()]
|
||||
public bool $bool = false;
|
||||
|
||||
#[SettingsParameter()]
|
||||
public int $int = 0;
|
||||
|
||||
#[SettingsParameter()]
|
||||
public float $float = 0.0;
|
||||
#[EmbeddedSettings()]
|
||||
public ?AttachmentsSettings $attachments = null;
|
||||
}
|
56
src/Settings/SystemSettings/AttachmentsSettings.php
Normal file
56
src/Settings/SystemSettings/AttachmentsSettings.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Settings\SystemSettings;
|
||||
|
||||
use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
|
||||
use Jbtronics\SettingsBundle\Settings\Settings;
|
||||
use Jbtronics\SettingsBundle\Settings\SettingsParameter;
|
||||
use Symfony\Component\Translation\TranslatableMessage as TM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[Settings(label: new TM("settings.system.attachments"))]
|
||||
class AttachmentsSettings
|
||||
{
|
||||
#[SettingsParameter(
|
||||
label: new TM("settings.system.attachments.maxFileSize"),
|
||||
description: new TM("settings.system.attachments.maxFileSize.help"),
|
||||
envVar: "MAX_ATTACHMENT_FILE_SIZE", envVarMode: EnvVarMode::OVERWRITE
|
||||
)]
|
||||
#[Assert\Regex("/^([1-9][0-9]*)([KMG])?$/", message: "validator.fileSize.invalidFormat")]
|
||||
public string $maxFileSize = '100M';
|
||||
|
||||
#[SettingsParameter(
|
||||
label: new TM("settings.system.attachments.allowDownloads"),
|
||||
description: new TM("settings.system.attachments.allowDownloads.help"),
|
||||
formOptions: ['help_html' => true],
|
||||
envVar: "bool:ALLOW_ATTACHMENT_DOWNLOADS", envVarMode: EnvVarMode::OVERWRITE
|
||||
)]
|
||||
public bool $allowDownloads = false;
|
||||
|
||||
#[SettingsParameter(
|
||||
label: new TM("settings.system.attachments.downloadByDefault"),
|
||||
envVar: "bool:ATTACHMENT_DOWNLOAD_BY_DEFAULT", envVarMode: EnvVarMode::OVERWRITE
|
||||
)]
|
||||
public bool $downloadByDefault = false;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue