Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple file upload in single model #10703

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/Casts/LegacyFilename.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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,
];
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/ForumTopicCoversCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle()

$deleted++;
$progress->advance();
$cover->deleteWithFile();
$cover->delete();
}
});

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public function cover()
}

try {
$user
->profileCustomization()
->setCover(Request::input('cover_id'), Request::file('cover_file'));
$profile = $user->profileCustomization();
$profile->cover()->set(Request::input('cover_id'), Request::file('cover_file'));
$profile->save();
} catch (ImageProcessorException $e) {
return error_popup($e->getMessage());
}
Expand Down
6 changes: 1 addition & 5 deletions app/Http/Controllers/Admin/ContestsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Models\Contest;
use App\Models\DeletedUser;
use App\Models\UserContestEntry;
use GuzzleHttp;
use ZipStream\ZipStream;

class ContestsController extends Controller
Expand Down Expand Up @@ -47,14 +46,11 @@ public function gimmeZip($id)
return response()->streamDownload(function () use ($entries) {
$zip = new ZipStream();

$client = new GuzzleHttp\Client();

$deletedUser = new DeletedUser();
foreach ($entries as $entry) {
$targetDir = ($entry->user ?? $deletedUser)->username." ({$entry->user_id})";
$filename = sanitize_filename($entry->original_filename);
$file = $client->get($entry->fileUrl())->getBody();
$zip->addFileFromPsr7Stream("$targetDir/{$filename}", $file);
$zip->addFile("$targetDir/{$filename}", $entry->file()->get());
}

$zip->finish();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ContestEntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function destroy($id)

priv_check('ContestEntryDestroy', $entry)->ensureCan();

$entry->deleteWithFile();
$entry->delete();

return $contest->userEntries($user);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Forum/TopicCoversController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function destroy($id)
if ($cover !== null) {
priv_check('ForumTopicCoverEdit', $cover)->ensureCan();

$cover->deleteWithFile();
$cover->delete();
}

return json_item($cover, new TopicCoverTransformer());
Expand Down
35 changes: 0 additions & 35 deletions app/Libraries/ForumDefaultTopicCover.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Libraries/Opengraph/Forum/ForumOpengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function get(): array
return [
'description' => $this->description(),
'title' => $this->forum->forum_name,
'image' => $this->forum->cover?->fileUrl(),
'image' => $this->forum->cover?->file()->url(),
];
}
}
2 changes: 1 addition & 1 deletion app/Libraries/Opengraph/Forum/TopicOpengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function get(): array

return [
'description' => "{$forumDescription} » {$this->topic->topic_title}",
'image' => $this->topic->cover?->fileUrl() ?? $this->topic->forum->cover?->defaultTopicCover->fileUrl(),
'image' => $this->topic->cover?->file()->url() ?? $this->topic->forum->cover?->defaultTopicCover->url(),
'title' => $this->topic->topic_title,
];
}
Expand Down
103 changes: 0 additions & 103 deletions app/Libraries/ProfileCover.php

This file was deleted.

114 changes: 114 additions & 0 deletions app/Libraries/Uploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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 const DEFAULT_MAX_FILESIZE = 10_000_000;

private ?string $filename;

public function __construct(
private string $baseDir,
public Model $model,
private string $attr,
private array $processors = [],
) {
$this->filename = $this->model->{$this->attr};
}

private static function process(string $name, ?array $options, string $srcPath): ?string
{
switch ($name) {
case 'image':
$processor = new ImageProcessor(
$srcPath,
$options['maxDimensions'],
$options['maxFilesize'] ?? static::DEFAULT_MAX_FILESIZE,
);
$processor->process();

return $processor->ext();
}

throw new InvariantException('unknown process name');
}

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;
}
}
Loading