Uploaded (non secure) attachments live now in public/

That way the attachment files can now be loaded much quicker (without invoking a controller). Also added thumbnailing for pictures in tables.
This commit is contained in:
Jan Böhmer 2019-10-05 20:30:27 +02:00
parent 1b28006267
commit 4fe10b6169
21 changed files with 552 additions and 21 deletions

View file

@ -37,6 +37,7 @@ use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\FootprintAttachment;
use App\Entity\Parts\Part;
use App\Services\AttachmentHelper;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\ElementTypeNameGenerator;
use App\Services\EntityURLGenerator;
use Doctrine\ORM\QueryBuilder;
@ -54,14 +55,17 @@ class AttachmentDataTable implements DataTableTypeInterface
protected $entityURLGenerator;
protected $attachmentHelper;
protected $elementTypeNameGenerator;
protected $attachmentURLGenerator;
public function __construct(TranslatorInterface $translator, EntityURLGenerator $entityURLGenerator,
AttachmentHelper $attachmentHelper, ElementTypeNameGenerator $elementTypeNameGenerator)
AttachmentHelper $attachmentHelper, AttachmentURLGenerator $attachmentURLGenerator,
ElementTypeNameGenerator $elementTypeNameGenerator)
{
$this->translator = $translator;
$this->entityURLGenerator = $entityURLGenerator;
$this->attachmentHelper = $attachmentHelper;
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
$this->attachmentURLGenerator = $attachmentURLGenerator;
}
protected function getQuery(QueryBuilder $builder)
@ -89,7 +93,7 @@ class AttachmentDataTable implements DataTableTypeInterface
return sprintf(
'<img alt="%s" src="%s" class="%s">',
'Part image',
$this->entityURLGenerator->viewURL($context),
$this->attachmentURLGenerator->getThumbnailURL($context),
'img-fluid hoverpic'
);
}

View file

@ -39,6 +39,7 @@ use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
use App\Entity\Parts\Supplier;
use App\Services\AmountFormatter;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\Attachments\PartPreviewGenerator;
use App\Services\EntityURLGenerator;
use App\Services\ToolsTreeBuilder;
@ -65,15 +66,18 @@ class PartsDataTable implements DataTableTypeInterface
protected $treeBuilder;
protected $amountFormatter;
protected $previewGenerator;
protected $attachmentURLGenerator;
public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator,
TreeBuilder $treeBuilder, AmountFormatter $amountFormatter, PartPreviewGenerator $previewGenerator)
TreeBuilder $treeBuilder, AmountFormatter $amountFormatter,
PartPreviewGenerator $previewGenerator, AttachmentURLGenerator $attachmentURLGenerator)
{
$this->urlGenerator = $urlGenerator;
$this->translator = $translator;
$this->treeBuilder = $treeBuilder;
$this->amountFormatter = $amountFormatter;
$this->previewGenerator = $previewGenerator;
$this->attachmentURLGenerator = $attachmentURLGenerator;
}
protected function getQuery(QueryBuilder $builder)
@ -164,7 +168,7 @@ class PartsDataTable implements DataTableTypeInterface
return sprintf(
'<img alt="%s" src="%s" class="%s">',
'Part image',
$this->urlGenerator->viewURL($preview_attachment),
$this->attachmentURLGenerator->getThumbnailURL($preview_attachment),
'img-fluid hoverpic'
);
}

View file

@ -64,7 +64,7 @@ abstract class Attachment extends NamedDBElement
/**
* When the path begins with one of this placeholders
*/
public const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%'];
public const INTERNAL_PLACEHOLDER = ['%BASE%', '%MEDIA%', '%SECURE%'];
/** @var array Placeholders for attachments which using built in files. */
public const BUILTIN_PLACEHOLDER = ['%FOOTPRINTS%', '%FOOTPRINTS3D%'];
@ -156,6 +156,23 @@ abstract class Attachment extends NamedDBElement
return !in_array($tmp[0], array_merge(static::INTERNAL_PLACEHOLDER, static::BUILTIN_PLACEHOLDER), false);
}
/**
* Check if this attachment is saved in a secure place.
* This means that it can not be accessed directly via a web request, but must be viewed via a controller.
* @return bool True, if the file is secure.
*/
public function isSecure() : bool
{
//After the %PLACEHOLDER% comes a slash, so we can check if we have a placholder via explode
$tmp = explode("/", $this->path);
if (empty($tmp)) {
return false;
}
return $tmp[0] === '%SECURE%';
}
/**
* 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)

View file

@ -75,7 +75,8 @@ class AttachmentHelper
}
/**
* Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved.
* Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved,
* or is not existing.
* @param Attachment $attachment The attachment for which the filepath should be determined
* @return string|null
*/
@ -95,7 +96,13 @@ class AttachmentHelper
if ($path === null) {
return null;
}
return realpath($path);
$tmp = realpath($path);
//If path is not existing realpath returns false.
if ($tmp === false) {
return null;
}
return $tmp;
}
/**

View file

@ -0,0 +1,156 @@
<?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\Services\Attachments;
use App\Entity\Attachments\Attachment;
use App\Services\AttachmentHelper;
use Liip\ImagineBundle\Service\FilterService;
use Symfony\Component\Asset\Package;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AttachmentURLGenerator
{
protected $assets;
protected $public_path;
protected $pathResolver;
protected $urlGenerator;
protected $attachmentHelper;
protected $filterService;
public function __construct(Packages $assets, AttachmentPathResolver $pathResolver,
UrlGeneratorInterface $urlGenerator, AttachmentHelper $attachmentHelper,
FilterService $filterService)
{
$this->assets = $assets;
$this->pathResolver = $pathResolver;
$this->urlGenerator = $urlGenerator;
$this->attachmentHelper = $attachmentHelper;
$this->filterService = $filterService;
//Determine a normalized path to the public folder (assets are relative to this folder)
$this->public_path = $this->pathResolver->parameterToAbsolutePath('public');
}
/**
* Converts the absolute file path to a version relative to the public folder, that can be passed to asset
* Asset Component functions.
* @param string $absolute_path The absolute path that should be converted.
* @param string|null $public_path The public path to which the relative pathes should be created.
* The path must NOT have a trailing slash!
* If this is set to null, the global public/ folder is used.
* @return string|null The relative version of the string. Null if the absolute path was not a child folder
* of public path
*/
public function absolutePathToAssetPath(string $absolute_path, ?string $public_path = null) : ?string
{
if ($public_path === null) {
$public_path = $this->public_path;
}
//Our absolute path must begin with public path or we can not use it for asset pathes.
if (strpos($absolute_path, $public_path) !== 0) {
return null;
}
//Return the part relative after public path.
return substr($absolute_path, strlen($public_path) + 1);
}
/**
* Returns a URL under which the attachment file can be viewed.
* @param Attachment $attachment
* @return string
*/
public function getViewURL(Attachment $attachment) : string
{
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
if ($absolute_path === null) {
throw new \RuntimeException(
'The given attachment is external or has no valid file, so no URL can get generated for it!
Use Attachment::getURL() to get the external URL!'
);
}
$asset_path = $this->absolutePathToAssetPath($absolute_path);
//If path is not relative to public path or marked as secure, serve it via controller
if ($asset_path === null || $attachment->isSecure()) {
return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
}
//Otherwise we can serve the relative path via Asset component
return $this->assets->getUrl($asset_path);
}
/**
* Returns a URL to an thumbnail of the attachment file.
* @param Attachment $attachment
* @param string $filter_name
* @return string
*/
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm') : string
{
if (!$attachment->isPicture()) {
throw new \InvalidArgumentException('Thumbnail creation only works for picture attachments!');
}
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
if ($absolute_path === null) {
throw new \RuntimeException(
'The given attachment is external or has no valid file, so no URL can get generated for it!
Use Attachment::getURL() to get the external URL!'
);
}
$asset_path = $this->absolutePathToAssetPath($absolute_path);
//If path is not relative to public path or marked as secure, serve it via controller
if ($asset_path === null || $attachment->isSecure()) {
return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
}
//Otherwise we can serve the relative path via Asset component
return $this->filterService->getUrlOfFilteredImage($asset_path, 'thumbnail_sm');
}
/**
* Returns a download link to the file associated with the attachment
* @param Attachment $attachment
* @return string
*/
public function getDownloadURL(Attachment $attachment) : string
{
//Redirect always to download controller, which sets the correct headers for downloading:
$this->urlGenerator->generate('attachment_download', ['id' => $attachment->getID()]);
}
}

View file

@ -45,6 +45,7 @@ use App\Entity\PriceInformations\Currency;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use App\Exceptions\EntityNotSupportedException;
use App\Services\Attachments\AttachmentURLGenerator;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@ -60,10 +61,12 @@ class EntityURLGenerator
* @var UrlGeneratorInterface
*/
protected $urlGenerator;
protected $attachmentURLGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
public function __construct(UrlGeneratorInterface $urlGenerator, AttachmentURLGenerator $attachmentURLGenerator)
{
$this->urlGenerator = $urlGenerator;
$this->attachmentURLGenerator = $attachmentURLGenerator;
}
/**
@ -138,7 +141,8 @@ class EntityURLGenerator
if ($entity->isExternal()) { //For external attachments, return the link to external path
return $entity->getURL();
}
return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]);
//return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]);
return $this->attachmentURLGenerator->getViewURL($entity);
}
//Otherwise throw an error
@ -151,7 +155,7 @@ class EntityURLGenerator
if ($entity->isExternal()) { //For external attachments, return the link to external path
return $entity->getURL();
}
return $this->urlGenerator->generate('attachment_download', ['id' => $entity->getID()]);
return $this->attachmentURLGenerator->getDownloadURL($entity);
}
//Otherwise throw an error