Skip to content

Commit 9ffd70a

Browse files
committed
Fix sonata-project#232 - Download with x-sendfile not working
1 parent edd6cfe commit 9ffd70a

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

Diff for: Filesystem/Local.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/*
3+
* This file is part of the Sonata project.
4+
*
5+
* (c) Thomas Rabaix <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Sonata\MediaBundle\Filesystem;
12+
13+
use Gaufrette\Adapter\Local as BaseLocal;
14+
15+
class Local extends BaseLocal
16+
{
17+
public function getDirectory()
18+
{
19+
return $this->directory;
20+
}
21+
}

Diff for: Provider/FileProvider.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Sonata\MediaBundle\Provider;
1313

1414
use Sonata\MediaBundle\Model\MediaInterface;
15+
use Gaufrette\Adapter\Local;
1516
use Sonata\MediaBundle\CDN\CDNInterface;
1617
use Sonata\MediaBundle\Generator\GeneratorInterface;
1718
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
@@ -323,7 +324,14 @@ public function getDownloadResponse(MediaInterface $media, $format, $mode, array
323324
}, 200, $headers);
324325
}
325326

326-
$headers[$mode] = $this->generatePrivateUrl($media, $format);
327+
if (!$this->getFilesystem()->getAdapter() instanceof \Sonata\MediaBundle\Filesystem\Local) {
328+
throw new \RuntimeException('Cannot use X-Sendfile or X-Accel-Redirect with non \Sonata\MediaBundle\Filesystem\Local');
329+
}
330+
331+
$headers[$mode] = sprintf('%s/%s',
332+
$this->getFilesystem()->getAdapter()->getDirectory(),
333+
$this->generatePrivateUrl($media, $format)
334+
);
327335

328336
return new Response('', 200, $headers);
329337
}

Diff for: Resources/config/gaufrette.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55

66
<services>
7-
<service id="sonata.media.adapter.filesystem.local" class="Gaufrette\Adapter\Local" />
7+
<service id="sonata.media.adapter.filesystem.local" class="Sonata\MediaBundle\Filesystem\Local" />
88
<service id="sonata.media.adapter.filesystem.ftp" class="Gaufrette\Adapter\Ftp" />
99
<service id="sonata.media.adapter.service.s3" class="AmazonS3" >
1010
<argument type="collection" />

Diff for: Tests/Filesystem/LocalTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sonata package.
5+
*
6+
* (c) Thomas Rabaix <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sonata\MediaBundle\Tests\Filesystem;
13+
14+
use Sonata\MediaBundle\Filesystem\Local;
15+
16+
class LocalTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testReplicate()
19+
{
20+
$local = new Local('/tmp');
21+
22+
$this->assertEquals('/tmp', $local->getDirectory());
23+
}
24+
}

0 commit comments

Comments
 (0)