1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlphaSnow\AliyunOss; |
4
|
|
|
|
5
|
|
|
use Aliyun\Flysystem\AliyunOss\AliyunOssAdapter as BaseAdapter; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use League\Flysystem\Adapter\CanOverwriteFiles; |
8
|
|
|
use League\Flysystem\AdapterInterface; |
9
|
|
|
use League\Flysystem\Config; |
10
|
|
|
use OSS\OssClient; |
11
|
|
|
|
12
|
|
|
class AliyunOssAdapter extends BaseAdapter implements CanOverwriteFiles |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AliyunOssConfig |
16
|
|
|
*/ |
17
|
|
|
protected $ossConfig; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param OssClient $ossClient |
21
|
|
|
* @param AliyunOssConfig $ossConfig |
22
|
|
|
*/ |
23
|
1 |
|
public function __construct(OssClient $ossClient, AliyunOssConfig $ossConfig) |
24
|
|
|
{ |
25
|
1 |
|
$this->ossConfig = $ossConfig; |
26
|
1 |
|
parent::__construct($ossClient, $ossConfig->get('bucket'), ltrim($ossConfig->get('prefix', null), '/'), $ossConfig->get('options', [])); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
6 |
|
protected function getOptionsFromConfig(Config $config) |
33
|
|
|
{ |
34
|
6 |
|
$options = parent::getOptionsFromConfig($config); |
35
|
|
|
|
36
|
6 |
|
if ($visibility = $config->get('visibility')) { |
37
|
|
|
// Object ACL > Bucket ACL |
38
|
1 |
|
$options[OssClient::OSS_HEADERS][OssClient::OSS_OBJECT_ACL] = $visibility === AdapterInterface::VISIBILITY_PUBLIC ? OssClient::OSS_ACL_TYPE_PUBLIC_READ : OssClient::OSS_ACL_TYPE_PRIVATE; |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
return $options; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Used by \Illuminate\Filesystem\FilesystemAdapter::url |
46
|
|
|
* Get the URL for the file at the given path. |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
3 |
|
public function getUrl($path) |
52
|
|
|
{ |
53
|
3 |
|
$object = $this->applyPathPrefix($path); |
54
|
3 |
|
return $this->ossConfig->getUrlDomain() . '/' . ltrim($object, '/'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Used by \Illuminate\Filesystem\FilesystemAdapter::temporaryUrl |
59
|
|
|
* Get a temporary URL for the file at the given path. |
60
|
|
|
* |
61
|
|
|
* @param string $path |
62
|
|
|
* @param \DateTimeInterface|null $expiration |
63
|
|
|
* @param array $options |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
2 |
|
public function getTemporaryUrl($path, $expiration = null, array $options = []) |
69
|
|
|
{ |
70
|
2 |
|
$object = $this->applyPathPrefix($path); |
71
|
2 |
|
$clientOptions = $this->getOptionsFromConfig(new Config($options)); |
72
|
2 |
|
if (is_null($expiration)) { |
73
|
|
|
$expiration = Carbon::parse($this->ossConfig->get('signature_expires')); |
74
|
|
|
} |
75
|
2 |
|
$timeout = $expiration->getTimestamp() - (new \DateTime('now'))->getTimestamp(); |
76
|
|
|
|
77
|
2 |
|
$url = $this->client->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $clientOptions); |
78
|
|
|
|
79
|
2 |
|
return $this->ossConfig->correctUrl($url); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|