Skip to content

Commit

Permalink
feat(core): 完成本地直传方案本地驱动开发
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Sep 3, 2018
1 parent 7b2cad2 commit 69410ec
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 33 deletions.
6 changes: 6 additions & 0 deletions app/FileStorage/Channels/AbstractChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Zhiyi\Plus\FileStorage\Channels;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Zhiyi\Plus\FileStorage\ResourceInterface;
use Zhiyi\Plus\FileStorage\Filesystems\FilesystemInterface;

Expand Down Expand Up @@ -49,4 +50,9 @@ public function put($context): bool
{
return $this->filesystem->put($this->resource->getPath(), $context);
}

public function response(?string $rule = null): Response
{
return $this->filesystem->response($this->resource, $rule);
}
}
3 changes: 2 additions & 1 deletion app/FileStorage/Channels/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Zhiyi\Plus\FileStorage\Channels;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Zhiyi\Plus\FileStorage\TaskInterface;
use Zhiyi\Plus\FileStorage\FileMetaInterface;
use Zhiyi\Plus\FileStorage\ResourceInterface;
Expand All @@ -38,7 +39,7 @@ public function createTask(): TaskInterface;

public function meta(): FileMetaInterface;

public function url(?string $rule = null): string;
public function response(?string $rule = null): Response;

public function callback(): void;
}
5 changes: 0 additions & 5 deletions app/FileStorage/Channels/PublicChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public function meta(): FileMetaInterface
return $this->filesystem->meta($this->resource);
}

public function url(?string $rule = null): string
{
return $this->filesystem->url($this->resource->getPath(), $rule);
}

public function callback(): void
{
}
Expand Down
3 changes: 2 additions & 1 deletion app/FileStorage/Filesystems/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Zhiyi\Plus\FileStorage\Filesystems;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Zhiyi\Plus\FileStorage\TaskInterface;
use Zhiyi\Plus\FileStorage\FileMetaInterface;
use Zhiyi\Plus\FileStorage\ResourceInterface;
Expand All @@ -29,7 +30,7 @@ interface FilesystemInterface
{
public function meta(ResourceInterface $resource): FileMetaInterface;

public function url(string $path, ?string $rule = null): string;
public function response(ResourceInterface $resource, ?string $rule = null): Response;

public function delete(string $path): bool;

Expand Down
17 changes: 15 additions & 2 deletions app/FileStorage/Filesystems/Local/FileMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function getVendorName(): string

public function url(): string
{
return route('storage:local-get', [
return route('storage:get', [
'channel' => $this->resource->getChannel(),
'path' => base64_encode($this->resource->getPath()),
]);
Expand All @@ -145,6 +145,19 @@ public function url(): string
*/
public function toArray(): array
{
return [1, 2];
$baseArr = [
'url' => $this->url(),
'vendor' => $this->getVendorName(),
'mime' => $this->getMimeType(),
'size' => $this->getSize(),
];
if ($this->hasImage()) {
$baseArr['dimension'] = [
'width' => $this->getImageDimension()->getWidth(),
'height' => $this->getImageDimension()->getHeight(),
];
}

return $baseArr;
}
}
69 changes: 69 additions & 0 deletions app/FileStorage/Filesystems/Local/RuleParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage\Filesystems\Local;

class RuleParser
{
protected $rule = [];

public function __construct(?string $rule = null)
{
if (! is_null($rule)) {
$this->parse(urldecode($rule));
}
}

protected function parse(?string $rule): void
{
if (! $rule) {
return;
}

$rules = explode(',', $rule);
foreach ($rules as $rule) {
$rule = explode('_', $rule);
$this->rule[$rule[0]] = $rule[1];
}
}

public function getRule(string $key, $default)
{
$value = $this->rule[$key] ?? $default;
if (! $value) {
return $default;
}

return $value;
}

public function getQuality(): int
{
return (int) $this->getRule('q', 90);
}

public function getBlur(): int
{
$blur = (int) $this->getRule('b', 0);
$blur = min(100, $blur);
$blur = max(0, $blur);

return $blur;
}

public function getWidth(): ?float
{
return (float) $this->getRule('w', 0.0);
}

public function getHeight(): ?float
{
return (float) $this->getRule('h', 0.0);
}

public function getFilename(): string
{
return sprintf('w%s-h%s-b%s-q%s', $this->getWidth(), $this->getHeight(), $this->getBlur(), $this->getQuality());
}
}
37 changes: 34 additions & 3 deletions app/FileStorage/Filesystems/LocalFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
use Illuminate\Support\Carbon;
use function Zhiyi\Plus\setting;
use Zhiyi\Plus\FileStorage\Task;
use Intervention\Image\Facades\Image;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Auth;
use Zhiyi\Plus\FileStorage\TaskInterface;
use Zhiyi\Plus\FileStorage\FileMetaInterface;
use Zhiyi\Plus\FileStorage\ResourceInterface;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;

class LocalFilesystem implements FilesystemInterface
{
protected $filesystem;
protected $meta;

public function __construct(FilesystemContract $filesystem)
{
Expand All @@ -42,12 +45,40 @@ public function __construct(FilesystemContract $filesystem)

public function meta(ResourceInterface $resource): FileMetaInterface
{
return new Local\FileMeta($this->filesystem, $resource);
if ($this->meta instanceof FileMetaInterface)
{
return $this->meta;
}

return $this->meta = new Local\FileMeta($this->filesystem, $resource);
}

public function url(string $path, ?string $rule = null): string
public function response(ResourceInterface $resource, ?string $rule = null): Response
{
//
if ($this->meta($resource)->hasImage()) {
$pathinfo = \League\Flysystem\Util::pathinfo($resource->getPath());
$rule = new Local\RuleParser($rule);
$cachePath = sprintf('%s/%s/%s.%s', $pathinfo['dirname'], $pathinfo['filename'], $rule->getFilename(), $pathinfo['extension']);
if ($this->filesystem->has($cachePath)) {
return $this->filesystem->response($cachePath);
}

$realPath = $this->filesystem->path($resource->getPath());
$image = Image::make($realPath);
$image->blur($rule->getBlur());
if (($image->width() > $rule->getWidth() || $image->height() > $rule->getHeight()) && ($rule->getWidth() || $rule->getHeight())) {
$image->resize($rule->getWidth() ?: null, $rule->getHeight() ?: null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
$contents = $image->encode($image->extension, $rule->getQuality());
$this->filesystem->put($cachePath, $contents);

return $image->response();
}

return $this->filesystem->response($resource->getPath());
}

public function delete(string $path): bool
Expand Down
16 changes: 3 additions & 13 deletions app/FileStorage/Http/Controllers/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(StorageInterface $storage)
{
$this
->middleware('signed')
->only(['put'/* , 'show' */]);
->only(['put']);
$this
->middleware('auth:api')
->only(['put']);
Expand All @@ -53,18 +53,8 @@ public function __construct(StorageInterface $storage)
public function get(Request $request, string $channel, string $path)
{
$resource = new Resource($channel, base64_decode($path));
$rule = $request->query('rule', null);
// $meta = $this->storage->meta($resource);
// dd(
// [
// 'size' => $meta->getSize(),
// 'mime' => $meta->getMimeType(),
// 'dimension' => [
// 'width' => $meta->getImageDimension()->getWidth(),
// 'height' => $meta->getImageDimension()->getHeight(),
// ]
// ]
// );

return $this->storage->response($resource, $request->query('rule', null));
}

public function put(Request $request, FactoryContract $cache, string $channel, string $path): Response
Expand Down
2 changes: 1 addition & 1 deletion app/FileStorage/Http/MakeRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function registerLocalFilesystemRoutes(): void
$this->router->group(['prefix' => 'storage'], function (RegistrarContract $router) {
$router
->get('{channel}:{path}', Controllers\Local::class.'@get')
->name('storage:local-get');
->name('storage:get');
$router
->put('{channel}:{path}', Controllers\Local::class.'@put')
->name('storage:local-put');
Expand Down
2 changes: 1 addition & 1 deletion app/FileStorage/ResourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* +----------------------------------------------------------------------+
*/

declare(srrict_types=1);
declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage;

Expand Down
5 changes: 3 additions & 2 deletions app/FileStorage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Illuminate\Http\Request;
use Zhiyi\Plus\AppInterface;
use Illuminate\Support\Carbon;
use Symfony\Component\HttpFoundation\Response;
use Zhiyi\Plus\FileStorage\Channels\ChannelInterface;

class Storage implements StorageInterface
Expand Down Expand Up @@ -80,9 +81,9 @@ public function meta(ResourceInterface $resource): FileMetaInterface
return $this->getChannel($resource)->meta();
}

public function url(ResourceInterface $resource, ?string $rule = null): string
public function response(ResourceInterface $resource, ?string $rule = null): Response
{
return $this->getChannel($resource)->url($rule);
return $this->getChannel($resource)->response($rule);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/FileStorage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* +----------------------------------------------------------------------+
*/

declare(srrict_types=1);
declare(strict_types=1);

namespace Zhiyi\Plus\FileStorage;

use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

interface StorageInterface
{
Expand All @@ -39,12 +40,12 @@ public function createTask(Request $request): TaskInterface;
public function meta(ResourceInterface $resource): FileMetaInterface;

/**
* Get a file URL.
* Get a file response.
* @param \Zhiyi\Plus\FileStorage\ResourceInterface $resource
* @param string|null $rule
* @return string
*/
public function url(ResourceInterface $resource, ?string $rule = null): string;
public function response(ResourceInterface $resource, ?string $rule = null): Response;

/**
* Deelte a resource.
Expand Down
15 changes: 14 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,21 @@ public function getJWTCustomClaims()
return [];
}

public function getAvatarAttribute(?string $resource): FileMetaInterface
public function getAvatarAttribute(?string $resource): ?FileMetaInterface
{
if (! $resource) {
return null;
}

return $this->parseFile($resource);
}

public function getBgAttribute(?string $resource): ?FileMetaInterface
{
if (! $resource) {
return null;
}

return $this->parseFile($resource);
}

Expand Down

0 comments on commit 69410ec

Please sign in to comment.