Skip to content

Commit ca1cc54

Browse files
authored
Merge pull request #149 from plank/4.0
4.0 Release
2 parents a322bc9 + 8f3d356 commit ca1cc54

17 files changed

+127
-256
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coverage/
44
.env
55
.idea/
66
.phpunit.result.cache
7+
infection/

.travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ script:
1313
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
1414
after_script:
1515
- php vendor/bin/php-coveralls -v
16-
branches:
17-
only:
18-
- master
19-
- develop
2016
cache:
2117
directories:
2218
- $HOME/.composer/cache

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.0.0
4+
- changed UrlGenerators to use the underlying filesystemAdapter's `url()` method
5+
- UrlGenerators no longer throw `MediaUrlException` when the file does not have public visibility. This removes the need to read IO for files local disks or to make HTTP calls for files on s3 disks.
6+
- Removed `LocalUrlGenerator::getPublicPath()`
7+
- No longer reading the `'prefix'` config of local disks. Value should be included in the `'url'` config instead.
8+
39
## 3.0.1 - 2019-09-18
410
- Fixed public visibility not being respected when generating URLs for local files that are not in the webroot.
511

UPGRADING.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upgrading
22

3+
## 3.x to 4.x
4+
5+
* UrlGenerators no longer throw `MediaUrlException` when the file does not have public visibility. This removes the need to read IO for files local disks or to make HTTP calls for files on s3 disks. Visibility can still checked with `$media->isPubliclyAccessible()`, if necessary.
6+
* Highly recommended to explicitly specify the `'url'` config value on all disks used to generate URLs.
7+
* No longer reading the `'prefix'` config of local disks. Value should be included in the `'url'` config instead.
8+
39
## 2.x to 3.x
410

511
* Minimum PHP version moved to 7.2

docs/source/configuration.rst

+5-117
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Disks
1010
------------------------
1111
Laravel-Mediable is built on top of Laravel's Filesystem component. Before you use the package, you will need to configure the filesystem disks where you would like files to be stored in ``config/filesystems.php``. `Learn more about filesystem disk <https://laravel.com/docs/5.2/filesystem>`_.
1212

13-
An example setup with one private disk (``local``) and one publicly accessible disk (``uploads``):
14-
1513
::
1614

1715
<?php
@@ -20,11 +18,15 @@ An example setup with one private disk (``local``) and one publicly accessible d
2018
'local' => [
2119
'driver' => 'local',
2220
'root' => storage_path('app'),
21+
'url' => 'https://example.com/storage/app',
22+
'visibility' => 'public'
2323
],
2424

2525
'uploads' => [
2626
'driver' => 'local',
2727
'root' => public_path('uploads'),
28+
'url' => 'https://example.com/uploads',
29+
'visibility' => 'public'
2830
],
2931
]
3032
//...
@@ -45,125 +47,11 @@ Once you have set up as many disks as you need, edit ``config/mediable.php`` to
4547
* Filesystems that can be used for media storage
4648
*/
4749
'allowed_disks' => [
50+
'local',
4851
'uploads',
4952
],
5053
//...
5154

52-
.. _disk_visibility:
53-
54-
Disk Visibility
55-
^^^^^^^^^^^^^^^
56-
57-
This package is able to generate public URLs for accessing media, and uses the disk config to determine how this should be done.
58-
59-
URLs can always be generated for ``Media`` placed on a disk located below the webroot.
60-
61-
::
62-
63-
<?php
64-
'disks' => [
65-
'uploads' => [
66-
'driver' => 'local',
67-
'root' => public_path('uploads'),
68-
],
69-
]
70-
71-
//...
72-
73-
$media->getUrl(); // returns http://domain.com/uploads/foo.jpg
74-
75-
``Media`` placed on a disk located elsewhere will throw an exception.
76-
77-
::
78-
79-
<?php
80-
'disks' => [
81-
'private' => [
82-
'driver' => 'local',
83-
'root' => storage_path('private'),
84-
],
85-
]
86-
87-
//...
88-
89-
$media->getUrl(); // Throws a Plank\Mediable\Exceptions\MediableUrlException
90-
91-
If you are using symbolic links to make local disks accessible, you can instruct the package to generate URLs with the ``'visibility' => 'public'`` key. By default, the package will assume that the symlink is named ``'storage'``, as per `laravel's documentation <https://laravel.com/docs/5.3/filesystem#the-public-disk>`_. This can be modified with the ``'prefix'`` key.
92-
93-
::
94-
95-
<?php
96-
'disks' => [
97-
'public' => [
98-
'driver' => 'local',
99-
'root' => storage_path('public'),
100-
'visibility' => 'public',
101-
'prefix' => 'assets'
102-
],
103-
]
104-
105-
//...
106-
107-
$media->getUrl(); // returns http://domain.com/assets/foo.jpg
108-
109-
Whether you are using symbolic links or not, you can set the ``'url'`` config value to generate disk urls on another domain. Note that you can specify any path in the url, as the root path doesn't have to match, as long as you have set up your web server accordingly.
110-
111-
::
112-
113-
<?php
114-
'disks' => [
115-
'uploads' => [
116-
'driver' => 'local',
117-
'root' => public_path('uploads'),
118-
'url' => 'http://example.com/assets',
119-
],
120-
]
121-
122-
//...
123-
124-
$media->getUrl(); // returns http://example.com/assets/foo.jpg
125-
126-
However, if you are using a symbolic link to make a local disk accessible, the prefix will be appended to the disk url.
127-
128-
::
129-
130-
<?php
131-
'disks' => [
132-
'public' => [
133-
'driver' => 'local',
134-
'root' => storage_path('public'),
135-
'visibility' => 'public',
136-
'prefix' => 'assets',
137-
'url' => 'http://example.com',
138-
],
139-
]
140-
141-
//...
142-
143-
$media->getUrl(); // returns http://example.com/assets/foo.jpg
144-
145-
146-
Permissions for S3-based disks is set on the buckets themselves. You can inform the package that ``Media`` on an S3 disk can be linked by URL by adding the ``'visibility' => 'public'`` key to the disk config.
147-
148-
::
149-
150-
<?php
151-
'disks' => [
152-
'cloud' => [
153-
'driver' => 's3',
154-
'key' => env('S3_KEY'),
155-
'secret' => env('S3_SECRET'),
156-
'region' => env('S3_REGION'),
157-
'bucket' => env('S3_BUCKET'),
158-
'version' => 'latest',
159-
'visibility' => 'public'
160-
],
161-
]
162-
163-
//...
164-
165-
$media->getUrl(); // returns https://s3.amazonaws.com/bucket/foo.jpg
166-
16755

16856
.. _validation:
16957

infection.json.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"timeout": 10,
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
"logs": {
9+
"text": "infection\/infection.log"
10+
},
11+
"mutators": {
12+
"@default": true
13+
}
14+
}

phpunit.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
convertWarningsToExceptions="true"
99
processIsolation="false"
1010
stopOnFailure="false"
11+
executionOrder="random"
12+
resolveDependencies="true"
1113
>
1214
<testsuites>
1315
<testsuite name="Package Test Suite">

src/Exceptions/MediaUpload/ConfigurationException.php

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ public static function unrecognizedSource($source): self
2828
$source = get_class($source);
2929
} elseif (is_resource($source)) {
3030
$source = get_resource_type($source);
31-
} else {
32-
$source = (string)$source;
3331
}
3432

3533
return new static("Could not recognize source, `{$source}` provided.");

src/Exceptions/MediaUrlException.php

-11
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,4 @@ public static function invalidGenerator(string $class): self
1919
{
2020
return new static("Could not set UrlGenerator, class `{$class}` does not extend `Plank\Mediable\UrlGenerators\UrlGenerator`");
2121
}
22-
23-
public static function mediaNotPubliclyAccessible(string $path): self
24-
{
25-
$public_path = public_path();
26-
return new static("Media file `{$path}` is not part of the public path `{$public_path}`");
27-
}
28-
29-
public static function cloudMediaNotPubliclyAccessible(string $disk): self
30-
{
31-
return new static("Media files on cloud disk `{$disk}` are not publicly accessible");
32-
}
3322
}

src/UrlGenerators/LocalUrlGenerator.php

+13-77
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,34 @@
44
namespace Plank\Mediable\UrlGenerators;
55

66
use Illuminate\Contracts\Config\Repository as Config;
7-
use Illuminate\Contracts\Routing\UrlGenerator;
8-
use Plank\Mediable\Exceptions\MediaUrlException;
7+
use Illuminate\Contracts\Filesystem\Cloud;
8+
use Illuminate\Filesystem\FilesystemManager;
99

1010
class LocalUrlGenerator extends BaseUrlGenerator
1111
{
1212
/**
13-
* @var UrlGenerator
13+
* @var FilesystemManager
1414
*/
15-
protected $url;
15+
protected $filesystem;
1616

1717
/**
1818
* Constructor.
1919
* @param Config $config
20-
* @param UrlGenerator $url
20+
* @param FilesystemManager $filesystem
2121
*/
22-
public function __construct(Config $config, UrlGenerator $url)
22+
public function __construct(Config $config, FilesystemManager $filesystem)
2323
{
2424
parent::__construct($config);
25-
$this->url = $url;
25+
$this->filesystem = $filesystem;
2626
}
2727

2828
/**
2929
* {@inheritdoc}
3030
*/
3131
public function isPubliclyAccessible(): bool
3232
{
33-
return (parent::isPubliclyAccessible() || $this->isInWebroot()) && $this->media->isVisible();
34-
}
35-
36-
/**
37-
* Get the path to relative to the webroot.
38-
* @return string
39-
* @throws MediaUrlException If media's disk is not publicly accessible
40-
*/
41-
public function getPublicPath(): string
42-
{
43-
if (!$this->isPubliclyAccessible()) {
44-
throw MediaUrlException::mediaNotPubliclyAccessible($this->getAbsolutePath());
45-
}
46-
if ($this->isInWebroot()) {
47-
$path = str_replace(public_path(), '', $this->getAbsolutePath());
48-
} else {
49-
$path = rtrim($this->getPrefix(), '/') . '/' . $this->media->getDiskPath();
50-
}
51-
52-
return $this->cleanDirectorySeparators($path);
33+
return ($this->getDiskConfig('visibility', 'private') == 'public' || $this->isInWebroot())
34+
&& $this->media->isVisible();
5335
}
5436

5537
/**
@@ -58,19 +40,9 @@ public function getPublicPath(): string
5840
*/
5941
public function getUrl(): string
6042
{
61-
$path = $this->getPublicPath();
62-
63-
$url = $this->getDiskConfig('url');
64-
65-
if ($url) {
66-
if ($this->isInWebroot()) {
67-
$path = $this->media->getDiskPath();
68-
}
69-
70-
return rtrim($url, '/') . '/' . trim($path, '/');
71-
}
72-
73-
return $this->url->asset($path);
43+
/** @var Cloud $filesystem */
44+
$filesystem = $this->filesystem->disk($this->media->disk);
45+
return $filesystem->url($this->media->getDiskPath());
7446
}
7547

7648
/**
@@ -81,44 +53,8 @@ public function getAbsolutePath(): string
8153
return $this->getDiskConfig('root') . DIRECTORY_SEPARATOR . $this->media->getDiskPath();
8254
}
8355

84-
/**
85-
* Correct directory separator slashes on non-unix systems.
86-
* @param string $path
87-
* @return string
88-
*/
89-
protected function cleanDirectorySeparators(string $path): string
90-
{
91-
if (DIRECTORY_SEPARATOR != '/') {
92-
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
93-
}
94-
95-
return $path;
96-
}
97-
9856
private function isInWebroot(): bool
9957
{
100-
return strpos(realpath($this->getAbsolutePath()), realpath(public_path())) === 0;
101-
}
102-
103-
/**
104-
* Get the prefix.
105-
*
106-
* If the prefix and the url are not set, we will assume the prefix
107-
* is "storage", in order to point to the default symbolic link.
108-
*
109-
* Otherwise, we will trust the user has correctly set the prefix and/or the url.
110-
*
111-
* @return string
112-
*/
113-
private function getPrefix()
114-
{
115-
$prefix = $this->getDiskConfig('prefix', '');
116-
$url = $this->getDiskConfig('url');
117-
118-
if (!$prefix && !$url) {
119-
return 'storage';
120-
}
121-
122-
return $prefix;
58+
return strpos($this->getAbsolutePath(), public_path()) === 0;
12359
}
12460
}

src/UrlGenerators/S3UrlGenerator.php

-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Contracts\Config\Repository as Config;
77
use Illuminate\Contracts\Filesystem\Cloud;
88
use Illuminate\Filesystem\FilesystemManager;
9-
use Plank\Mediable\Exceptions\MediaUrlException;
109

1110
class S3UrlGenerator extends BaseUrlGenerator
1211
{
@@ -40,10 +39,6 @@ public function getAbsolutePath(): string
4039
*/
4140
public function getUrl(): string
4241
{
43-
if (!$this->isPubliclyAccessible()) {
44-
throw MediaUrlException::cloudMediaNotPubliclyAccessible($this->media->disk);
45-
}
46-
4742
/** @var Cloud $filesystem */
4843
$filesystem = $this->filesystem->disk($this->media->disk);
4944
return $filesystem->url($this->media->getDiskPath());

0 commit comments

Comments
 (0)