Skip to content

Commit

Permalink
Better download links
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1DEA committed Sep 11, 2023
1 parent ea3a11f commit 29e4d72
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
8 changes: 5 additions & 3 deletions app/Http/Controllers/Game/LevelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use App\Models\Game\LevelReplay;
use App\Models\Media;
use App\Models\System\User;
use Hashids\Hashids;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Inertia\Response;
Expand Down Expand Up @@ -109,14 +111,14 @@ public function show($id): Responsable

$level->replays->transform(function (LevelReplay $replay) {
$replay->files->transform(function (Media $media) {
$media->setAttribute('url', Storage::disk('contabo')->temporaryUrl($media->path, now()->addHour()));
$hashids = new Hashids(bin2hex(Crypt::getKey()), 8);
$result = $hashids->encode([$media->id, 0]);
$media->setAttribute('url', route('download', $result));
return $media;
});
return $replay;
});

clock($level);

return page('Levels/Show', [
'level' => $level,
'reviews' => $level->reviews()
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/Game/LevelReplayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use App\Models\Game\LevelReplay;
use App\Models\Media;
use App\Models\System\User;
use Hashids\Hashids;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;

class LevelReplayController extends Controller
Expand Down Expand Up @@ -39,7 +41,9 @@ public function index(Request $request): Responsable
->paginate()
->through(function (LevelReplay $replay) {
$replay->files->transform(function (Media $media) {
$media->setAttribute('url', Storage::disk('contabo')->temporaryUrl($media->path, now()->addHour()));
$hashids = new Hashids(bin2hex(Crypt::getKey()), 8);
$result = $hashids->encode([$media->id, 0]);
$media->setAttribute('url', route('download', $result));
return $media;
});
return $replay;
Expand Down
4 changes: 3 additions & 1 deletion resources/js/Pages/Replays/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import route from "ziggy-js";
import {useDropZone} from "@vueuse/core";
import Username from "@/Components/Username.vue";
import Dropdown from "@/Jetstream/Dropdown.vue";
import Pagination from "@/Components/Pagination.vue";
const props = defineProps({
leaderboard: Object,
Expand Down Expand Up @@ -348,10 +349,11 @@ const search = () => {
<span>{{macro.level.name}} by {{macro.level.creator}}</span>
<span class="text-xs">Recorded by <Username :user="macro.author"/></span>
</div>
<a :href="macro.files[0].url"><span class="text-sm underline text-white">Download</span> {{ macro.format }}</a>
<a :download="macro.files[0].filename" :href="macro.files[0].url"><span class="text-sm underline text-white">Download</span> {{ macro.format }}</a>
</div>
<p v-if="replays.data.length === 0" class="text-center text-ui-600 pane">None :(</p>
</div>
<Pagination :list="replays"/>
</div>
</div>
</div>
Expand Down
23 changes: 20 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

Route::get('/search', [SearchController::class, 'index'])->name('search')->middleware(['auth', 'verified']);

Route::get('/download/{id}', [\App\Http\Controllers\DownloadController::class, '__invoke'])->middleware(['auth']);
Route::get('/download/{id}', [\App\Http\Controllers\DownloadController::class, '__invoke'])->name('download');
Route::get('/upload', function (Request $request) {

})->middleware(['auth']);
Expand Down Expand Up @@ -89,7 +89,11 @@
Route::get('/', [DashboardController::class, 'home'])->name('settings.home');
Route::post('/', DashboardController::class)->name('settings.update');
Route::get('/account', [DashboardController::class, 'account'])->name('settings.account');
Route::get('/profile', [DashboardController::class, 'profile'])->name('settings.profile');

Route::get('/profile', [DashboardController::class, 'profile'])->name('settings.profile')->middleware(['verified']);

Route::get('/connections', [\App\Http\Controllers\Dashboard\DashboardConnectionsController::class, 'show'])->name('settings.connections')->middleware(['verified']);
Route::post('/connections', [\App\Http\Controllers\Dashboard\DashboardConnectionsController::class, 'update'])->middleware(['verified']);
});

Route::get('/replays', [\App\Http\Controllers\Game\LevelReplayController::class, 'index'])->name('replays.index');
Expand All @@ -99,6 +103,9 @@
return page('Groups/Index');
});

Route::get('/profiles', [ProfileController::class, 'index'])->name('users.index');
Route::get('/profile/{profile:name}', [ProfileController::class, 'show'])->name('users.show');

Route::get('/users', [UserController::class, 'index'])->name('users.index');
Route::get('/user/{user:id}', [UserController::class, 'show'])->name('users.show');
Route::get('/user/{id}/names', [NameChangeController::class, 'index'])->name('names.show');
Expand All @@ -107,6 +114,15 @@
Route::get('/user/{id}/reviews', [ReputationLogController::class, 'index'])->name('user.reviews.show');
Route::post('/user/{id}/comments', [ProfileCommentController::class, 'store'])->name('user.comments.store')->middleware(['auth', 'verified', 'throttle:10,10']);

Route::inertia('/tools/inspector', 'Tools/Inspector', [
'__meta_breadcrumbs' => [
[
'text' => 'Save Inspector',
'url' => '/tools/inspector'
]
]
])->name('tools.inspector');

Route::get('/forums', [ForumController::class, 'index'])->name('forums.index');
Route::get('/forum/{forum}', [ForumController::class, 'show'])->name('forums.show');

Expand Down Expand Up @@ -168,8 +184,9 @@
Route::get('/mods', [ModController::class, 'index'])->name('mods.index');
Route::get('/mod/{mod}', [ModController::class, 'show'])->name('mods.show');


Route::get('/styles', [StyleController::class, 'index'])->name('styles.index');
Route::get('/styles/{style:id}', [StyleController::class, 'show'])->name('styles.show');
Route::get('/styles/create', [StyleController::class, 'create'])->name('styles.create');

Route::get('/videos', [VideoController::class, 'index'])->name('videos.index');
Route::post('/videos/create', [VideoController::class, 'store'])->name('videos.store')->middleware(['auth', 'verified']);
Expand Down

0 comments on commit 29e4d72

Please sign in to comment.