Skip to content

Commit dc4af7c

Browse files
committed
feat: add aliyun macros
1 parent 8e5ef01 commit dc4af7c

File tree

9 files changed

+159
-8
lines changed

9 files changed

+159
-8
lines changed

README-CN.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ Storage::disk('oss')->directories('dir/path');
116116
Storage::disk('oss')->allDirectories('dir/path');
117117
```
118118

119+
#### 使用宏扩展
120+
```php
121+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The first line paragraph.', 0);
122+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The second line paragraph.', 25);
123+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The last line paragraph.', 51);
124+
```
125+
119126
## 文档
120127
- [对象存储 OSS-阿里云](https://help.aliyun.com/product/31815.html)
121128

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ Storage::disk('oss')->directories('dir/path');
116116
Storage::disk('oss')->allDirectories('dir/path');
117117
```
118118
119+
#### Use Macro
120+
```php
121+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The first line paragraph.', 0);
122+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The second line paragraph.', 25);
123+
Storage::disk('oss')->appendObject('dir/path/news.txt', 'The last line paragraph.', 51);
124+
```
125+
119126
## Documentation
120127
- [Object storage OSS-aliyun](https://help.aliyun.com/product/31815.html)
121128

config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
'signature_expires' => env('OSS_SIGNATURE_EXPIRES', 3600),
2323
'internal' => env('OSS_INTERNAL', null), // For example: oss-cn-shanghai-internal.aliyuncs.com
2424
'domain' => env('OSS_DOMAIN', null), // For example: oss.my-domain.com
25+
'macros' => []
2526
];

src/AliyunAdapter.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace AlphaSnow\LaravelFilesystem\Aliyun;
44

55
use AlphaSnow\Flysystem\Aliyun\AliyunAdapter as BaseAdapter;
6+
use AlphaSnow\Flysystem\Aliyun\OssOptions;
67
use League\Flysystem\Config as FlysystemConfig;
8+
use League\Flysystem\PathPrefixer;
79
use OSS\OssClient;
810

911
/**
@@ -29,7 +31,7 @@ public function __construct(OssClient $ossClient, AliyunConfig $aliyunConfig)
2931
/**
3032
* Get the URL for the file at the given path.
3133
*
32-
* @param string $path
34+
* @param string $path
3335
* @return string
3436
*
3537
* @throws \RuntimeException
@@ -43,9 +45,9 @@ public function getUrl(string $path): string
4345
/**
4446
* Get a temporary URL for the file at the given path.
4547
*
46-
* @param string $path
48+
* @param string $path
4749
* @param \DateTimeInterface|null $expiration
48-
* @param array $options
50+
* @param array $options
4951
* @return string
5052
*
5153
* @throws \RuntimeException
@@ -64,4 +66,36 @@ public function getTemporaryUrl(string $path, \DateTimeInterface $expiration = n
6466
$url = $this->client->signUrl($this->bucket, $object, $timeout, OssClient::OSS_HTTP_GET, $options);
6567
return $this->config->correctUrl($url);
6668
}
69+
70+
/**
71+
* @return OssClient
72+
*/
73+
public function getClient(): OssClient
74+
{
75+
return $this->client;
76+
}
77+
78+
/**
79+
* @return OssOptions
80+
*/
81+
public function getOptions(): OssOptions
82+
{
83+
return $this->options;
84+
}
85+
86+
/**
87+
* @return PathPrefixer
88+
*/
89+
public function getPrefixer(): PathPrefixer
90+
{
91+
return $this->prefixer;
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
public function getBucket(): string
98+
{
99+
return $this->bucket;
100+
}
67101
}

src/AliyunConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AliyunConfig extends Collection
1515
public function getDomain()
1616
{
1717
if ($this->get('domain')) {
18-
return $this->getProtocol().'://'.$this->get('domain');
18+
return $this->getProtocol() . '://' . $this->get('domain');
1919
}
2020
return $this->getEndpointDomain();
2121
}
@@ -25,15 +25,15 @@ public function getDomain()
2525
*/
2626
protected function getEndpointDomain()
2727
{
28-
return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('endpoint');
28+
return $this->getProtocol() . '://' . $this->get('bucket') . '.' . $this->get('endpoint');
2929
}
3030

3131
/**
3232
* @return string
3333
*/
3434
protected function getInternalDomain()
3535
{
36-
return $this->getProtocol().'://'.$this->get('bucket').'.'.$this->get('internal');
36+
return $this->getProtocol() . '://' . $this->get('bucket') . '.' . $this->get('internal');
3737
}
3838

3939
/**

src/AliyunServiceProvider.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
namespace AlphaSnow\LaravelFilesystem\Aliyun;
44

5+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
6+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
7+
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
58
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
69
use Illuminate\Filesystem\FilesystemAdapter;
710
use League\Flysystem\Filesystem;
811
use OSS\OssClient;
912

1013
class AliyunServiceProvider extends BaseServiceProvider
1114
{
15+
private $defaultMacros = [
16+
AppendObject::class,
17+
AppendFile::class,
18+
];
19+
1220
public function boot()
1321
{
1422
$this->mergeConfigFrom(
@@ -27,9 +35,17 @@ public function boot()
2735
$aliyunConfig->get("timeout") && $ossClient->setTimeout($config["timeout"]);
2836
$aliyunConfig->get("connect_timeout") && $ossClient->setConnectTimeout($config["connect_timeout"]);
2937

30-
$ossAdapter = new AliyunAdapter($ossClient, $aliyunConfig);
38+
$aliyunAdapter = new AliyunAdapter($ossClient, $aliyunConfig);
39+
$filesystemAdapter = new FilesystemAdapter(new Filesystem($aliyunAdapter), $aliyunAdapter, $config);
3140

32-
return new FilesystemAdapter(new Filesystem($ossAdapter), $ossAdapter, $config);
41+
$macros = array_merge($this->defaultMacros, $aliyunConfig->get('macros', []));
42+
foreach ($macros as $macro) {
43+
$aliyunMacro = $app->make($macro);
44+
if ($aliyunMacro instanceof AliyunMacro) {
45+
$filesystemAdapter::macro($aliyunMacro->name(), $aliyunMacro->macro());
46+
}
47+
}
48+
return $filesystemAdapter;
3349
});
3450
}
3551
}

src/Macros/AliyunMacro.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace AlphaSnow\LaravelFilesystem\Aliyun\Macros;
4+
5+
use Closure;
6+
7+
interface AliyunMacro
8+
{
9+
public function name(): string;
10+
11+
public function macro(): Closure;
12+
}

src/Macros/AppendFile.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace AlphaSnow\LaravelFilesystem\Aliyun\Macros;
4+
5+
use AlphaSnow\Flysystem\Aliyun\AliyunException;
6+
use Illuminate\Filesystem\FilesystemAdapter;
7+
use League\Flysystem\Config;
8+
use OSS\Core\OssException;
9+
use Closure;
10+
11+
class AppendFile implements AliyunMacro
12+
{
13+
public function name(): string
14+
{
15+
return "appendFile";
16+
}
17+
18+
public function macro(): Closure
19+
{
20+
return function (string $path, string $content, int $position = 0, array $options = []) {
21+
try {
22+
/**
23+
* @var FilesystemAdapter $this
24+
*/
25+
return $this->getAdapter()->getClient()->appendFile(
26+
$this->getAdapter()->getBucket(),
27+
$this->getAdapter()->getPrefixer()->prefixPath($path),
28+
$content,
29+
$position,
30+
$this->getAdapter()->getOptions()->mergeConfig(new Config($options))
31+
);
32+
} catch (OssException $exception) {
33+
throw new AliyunException($exception->getErrorMessage(), 0, $exception);
34+
}
35+
};
36+
}
37+
}

src/Macros/AppendObject.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace AlphaSnow\LaravelFilesystem\Aliyun\Macros;
4+
5+
use AlphaSnow\Flysystem\Aliyun\AliyunException;
6+
use Illuminate\Filesystem\FilesystemAdapter;
7+
use League\Flysystem\Config;
8+
use OSS\Core\OssException;
9+
use Closure;
10+
11+
class AppendObject implements AliyunMacro
12+
{
13+
public function name(): string
14+
{
15+
return "appendObject";
16+
}
17+
18+
public function macro(): Closure
19+
{
20+
return function (string $path, string $content, int $position = 0, array $options = []) {
21+
try {
22+
/**
23+
* @var FilesystemAdapter $this
24+
*/
25+
return $this->getAdapter()->getClient()->appendObject(
26+
$this->getAdapter()->getBucket(),
27+
$this->getAdapter()->getPrefixer()->prefixPath($path),
28+
$content,
29+
$position,
30+
$this->getAdapter()->getOptions()->mergeConfig(new Config($options))
31+
);
32+
} catch (OssException $exception) {
33+
throw new AliyunException($exception->getErrorMessage(), 0, $exception);
34+
}
35+
};
36+
}
37+
}

0 commit comments

Comments
 (0)