|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the overtrue/laravel-ueditor. |
| 5 | + * (c) overtrue <[email protected]> |
| 6 | + * This source file is subject to the MIT license that is bundled |
| 7 | + * with this source code in the file LICENSE. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Overtrue\LaravelUEditor; |
| 11 | + |
| 12 | +use Illuminate\Support\Str; |
| 13 | +use League\Flysystem\Adapter\Local as LocalAdapter; |
| 14 | +use League\Flysystem\AwsS3v3\AwsS3Adapter; |
| 15 | +use RuntimeException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Trait UrlResolverTrait. |
| 19 | + */ |
| 20 | +trait UrlResolverTrait |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param string $filename |
| 24 | + * |
| 25 | + * @return string |
| 26 | + */ |
| 27 | + public function getUrl($filename) |
| 28 | + { |
| 29 | + if (method_exists($this->disk, 'url')) { |
| 30 | + return $this->disk->url($filename); |
| 31 | + } |
| 32 | + |
| 33 | + return $this->url($filename); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the URL for the file at the given path. |
| 38 | + * |
| 39 | + * @param string $path |
| 40 | + * |
| 41 | + * @return string |
| 42 | + */ |
| 43 | + public function url($path) |
| 44 | + { |
| 45 | + $adapter = $this->disk->getDriver()->getAdapter(); |
| 46 | + |
| 47 | + if (method_exists($adapter, 'getUrl')) { |
| 48 | + return $adapter->getUrl($path); |
| 49 | + } elseif ($adapter instanceof AwsS3Adapter) { |
| 50 | + return $this->getAwsUrl($adapter, $path); |
| 51 | + } elseif ($adapter instanceof LocalAdapter) { |
| 52 | + return $this->getLocalUrl($path); |
| 53 | + } |
| 54 | + throw new RuntimeException('This driver does not support retrieving URLs.'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the URL for the file at the given path. |
| 59 | + * |
| 60 | + * @param \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter |
| 61 | + * @param string $path |
| 62 | + * |
| 63 | + * @return string |
| 64 | + */ |
| 65 | + protected function getAwsUrl($adapter, $path) |
| 66 | + { |
| 67 | + return $adapter->getClient()->getObjectUrl( |
| 68 | + $adapter->getBucket(), $adapter->getPathPrefix().$path |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the URL for the file at the given path. |
| 74 | + * |
| 75 | + * @param string $path |
| 76 | + * |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + protected function getLocalUrl($path) |
| 80 | + { |
| 81 | + $config = $this->disk->getDriver()->getConfig(); |
| 82 | + |
| 83 | + // If an explicit base URL has been set on the disk configuration then we will use |
| 84 | + // it as the base URL instead of the default path. This allows the developer to |
| 85 | + // have full control over the base path for this filesystem's generated URLs. |
| 86 | + if ($config->has('url')) { |
| 87 | + return rtrim($config->get('url'), '/').'/'.ltrim($path, '/'); |
| 88 | + } |
| 89 | + |
| 90 | + $path = '/storage/'.$path; |
| 91 | + |
| 92 | + // If the path contains "storage/public", it probably means the developer is using |
| 93 | + // the default disk to generate the path instead of the "public" disk like they |
| 94 | + // are really supposed to use. We will remove the public from this path here. |
| 95 | + if (Str::contains($path, '/storage/public/')) { |
| 96 | + return Str::replaceFirst('/public/', '/', $path); |
| 97 | + } |
| 98 | + |
| 99 | + return $path; |
| 100 | + } |
| 101 | +} |
0 commit comments