Skip to content
This repository was archived by the owner on Aug 22, 2021. It is now read-only.

Commit c71ddd9

Browse files
committed
Fix url method compatible in laravel 5.1.
1 parent b103c50 commit c71ddd9

File tree

2 files changed

+107
-4
lines changed

2 files changed

+107
-4
lines changed

src/StorageManager.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99

1010
namespace Overtrue\LaravelUEditor;
1111

12+
use Illuminate\Contracts\Filesystem\Filesystem;
1213
use Illuminate\Http\Request;
1314
use Illuminate\Support\Facades\Storage;
14-
use Illuminate\Contracts\Filesystem\Filesystem;
1515
use Symfony\Component\HttpFoundation\File\UploadedFile;
1616

1717
/**
1818
* Class StorageManager.
1919
*/
2020
class StorageManager
2121
{
22+
use UrlResolverTrait;
23+
2224
/**
2325
* @var \Illuminate\Contracts\Filesystem\Filesystem
2426
*/
@@ -61,7 +63,7 @@ public function upload(Request $request)
6163

6264
$response = [
6365
'state' => 'SUCCESS',
64-
'url' => $this->disk->url($filename),
66+
'url' => $this->getUrl($filename),
6567
'title' => $filename,
6668
'original' => $file->getClientOriginalName(),
6769
'type' => $file->getExtension(),
@@ -226,9 +228,9 @@ protected function formatPath($path)
226228
$path = str_replace('{time}', $time, $path);
227229

228230
//替换随机字符串
229-
if (preg_match("/\{rand\:([\d]*)\}/i", $path, $matches)) {
231+
if (preg_match('/\{rand\:([\d]*)\}/i', $path, $matches)) {
230232
$length = min($matches[1], strlen(PHP_INT_MAX));
231-
$path = preg_replace("/\{rand\:[\d]*\}/i", str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT), $path);
233+
$path = preg_replace('/\{rand\:[\d]*\}/i', str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT), $path);
232234
}
233235

234236
return $path;

src/UrlResolverTrait.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)