Skip to content

Commit 42bf3c6

Browse files
committed
feat(core): 增加文件本地驱动,增加用户测试代码
1 parent 9c7eae8 commit 42bf3c6

20 files changed

+394
-102
lines changed

app/FileStorage/Channels/ChannelInterface.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Http\Request;
88
use Zhiyi\Plus\FileStorage\TaskInterface;
9-
use Zhiyi\Plus\FileStorage\FileMateInterface;
9+
use Zhiyi\Plus\FileStorage\FileMetaInterface;
1010
use Zhiyi\Plus\FileStorage\ResourceInterface;
1111
use Zhiyi\Plus\FileStorage\Filesystems\FilesystemInterface;
1212

@@ -20,5 +20,9 @@ public function setFilesystem(FilesystemInterface $filesystem): void;
2020

2121
public function createTask(): TaskInterface;
2222

23-
public function meta(): FileMateInterface;
23+
public function meta(): FileMetaInterface;
24+
25+
public function url(?string $rule = null): string;
26+
27+
public function callback(): void;
2428
}

app/FileStorage/Channels/PublicChannel.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Zhiyi\Plus\AppInterface;
99
use Zhiyi\Plus\FileStorage\TaskInterface;
1010
use Zhiyi\Plus\FileStorage\Task;
11-
use Zhiyi\Plus\FileStorage\FileMateInterface;
11+
use Zhiyi\Plus\FileStorage\FileMetaInterface;
1212

1313
class PublicChannel extends AbstractChannel
1414
{
@@ -24,8 +24,16 @@ public function createTask(): TaskInterface
2424
return $this->filesystem->createTask($this->request, $this->resource);
2525
}
2626

27-
public function meta(): FileMateInterface
27+
public function meta(): FileMetaInterface
2828
{
29-
return $this->filesystem->mate($this->resource);
29+
return $this->filesystem->meta($this->resource);
3030
}
31+
32+
public function url(?string $rule = null): string
33+
{
34+
return $this->filesystem->url($this->resource->getPath(), $rule);
35+
}
36+
37+
public function callback(): void
38+
{}
3139
}

app/FileStorage/FileMetaInterface.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
namespace Zhiyi\Plus\FileStorage;
66

77
use \Zhiyi\Plus\Models\User;
8+
use Illuminate\Contracts\Support\Arrayable;
89
use Zhiyi\Plus\FileStorage\Pay\PayInterface;
910

10-
interface FileMetaInterface
11+
interface FileMetaInterface extends Arrayable
1112
{
1213
/**
1314
* Has the file is image.
@@ -33,10 +34,25 @@ public function getSize(): int;
3334
*/
3435
public function getMimeType(): string;
3536

37+
/**
38+
* Get the storage vendor name.
39+
* @return string
40+
*/
41+
public function getVendorName(): string;
42+
3643
/**
3744
* Get the resource pay info.
3845
* @param \Zhiyi\Plus\Models\User $user
3946
* @return \Zhiyi\Plus\FileStorage\Pay\PayInterface
4047
*/
4148
public function getPay(User $user): ?PayInterface;
49+
50+
public function url(): string;
51+
52+
/**
53+
* Get the instance as an array.
54+
*
55+
* @return array
56+
*/
57+
public function toArray(): array;
4258
}

app/FileStorage/FilesystemManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function createLocalDriver(): Filesystems\FilesystemInterface
2828
$filesystem = $this
2929
->app
3030
->make(\Illuminate\Contracts\Filesystem\Factory::class)
31-
->disk(setting('core:file-local-select', 'local'));
31+
->disk(setting('core', 'file:local-filesystem-select', 'local'));
3232

3333
return new Filesystems\LocalFilesystem($filesystem);
3434
}

app/FileStorage/Filesystems/FilesystemInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
interface FilesystemInterface
1313
{
14-
public function meta(ResourceInterface $resource): ?FileMetaInterface;
14+
public function meta(ResourceInterface $resource): FileMetaInterface;
1515

1616
public function url(string $path, ?string $rule = null): string;
1717

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zhiyi\Plus\FileStorage\Filesystems\Local;
6+
7+
use Closure;
8+
use Exception;
9+
use Zhiyi\Plus\Models\User;
10+
use Zhiyi\Plus\FileStorage\ImageDimension;
11+
use Zhiyi\Plus\FileStorage\Pay\PayInterface;
12+
use Zhiyi\Plus\FileStorage\ResourceInterface;
13+
use Zhiyi\Plus\FileStorage\FileMetaInterface;
14+
use Zhiyi\Plus\FileStorage\Traits\HasImageTrait;
15+
use Zhiyi\Plus\FileStorage\ImageDimensionInterface;
16+
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
17+
18+
class FileMeta implements FileMetaInterface
19+
{
20+
use HasImageTrait;
21+
22+
protected $filesystem;
23+
protected $resource;
24+
protected $dimension;
25+
26+
public function __construct(FilesystemContract $filesystem, ResourceInterface $resource)
27+
{
28+
$this->filesystem = $filesystem;
29+
$this->resource = $resource;
30+
}
31+
32+
protected function useCustomTypes(): ?Closure
33+
{
34+
return function () {
35+
return [
36+
'image/jpeg',
37+
'image/png',
38+
'image/gif',
39+
'image/bmp',
40+
'image/webp',
41+
];
42+
};
43+
}
44+
45+
/**
46+
* Has the file is image.
47+
* @return bool
48+
*/
49+
public function hasImage(): bool
50+
{
51+
return $this->hasImageType(
52+
$this->getMimeType()
53+
);
54+
}
55+
56+
/**
57+
* Get image file dimension.
58+
* @return \Zhiyi\Plus\FileStorage\ImageDimensionInterface
59+
*/
60+
public function getImageDimension(): ImageDimensionInterface
61+
{
62+
if (! $this->hasImage()) {
63+
throw new Exception('调用的资源并非图片或者是不支持的图片资源');
64+
} elseif ($this->dimension instanceof ImageDimensionInterface) {
65+
return $this->dimension;
66+
};
67+
68+
$realPath = $this->filesystem->path(
69+
$this->resource->getPath()
70+
);
71+
list($width, $height) = getimagesize($realPath);
72+
73+
return $this->dimension = new ImageDimension((float) $width, (float) $height);
74+
}
75+
76+
/**
77+
* Get the file size (Byte).
78+
* @return int
79+
*/
80+
public function getSize(): int
81+
{
82+
return $this->filesystem->getSize(
83+
$this->resource->getPath()
84+
);
85+
}
86+
87+
/**
88+
* Get the resource mime type.
89+
* @return string
90+
*/
91+
public function getMimeType(): string
92+
{
93+
return $this->filesystem->mimeType(
94+
$this->resource->getPath()
95+
);
96+
}
97+
98+
/**
99+
* Get the resource pay info.
100+
* @param \Zhiyi\Plus\Models\User $user
101+
* @return \Zhiyi\Plus\FileStorage\Pay\PayInterface
102+
*/
103+
public function getPay(User $user): ?PayInterface
104+
{
105+
return null;
106+
}
107+
108+
/**
109+
* Get the storage vendor name.
110+
* @return string
111+
*/
112+
public function getVendorName(): string
113+
{
114+
return 'local';
115+
}
116+
117+
public function url(): string
118+
{
119+
return route('storage:local-get', [
120+
'channel' => $this->resource->getChannel(),
121+
'path' => base64_encode($this->resource->getPath()),
122+
]);
123+
}
124+
125+
/**
126+
* Get the instance as an array.
127+
*
128+
* @return array
129+
*/
130+
public function toArray(): array
131+
{
132+
return [1, 2];
133+
}
134+
}

app/FileStorage/Filesystems/LocalFilesystem.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Zhiyi\Plus\FileStorage\FileMetaInterface;
1313
use function Zhiyi\Plus\setting;
1414
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
15+
use Illuminate\Contracts\Auth\Guard;
16+
use Illuminate\Support\Facades\Auth;
1517

1618
class LocalFilesystem implements FilesystemInterface
1719
{
@@ -22,10 +24,9 @@ public function __construct(FilesystemContract $filesystem)
2224
$this->filesystem = $filesystem;
2325
}
2426

25-
public function meta(ResourceInterface $resource): ?FileMetaInterface
27+
public function meta(ResourceInterface $resource): FileMetaInterface
2628
{
27-
$meta = $this->filesystem->getMetadata($resource->getPath());
28-
dd($meta);
29+
return new Local\FileMeta($this->filesystem, $resource);
2930
}
3031

3132
public function url(string $path, ?string $rule = null): string
@@ -47,8 +48,10 @@ public function createTask(Request $request, ResourceInterface $resource): TaskI
4748
'channel' => $resource->getChannel(),
4849
'path' => base64_encode($resource->getPath()),
4950
]);
51+
$user = $this->guard()->user();
5052

51-
return new Task($uri, 'PUT', null, null, [
53+
return new Task($resource, $uri, 'PUT', null, null, [
54+
'Authorization' => 'Bearer '.$this->guard()->login($user),
5255
'x-plus-storage-filename' => $request->input('filename'),
5356
'x-plus-storage-hash' => $request->input('hash'),
5457
'x-plus-storage-size' => $request->input('size'),
@@ -60,4 +63,14 @@ public function put(string $path, $content): bool
6063
{
6164
return (bool) $this->filesystem->put($path, $content);
6265
}
66+
67+
/**
68+
* Get the guard to be used during authentication.
69+
*
70+
* @return \Illuminate\Contracts\Auth\Guard
71+
*/
72+
public function guard(): Guard
73+
{
74+
return Auth::guard('api');
75+
}
6376
}

app/FileStorage/Http/Controllers/CreateTask.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __invoke(Request $request, StorageInterface $storage)
3535
'headers' => $task->getHeaders(),
3636
'form' => $task->getForm(),
3737
'file_key' => $task->getFileKey(),
38+
'node' => $task->getNode(),
3839
], JsonResponse::HTTP_CREATED);
3940
}
4041
}

app/FileStorage/Http/Controllers/Local.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\HttpKernel\Exception\HttpException;
1616
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1717
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
18+
use Illuminate\Contracts\Auth\Guard;
19+
use Illuminate\Support\Facades\Auth;
1820

1921
class Local extends Controller
2022
{
@@ -24,14 +26,29 @@ public function __construct(StorageInterface $storage)
2426
{
2527
$this
2628
->middleware('signed')
29+
->only(['put'/* , 'show' */]);
30+
$this
31+
->middleware('auth:api')
2732
->only(['put']);
2833

2934
$this->storage = $storage;
3035
}
3136

32-
public function show()
37+
public function get(Request $request, string $channel, string $path)
3338
{
34-
// todo.
39+
$resource = new Resource($channel, base64_decode($path));
40+
$rule = $request->query('rule', null);
41+
// $meta = $this->storage->meta($resource);
42+
// dd(
43+
// [
44+
// 'size' => $meta->getSize(),
45+
// 'mime' => $meta->getMimeType(),
46+
// 'dimension' => [
47+
// 'width' => $meta->getImageDimension()->getWidth(),
48+
// 'height' => $meta->getImageDimension()->getHeight(),
49+
// ]
50+
// ]
51+
// );
3552
}
3653

3754
public function put(Request $request, FactoryContract $cache, string $channel, string $path): Response
@@ -52,11 +69,23 @@ public function put(Request $request, FactoryContract $cache, string $channel, s
5269
throw new HttpException('储存文件失败');
5370
}
5471

72+
$this->storage->callback($resource);
5573
$expiresAt = (new Carbon)->addHours(
5674
setting('core', 'file:put-signature-expires-at', 1)
5775
);
5876
$cache->put($signature, 1, $expiresAt);
77+
$this->guard()->invalidate();
5978

6079
return new Response('', Response::HTTP_NO_CONTENT);
6180
}
81+
82+
/**
83+
* Get the guard to be used during authentication.
84+
*
85+
* @return \Illuminate\Contracts\Auth\Guard
86+
*/
87+
public function guard(): Guard
88+
{
89+
return Auth::guard('api');
90+
}
6291
}

app/FileStorage/Http/MakeRoutes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function registerLocalFilesystemRoutes(): void
3636

3737
protected function registerChannelCallbackRoutes(): void
3838
{
39-
$this->router->group(['prefix' => 'api/v2', 'middleware' => ['api']], function (RegistrarContract $router) {
39+
$this->router->group(['prefix' => 'api/v2'], function (RegistrarContract $router) {
4040
$router
4141
->post('storage/{channel}:{path}', Controllers\Callback::class)
4242
->name('storage:callback');
@@ -45,7 +45,7 @@ protected function registerChannelCallbackRoutes(): void
4545

4646
protected function registerCreateTaskRoutes(): void
4747
{
48-
$this->router->group(['prefix' => 'api/v2', 'middleware' => ['api']], function (RegistrarContract $router) {
48+
$this->router->group(['prefix' => 'api/v2'], function (RegistrarContract $router) {
4949
$router
5050
->post('storage', Controllers\CreateTask::class)
5151
->name('storage:create-task');

0 commit comments

Comments
 (0)