如何在树枝中显示与gaufrette上传的文件的链接?

时间:2019-01-15 10:53:35

标签: php symfony twig gaufrette

我想显示一个用gaufrette上传的PDF文件。 我从Doctrine中获得了URI(“ wine_information / 1-wine_information.pdf”)。

但是如果根目录位于本地或远程(Amazon s3)上,则可以不同。

我尝试使用liip_imagine的过滤器细枝来使用CacheManager.php和方法getBrowserPath,但由于没有可应用于PDF的过滤器,因此无法创建过滤器。它仅用于图像。

现在我找到了此解决方案:https://github.com/KnpLabs/Gaufrette/issues/332#issuecomment-159849533

UploaderService.php

public function getPath($filePath)
{
    $fileSystem = $this->container->get('gaufrette.' . $this->env . self::MEDIA_FILESYSTEM);
    $adapter = $fileSystem->getAdapter();
    $reflection = new \ReflectionClass($adapter);
    $directory = $reflection->getProperty('directory');
    $directory->setAccessible(true);

    return $directory->getValue($adapter) . $filePath;
} 

在本地时,返回以下路径: /home/company/projectName/web/media/wine_information/1-wine_information.pdf

但是我想在我的<a href="" target="_blanck"></a>

中设置一个真实的链接

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我做了一个树枝过滤器,以获取使用gaufrette上传的文件的路径。用于 Amazon S3 本地的解决方案。

app \ config \ services.yml

app.aws_s3.client:
    class: Aws\S3\S3Client
    arguments:
        -
            version: '%amazon_s3.version%'
            region: '%amazon_s3.region%'
            credentials:
                key: '%amazon_s3.key%'
                secret: '%amazon_s3.secret%'

html.twig

<a class="btn btn-primary" href="{{ pdf|get_path }}" target="_blank">Afficher le PDF actuel</a>

AppBundle \ Resources \ config \ services.yml

app.twig_filter_extension:
    class: AppBundle\Twig\AppFilterExtension
    public: true
    arguments:
        - '%kernel.environment%'
        - '%prod_env%'
        - '%kernel.root_dir%'
        - '@router.request_context'
        - '@app.aws_s3.client'
        - '%amazon_s3.media_bucket%'
    tags:
        - { name: twig.extension }

AppBundle \ Twig \ AppFilterExtension.php

<?php

namespace AppBundle\Twig;

use Aws\S3\S3Client;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RequestContext;

class AppFilterExtension extends \Twig_Extension
{
/**
 * @var string
 */
private $env;

/**
 * @var bool
 */
private $prodEnv;

/**
 * @var S3Client
 */
private $s3Client;

/**
 * @var string
 */
private $s3Bucket;

/**
 * @var Filesystem
 */
private $fileSystem;

/**
 * @var string
 */
private $rootPath;

/**
 * @var string
 */
private $baseUrl;

/**
 * AppFilterExtension constructor.
 *
 * @param string $env
 * @param bool   $prodEnv
 */
public function __construct(string $env, bool $prodEnv, string $rootDir, RequestContext $requestContext, S3Client $s3Client, string $s3Bucket)
{
    $this->env     = $env;
    $this->prodEnv = $prodEnv;

    $this->fileSystem = new Filesystem();
    $this->s3Client   = $s3Client;
    $this->s3Bucket   = $s3Bucket;
    $this->rootPath   = $rootDir . '/../web/';
    $this->baseUrl    = sprintf('%s://%s', $requestContext->getScheme(), $requestContext->getHost());
}

public function getFilters()
{
    return [
        new \Twig_SimpleFilter('get_path', [$this, 'getPath']),
    ];
}

/**
 * @param string      $uri
 * @param null|string $folder
 *
 * @return string
 */
public function getPath(string $uri, string $folder = null)
{
    if (
        (
            'dev' === $this->env ||
            false === $this->prodEnv
        ) &&
        true === $this->fileSystem->exists(str_replace('//', '/', sprintf('%s/%s/%s', $this->rootPath, ($folder ?? 'media'), $uri)))
    ) {
        return $this->baseUrl . str_replace('//', '/', sprintf('/%s/%s', ($folder ?? 'media'), $uri));
    }

    return str_replace('//', '/', $this->s3Client->getObjectUrl($folder ?? $this->s3Bucket, $uri));
}

/**
 * @return string
 */
public function getName()
{
    return 'app_filter_extension';
}
}