Skip to content

Commit

Permalink
feat: add macro processObject
Browse files Browse the repository at this point in the history
  • Loading branch information
alphasnow committed Jun 13, 2024
1 parent c391a80 commit 877feec
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Storage::disk("oss")->appendObject("dir/path/news.txt", "The last line paragraph
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.001", 0);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.002", 1024);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.003", 1024);
Storage::disk("oss")->processObject("dir/path/image.jpg", "image/resize,l_1000");
```
##### 自定义 Macro
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ Storage::disk("oss")->appendObject("dir/path/news.txt", "The last line paragraph
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.001", 0);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.002", 1024);
Storage::disk("oss")->appendFile("dir/path/file.zip", "dir/path/file.zip.003", 1024);
Storage::disk("oss")->processObject("dir/path/image.jpg", "image/resize,l_1000");
```
##### Add Custom Macro
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<testsuites>
<testsuite name="Application Test Suite">
<file>tests/AdapterTest.php</file>
<file>tests/MacroTest.php</file>
<file>tests/ProviderTest.php</file>
<file>tests/ProviderWithExistConfigTest.php</file>
</testsuite>
Expand Down
4 changes: 2 additions & 2 deletions src/AliyunServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public function boot()
$this->app->make("filesystem")
->extend("oss", function (Application $app, array $config) {
$config["url_prefixed"] = version_compare($app->version(), "9.33.0", ">=");
$client = (new AliyunFactory())->createClient($config);
$client = $app->make(AliyunFactory::class)->createClient($config);
$adapter = new AliyunAdapter($client, $config["bucket"], $config["prefix"] ?? "", $config);
$driver = new Filesystem($adapter);
$filesystem = new FilesystemAdapter($driver, $adapter, $config);
(new FilesystemMacroManager($filesystem))->defaultRegister()->register($config["macros"] ?? []);
(new FilesystemMacroManager($app, $filesystem))->defaultRegister()->register($config["macros"] ?? []);
return $filesystem;
});
}
Expand Down
13 changes: 11 additions & 2 deletions src/FilesystemMacroManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AliyunMacro;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendFile;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\AppendObject;
use AlphaSnow\LaravelFilesystem\Aliyun\Macros\ProcessObject;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Contracts\Foundation\Application;

class FilesystemMacroManager
{
/**
* @var Application
*/
protected $app;
/**
* @var FilesystemAdapter
*/
Expand All @@ -21,13 +27,16 @@ class FilesystemMacroManager
protected array $defaultMacros = [
AppendFile::class,
AppendObject::class,
ProcessObject::class,
];

/**
* @param Application $app
* @param FilesystemAdapter $filesystemAdapter
*/
public function __construct(FilesystemAdapter $filesystemAdapter)
public function __construct(Application $app, FilesystemAdapter $filesystemAdapter)
{
$this->app = $app;
$this->filesystemAdapter = $filesystemAdapter;
}

Expand All @@ -47,7 +56,7 @@ public function defaultRegister(): FilesystemMacroManager
public function register(array $macros): FilesystemMacroManager
{
foreach ($macros as $macro) {
$filesystemMacro = new $macro();
$filesystemMacro = $this->app->make($macro);
if (!$filesystemMacro instanceof AliyunMacro) {
throw new AliyunException("FilesystemMacroManager register want AliyunMacro, But got ".$filesystemMacro::class, 0);
}
Expand Down
53 changes: 53 additions & 0 deletions src/Macros/ProcessObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace AlphaSnow\LaravelFilesystem\Aliyun\Macros;

use AlphaSnow\Flysystem\Aliyun\AliyunException;
use AlphaSnow\LaravelFilesystem\Aliyun\OssClientAdapter;
use Closure;
use Illuminate\Filesystem\FilesystemAdapter;
use OSS\Core\OssException;

class ProcessObject implements AliyunMacro
{
/**
* @return string
*/
public function name(): string
{
return "processObject";
}

/**
* @return Closure
*/
public function macro(): Closure
{
$base64UrlEncode = function ($data): string {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
};

return function (string $path, string $process, string $save = "", array $options = []) use ($base64UrlEncode) {
/**
* @var FilesystemAdapter $this
*/
$adapter = new OssClientAdapter($this);

if($save == "") {
$save = $path;
}
$process .= "|sys/saveas,o_".$base64UrlEncode($adapter->path($save)).',b_'.$base64UrlEncode($adapter->bucket());

try {
return $adapter->client()->processObject(
$adapter->bucket(),
$adapter->path($path),
$process,
$adapter->options($options)
);
} catch (OssException $exception) {
throw new AliyunException($exception->getErrorMessage(), 0, $exception);
}
};
}
}
7 changes: 3 additions & 4 deletions src/OssClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use AlphaSnow\Flysystem\Aliyun\AliyunAdapter;
use AlphaSnow\Flysystem\Aliyun\AliyunException;
use Illuminate\Filesystem\FilesystemAdapter;
use JetBrains\PhpStorm\Pure;
use League\Flysystem\Config;
use OSS\OssClient;

Expand All @@ -32,15 +31,15 @@ public function __construct(FilesystemAdapter $filesystemAdapter)
/**
* @return OssClient
*/
#[Pure] public function client(): OssClient
public function client(): OssClient
{
return $this->adapter->getClient();
}

/**
* @return string
*/
#[Pure] public function bucket(): string
public function bucket(): string
{
return $this->adapter->getBucket();
}
Expand All @@ -49,7 +48,7 @@ public function __construct(FilesystemAdapter $filesystemAdapter)
* @param string $path
* @return string
*/
#[Pure] public function path(string $path = ""): string
public function path(string $path = ""): string
{
return $this->adapter->getPrefixer()->prefixPath($path);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,17 @@ public function append_object()
$position = Storage::disk("oss")->appendObject("stubs/file.txt", "contents", 0, ["options" => ["checkmd5" => false]]);
$this->assertSame($position, 8);
}

/**
* @test
*/
public function process_object()
{
$this->ossClient->shouldReceive("processObject")
->with("bucket", "tests/stubs/file.jpg", "image/resize,l_1000|sys/saveas,o_dGVzdHMvc3R1YnMvZmlsZS5qcGc,b_YnVja2V0", [])
->once()
->andReturn('{"bucket":"bucket","fileSize":1024,"object":"tests/stubs/file.jpg","status":"OK"}');
$result = Storage::disk("oss")->processObject("stubs/file.jpg", "image/resize,l_1000");
$this->assertSame($result, '{"bucket":"bucket","fileSize":1024,"object":"tests/stubs/file.jpg","status":"OK"}');
}
}

0 comments on commit 877feec

Please sign in to comment.