Skip to content

Commit

Permalink
Support multiple file upload in single model
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 2, 2023
1 parent 848f059 commit 0bbbef1
Show file tree
Hide file tree
Showing 24 changed files with 426 additions and 473 deletions.
31 changes: 31 additions & 0 deletions app/Casts/LegacyFileJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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 LegacyFileJson implements CastsAttributes
{
public function get(Model $model, string $key, mixed $value, array $attributes)
{
return isset($attributes['ext'], $attributes['hash'])
? [
'ext' => $attributes['ext'],
'hash' => $attributes['hash'],
] : null;
}

public function set(Model $model, string $key, mixed $value, array $attributes)
{
return [
'ext' => $value['ext'] ?? null,
'hash' => $value['hash'] ?? null,
];
}
}
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 @@ -90,9 +90,9 @@ public function cover()
}

try {
$user
->profileCustomization()
->setCover(Request::input('cover_id'), Request::file('cover_file'));
$profile = $user->profileCustomization();
$profile->setCover(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.

103 changes: 0 additions & 103 deletions app/Libraries/ProfileCover.php

This file was deleted.

19 changes: 19 additions & 0 deletions app/Libraries/StorageUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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;

class StorageUrl
{
public static function make(?string $diskName, string $path): string
{
$diskName ??= config('filesystems.default');
$baseUrl = config("filesystems.disks.{$diskName}.base_url");

return "{$baseUrl}/{$path}";
}
}
36 changes: 0 additions & 36 deletions app/Libraries/StorageWithUrl.php

This file was deleted.

Loading

0 comments on commit 0bbbef1

Please sign in to comment.