Allow to specifiy a path to a builtin ressource via the url field.

This commit is contained in:
Jan Böhmer 2019-09-25 16:03:22 +02:00
parent d99365f906
commit 2001680542
6 changed files with 192 additions and 10 deletions

View file

@ -59,7 +59,10 @@ abstract class Attachment extends NamedDBElement
/**
* When the path begins with one of this placeholders
*/
const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%', '%FOOTPRINTS%', '%FOOTPRINTS3D%'];
const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%'];
/** @var array Placeholders for attachments which using built in files. */
const BUILTIN_PLACEHOLDER = ['%FOOTPRINTS%', '%FOOTPRINTS3D%'];
/**
* @var bool
@ -128,7 +131,17 @@ abstract class Attachment extends NamedDBElement
return true;
}
return !in_array($tmp[0], static::INTERNAL_PLACEHOLDER, false);
return !in_array($tmp[0], array_merge(static::INTERNAL_PLACEHOLDER, static::BUILTIN_PLACEHOLDER), false);
}
/**
* Checks if the attachment file is using a builtin file. (see BUILTIN_PLACEHOLDERS const for possible placeholders)
* If a file is built in, the path is shown to user in url field (no sensitive infos are provided)
* @return bool True if the attachment is uning an builtin file.
*/
public function isBuiltIn() : bool
{
return static::checkIfBuiltin($this->path);
}
/********************************************************************************
@ -167,13 +180,13 @@ abstract class Attachment extends NamedDBElement
}
/**
* The URL to the external file.
* Returns null, if the file is not external.
* The URL to the external file, or the path to the built in file.
* Returns null, if the file is not external (and not builtin).
* @return string|null
*/
public function getURL(): ?string
{
if (!$this->isExternal()) {
if (!$this->isExternal() && !$this->isBuiltIn()) {
return null;
}
@ -204,8 +217,6 @@ abstract class Attachment extends NamedDBElement
return $this->path;
}
/**
* Returns the filename of the attachment.
* For a path like %BASE/path/foo.bar, foo.bar will be returned.
@ -336,6 +347,22 @@ abstract class Attachment extends NamedDBElement
* Static functions
*****************************************************************************************************/
/**
* Checks if the given path is a path to a builtin ressource.
* @param string $path The path that should be checked
* @return bool True if the path is pointing to a builtin ressource.
*/
public static function checkIfBuiltin(string $path) : bool
{
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode
$tmp = explode('/', $path);
//Builtins must have a %PLACEHOLDER% construction
if (empty($tmp)) {
return false;
}
return in_array($tmp[0], static::BUILTIN_PLACEHOLDER, false);
}
/**
* Check if a string is a URL and is valid.
* @param $string string The string which should be checked.

View file

@ -66,6 +66,11 @@ class AttachmentDeleteListener
public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event)
{
if ($event->hasChangedField('path')) {
//Dont delete file if the attachment uses a builtin ressource:
if (Attachment::checkIfBuiltin($event->getOldValue('path'))) {
return;
}
$file = new \SplFileInfo($this->attachmentHelper->placeholderToRealPath($event->getOldValue('path')));
$this->attachmentReverseSearch->deleteIfNotUsed($file);
}
@ -81,6 +86,11 @@ class AttachmentDeleteListener
*/
public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event)
{
//Dont delete file if the attachment uses a builtin ressource:
if (Attachment::checkIfBuiltin($event->getOldValue('path'))) {
return;
}
$file = $this->attachmentHelper->attachmentToFile($attachment);
//Only delete if the attachment has a valid file.
if ($file !== null) {

View file

@ -37,6 +37,7 @@ use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\StructuralDBElement;
use App\Form\Type\StructuralEntityType;
use App\Services\AttachmentHelper;
use App\Validator\Constraints\UrlOrBuiltin;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
@ -75,11 +76,11 @@ class AttachmentFormType extends AbstractType
'attr' => ['class' => 'form-control-sm'],
'label_attr' => ['class' => 'checkbox-custom']]);
$builder->add('url', UrlType::class, [
$builder->add('url', TextType::class, [
'label' => $this->trans->trans('attachment.edit.url'),
'required' => false,
'constraints' => [
new Url()
$options['allow_builtins'] ? new UrlOrBuiltin() : new Url()
]
]);
@ -101,7 +102,8 @@ class AttachmentFormType extends AbstractType
{
$resolver->setDefaults([
'data_class' => Attachment::class,
'max_file_size' => '16M'
'max_file_size' => '16M',
'allow_builtins' => true
]);
}

View file

@ -0,0 +1,46 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 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 General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Validator\Constraints;
use App\Entity\Attachments\Attachment;
use Symfony\Component\Validator\Constraints\Url;
/**
* Constraints the field that way that the content is either a url or a path to a builtin ressource (like %FOOTPRINTS%)
* @package App\Validator\Constraints
*/
class UrlOrBuiltin extends Url
{
/** @var array A list of the placeholders that are treated as builtin */
public $allowed_placeholders = Attachment::BUILTIN_PLACEHOLDER;
}

View file

@ -0,0 +1,76 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 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 General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\UrlValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
/**
* The validator for UrlOrBuiltin.
* It checks if the value is either a builtin ressource or a valid url.
* In both cases it is not checked, if the ressource is really existing.
* @package App\Validator\Constraints
*/
class UrlOrBuiltinValidator extends UrlValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof UrlOrBuiltin) {
throw new UnexpectedTypeException($constraint, UrlOrBuiltin::class);
}
if (null === $value || '' === $value) {
return;
}
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if ('' === $value) {
return;
}
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode
$tmp = explode('/', $value);
//Builtins must have a %PLACEHOLDER% construction
if (!empty($tmp) && in_array($tmp[0], $constraint->allowed_placeholders, false)) {
return;
}
parent::validate($value, $constraint); // TODO: Change the autogenerated stub
}
}

View file

@ -105,6 +105,27 @@ class AttachmentTest extends TestCase
$this->assertTrue($attachment->isPicture());
}
public function testIsBuiltIn()
{
$attachment = new PartAttachment();
$this->setProtectedProperty($attachment, 'path', '%MEDIA%/foo/bar.txt');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', '%BASE%/foo/bar.txt');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', '/');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', 'https://google.de');
$this->assertFalse($attachment->isBuiltIn());
$this->setProtectedProperty($attachment, 'path', '%FOOTPRINTS%/foo/bar.txt');
$this->assertTrue($attachment->isBuiltIn());
}
public function testGetHost()
{
$attachment = new PartAttachment();