2019-10-05 20:30:27 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 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/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-05 20:30:27 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-10-05 20:30:27 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-10-05 20:30:27 +02:00
|
|
|
*
|
|
|
|
* 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;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2019-10-05 20:30:27 +02:00
|
|
|
use Liip\ImagineBundle\Service\FilterService;
|
2022-07-21 00:31:34 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use RuntimeException;
|
|
|
|
use function strlen;
|
2019-10-05 20:30:27 +02:00
|
|
|
use Symfony\Component\Asset\Packages;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
|
|
|
|
class AttachmentURLGenerator
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected Packages $assets;
|
|
|
|
protected string $public_path;
|
|
|
|
protected AttachmentPathResolver $pathResolver;
|
|
|
|
protected UrlGeneratorInterface $urlGenerator;
|
|
|
|
protected AttachmentManager $attachmentHelper;
|
|
|
|
protected FilterService $filterService;
|
2019-10-05 20:30:27 +02:00
|
|
|
|
2022-09-18 22:59:31 +02:00
|
|
|
protected LoggerInterface $logger;
|
2022-07-21 00:31:34 +02:00
|
|
|
|
2019-10-05 20:30:27 +02:00
|
|
|
public function __construct(Packages $assets, AttachmentPathResolver $pathResolver,
|
2019-10-19 23:29:51 +02:00
|
|
|
UrlGeneratorInterface $urlGenerator, AttachmentManager $attachmentHelper,
|
2022-07-21 00:31:34 +02:00
|
|
|
FilterService $filterService, LoggerInterface $logger)
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
|
|
|
$this->assets = $assets;
|
|
|
|
$this->pathResolver = $pathResolver;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->attachmentHelper = $attachmentHelper;
|
|
|
|
$this->filterService = $filterService;
|
2022-07-21 00:31:34 +02:00
|
|
|
$this->logger = $logger;
|
2019-10-05 20:30:27 +02:00
|
|
|
|
|
|
|
//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.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param string $absolute_path the absolute path that should be converted
|
2019-11-09 00:47:20 +01:00
|
|
|
* @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.
|
|
|
|
*
|
2019-10-05 20:30:27 +02:00
|
|
|
* @return string|null The relative version of the string. Null if the absolute path was not a child folder
|
2019-11-09 00:47:20 +01:00
|
|
|
* of public path
|
2019-10-05 20:30:27 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function absolutePathToAssetPath(string $absolute_path, ?string $public_path = null): ?string
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
2020-06-07 19:25:58 +02:00
|
|
|
//Normalize file path (public path, use / as file path)
|
|
|
|
$absolute_path = str_replace('\\', '/', $absolute_path);
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $public_path) {
|
2019-10-05 20:30:27 +02:00
|
|
|
$public_path = $this->public_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Our absolute path must begin with public path or we can not use it for asset pathes.
|
2019-11-09 00:47:20 +01:00
|
|
|
if (0 !== strpos($absolute_path, $public_path)) {
|
2019-10-05 20:30:27 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return the part relative after public path.
|
2020-01-05 22:49:00 +01:00
|
|
|
return substr($absolute_path, strlen($public_path) + 1);
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 14:51:38 +02:00
|
|
|
/**
|
|
|
|
* Converts a placeholder path to a path to a image path.
|
|
|
|
*
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param string $placeholder_path the placeholder path that should be converted
|
2020-06-01 14:51:38 +02:00
|
|
|
*/
|
|
|
|
public function placeholderPathToAssetPath(string $placeholder_path): ?string
|
|
|
|
{
|
|
|
|
$absolute_path = $this->pathResolver->placeholderToRealPath($placeholder_path);
|
2020-08-21 21:36:22 +02:00
|
|
|
|
2020-06-01 14:51:38 +02:00
|
|
|
return $this->absolutePathToAssetPath($absolute_path);
|
|
|
|
}
|
|
|
|
|
2019-10-05 20:30:27 +02:00
|
|
|
/**
|
|
|
|
* Returns a URL under which the attachment file can be viewed.
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getViewURL(Attachment $attachment): string
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
|
|
|
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $absolute_path) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!
|
2019-11-09 00:47:20 +01:00
|
|
|
Use Attachment::getURL() to get the external URL!');
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$asset_path = $this->absolutePathToAssetPath($absolute_path);
|
|
|
|
//If path is not relative to public path or marked as secure, serve it via controller
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $asset_path || $attachment->isSecure()) {
|
2019-10-05 20:30:27 +02:00
|
|
|
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.
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): string
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$attachment->isPicture()) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('Thumbnail creation only works for picture attachments!');
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:36:22 +02:00
|
|
|
if ($attachment->isExternal() && !empty($attachment->getURL())) {
|
2019-10-13 13:27:04 +02:00
|
|
|
return $attachment->getURL();
|
|
|
|
}
|
|
|
|
|
2019-10-05 20:30:27 +02:00
|
|
|
$absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $absolute_path) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new RuntimeException('The given attachment is external or has no valid file, so no URL can get generated for it!');
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$asset_path = $this->absolutePathToAssetPath($absolute_path);
|
|
|
|
//If path is not relative to public path or marked as secure, serve it via controller
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $asset_path || $attachment->isSecure()) {
|
2019-10-05 20:30:27 +02:00
|
|
|
return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
|
|
|
|
}
|
|
|
|
|
2019-10-06 15:44:19 +02:00
|
|
|
//For builtin ressources it is not useful to create a thumbnail
|
|
|
|
//because the footprints images are small and highly optimized already.
|
2020-05-21 22:09:13 +02:00
|
|
|
if (('thumbnail_md' === $filter_name && $attachment->isBuiltIn())
|
|
|
|
//GD can not work with SVG, so serve it directly...
|
2020-08-21 21:36:22 +02:00
|
|
|
|| 'svg' === $attachment->getExtension()) {
|
2019-10-06 15:44:19 +02:00
|
|
|
return $this->assets->getUrl($asset_path);
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:31:34 +02:00
|
|
|
try {
|
|
|
|
//Otherwise we can serve the relative path via Asset component
|
|
|
|
return $this->filterService->getUrlOfFilteredImage($asset_path, $filter_name);
|
|
|
|
} catch (\Imagine\Exception\RuntimeException $e) {
|
|
|
|
//If the filter fails, we can not serve the thumbnail and fall back to the original image and log an warning
|
|
|
|
$this->logger->warning('Could not open thumbnail for attachment with ID ' . $attachment->getID() . ': ' . $e->getMessage());
|
|
|
|
return $this->assets->getUrl($asset_path);
|
|
|
|
}
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Returns a download link to the file associated with the attachment.
|
2019-10-05 20:30:27 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getDownloadURL(Attachment $attachment): string
|
2019-10-05 20:30:27 +02:00
|
|
|
{
|
|
|
|
//Redirect always to download controller, which sets the correct headers for downloading:
|
2019-10-06 15:44:19 +02:00
|
|
|
return $this->urlGenerator->generate('attachment_download', ['id' => $attachment->getID()]);
|
2019-10-05 20:30:27 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|