Skip to content

Commit

Permalink
Merge pull request #11686 from nanaya/proxy-media-controller
Browse files Browse the repository at this point in the history
Clean up proxy media controller
  • Loading branch information
notbakaneko authored Nov 28, 2024
2 parents 10c5346 + eaf1709 commit b24f634
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
12 changes: 0 additions & 12 deletions app/Http/Controllers/BeatmapDiscussionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,6 @@ public function index()
return ext_view('beatmap_discussions.index', compact('json', 'search', 'paginator'));
}

public function mediaUrl()
{
$url = presence(get_string(request('url')));

if (!isset($url)) {
return response('Missing url parameter', 422);
}

// Tell browser not to request url for a while.
return redirect(proxy_media($url))->header('Cache-Control', 'max-age=600');
}

public function restore($id)
{
$discussion = BeatmapDiscussion::whereNotNull('deleted_at')->findOrFail($id);
Expand Down
36 changes: 36 additions & 0 deletions app/Http/Controllers/ProxyMediaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Http\Controllers;

class ProxyMediaController extends Controller
{
private static function fromNonBrowser(): bool
{
$headers = \Request::instance()->headers;

return $headers->get('origin') === null
&& $headers->get('referer') === null
&& $headers->get('sec-fetch-site') === null;
}

public function __invoke()
{
if (!static::fromNonBrowser() && !from_app_url()) {
return response('Forbidden', 403);
}

$url = presence(get_string(\Request::input('url')));

if (!isset($url)) {
return response('Missing url parameter', 422);
}

// Tell browser to cache redirect url for a while.
return redirect(proxy_media($url))->header('Cache-Control', 'max-age=86400');
}
}
2 changes: 1 addition & 1 deletion resources/js/beatmap-discussions/image-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class ImageLink extends React.Component<Props> {
render() {
if (this.props.src == null) return null;

const src = route('beatmapsets.discussions.media-url', { url: this.props.src });
const src = route('media-url', { url: this.props.src });
const content = (
<>
{!this.loaded && this.renderSpinner()}
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Http\Middleware\ThrottleRequests;

Route::get('wiki/images/{path}', 'WikiController@image')->name('wiki.image')->where('path', '.+');
Route::get('beatmapsets/discussions/media-url', 'BeatmapDiscussionsController@mediaUrl')->name('beatmapsets.discussions.media-url');
Route::get('media-url', 'ProxyMediaController')->name('media-url');

Route::group(['middleware' => ['web']], function () {
Route::group(['as' => 'admin.', 'prefix' => 'admin', 'namespace' => 'Admin'], function () {
Expand Down

0 comments on commit b24f634

Please sign in to comment.