Completed
Push — master ( a50e59...22377b )
by Freek
10:42
created

src/UrlGenerator/LocalUrlGenerator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use DateTimeInterface;
6
use Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined;
7
8
class LocalUrlGenerator extends BaseUrlGenerator
9
{
10
    /**
11
     * Get the url for a media item.
12
     *
13
     * @return string
14
     *
15
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined
16
     */
17
    public function getUrl(): string
18
    {
19
        $url = $this->getBaseMediaDirectoryUrl().'/'.$this->getPathRelativeToRoot();
20
21
        $url = $this->makeCompatibleForNonUnixHosts($url);
22
23
        $url = $this->rawUrlEncodeFilename($url);
24
25
        return $url;
26
    }
27
28
    /**
29
     * @param \DateTimeInterface $expiration
30
     * @param array              $options
31
     *
32
     * @return string
33
     *
34
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined
35
     */
36
    public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
37
    {
38
        throw UrlCannotBeDetermined::filesystemDoesNotSupportTemporaryUrls();
39
    }
40
41
    /*
42
     * Get the path for the profile of a media item.
43
     */
44
    public function getPath(): string
45
    {
46
        return $this->getStoragePath().'/'.$this->getPathRelativeToRoot();
47
    }
48
49
    protected function getBaseMediaDirectoryUrl(): string
50
    {
51
        if ($diskUrl = $this->config->get("filesystems.disks.{$this->media->disk}.url")) {
52
            return str_replace(url('/'), '', $diskUrl);
53
        }
54
55
        if (! starts_with($this->getStoragePath(), public_path())) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
56
            throw UrlCannotBeDetermined::mediaNotPubliclyAvailable($this->getStoragePath(), public_path());
57
        }
58
59
        return $this->getBaseMediaDirectory();
60
    }
61
62
    /*
63
     * Get the directory where all files of the media item are stored.
64
     */
65
    protected function getBaseMediaDirectory(): string
66
    {
67
        return str_replace(public_path(), '', $this->getStoragePath());
68
    }
69
70
    /*
71
     * Get the path where the whole medialibrary is stored.
72
     */
73
    protected function getStoragePath() : string
74
    {
75
        $diskRootPath = $this->config->get("filesystems.disks.{$this->media->disk}.root");
76
77
        return realpath($diskRootPath);
78
    }
79
80
    protected function makeCompatibleForNonUnixHosts(string $url): string
81
    {
82
        if (DIRECTORY_SEPARATOR != '/') {
83
            $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
84
        }
85
86
        return $url;
87
    }
88
89
    /**
90
     * Get the url to the directory containing responsive images.
91
     *
92
     * @return string
93
     */
94
    public function getResponsiveImagesDirectoryUrl(): string
95
    {
96
        return url($this->getBaseMediaDirectoryUrl().'/'.$this->pathGenerator->getPathForResponsiveImages($this->media)).'/';
97
    }
98
}
99