forked from ppy/osu-web
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple file upload in single model
- Loading branch information
Showing
22 changed files
with
416 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class LegacyFilename implements CastsAttributes | ||
{ | ||
public static function makeFromAttributes(?array $attributes): ?string | ||
{ | ||
if (!isset($attributes['hash'])) { | ||
return null; | ||
} | ||
|
||
$filename = $attributes['hash']; | ||
if (isset($attributes['ext'])) { | ||
$filename .= ".{$attributes['ext']}"; | ||
} | ||
|
||
return $filename; | ||
} | ||
|
||
public function get(Model $model, string $key, mixed $value, array $attributes) | ||
{ | ||
return static::makeFromAttributes($attributes); | ||
} | ||
|
||
public function set(Model $model, string $key, mixed $value, array $attributes) | ||
{ | ||
return [ | ||
'ext' => null, | ||
'hash' => $value, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Libraries; | ||
|
||
use App\Exceptions\InvariantException; | ||
use App\Models\Model; | ||
use Illuminate\Http\File; | ||
use League\Flysystem\Local\LocalFilesystemAdapter; | ||
|
||
class Uploader | ||
{ | ||
private static function process(string $name, ?array $options, string $srcPath): ?string | ||
{ | ||
switch ($name) { | ||
case 'image': | ||
$processor = new ImageProcessor($srcPath, $options['maxDimensions'], $options['maxFileSize'] ?? 10_000_000); | ||
$processor->process(); | ||
|
||
return $processor->ext(); | ||
} | ||
|
||
throw new InvariantException('unknown process name'); | ||
} | ||
|
||
private ?string $filename; | ||
|
||
public function __construct( | ||
private string $baseDir, | ||
private Model $model, | ||
private string $attr, | ||
private array $processors = [], | ||
) { | ||
$this->filename = $this->model->{$this->attr}; | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
$this->setFilename(null); | ||
\Storage::deleteDirectory($this->dir()); | ||
} | ||
|
||
public function get(): ?string | ||
{ | ||
$path = $this->path(); | ||
|
||
return $path === null ? null : \Storage::get($path); | ||
} | ||
|
||
public function store(string $srcPath, string $ext = ''): void | ||
{ | ||
foreach ($this->processors as $processName => $processOptions) { | ||
$newExt = static::process($processName, $processOptions, $srcPath); | ||
if ($newExt !== null) { | ||
$ext = $newExt; | ||
} | ||
} | ||
|
||
$this->delete(); | ||
$filename = hash_file('sha256', $srcPath); | ||
if (present($ext)) { | ||
$filename .= ".{$ext}"; | ||
} | ||
$this->setFilename($filename); | ||
$storage = \Storage::disk(); | ||
|
||
if ($storage->getAdapter() instanceof LocalFilesystemAdapter) { | ||
$options = [ | ||
'visibility' => 'public', | ||
'directory_visibility' => 'public', | ||
]; | ||
} | ||
|
||
$storage->putFileAs( | ||
$this->dir(), | ||
new File($srcPath), | ||
$this->filename, | ||
$options ?? [], | ||
); | ||
} | ||
|
||
public function url(): ?string | ||
{ | ||
$path = $this->path(); | ||
|
||
return $path === null ? null : StorageUrl::make(null, $path); | ||
} | ||
|
||
private function dir(): string | ||
{ | ||
return "{$this->baseDir}/{$this->model->getKey()}"; | ||
} | ||
|
||
private function path(): ?string | ||
{ | ||
return $this->filename === null ? null : "{$this->dir()}/{$this->filename}"; | ||
} | ||
|
||
private function setFilename(?string $filename): void | ||
{ | ||
$this->filename = $filename; | ||
$this->model->{$this->attr} = $filename; | ||
} | ||
} |
Oops, something went wrong.