3

我想展示一个用 gaufrette 上传的 PDF 文件。我从 Doctrine ("wine_information/1-wine_information.pdf") 获得 URI。

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

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

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

上传服务.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>

4

1 回答 1

2

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

应用\配置\服务.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';
}
}
于 2019-01-17T08:19:56.387 回答