<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */

Route::get('healthz', function () {
    return response('', 200)
        ->header('Content-Type', 'text/plain');
});

Route::group(['prefix' => 'download'], function () {
    Route::get('iframe-breakout', 'DownloadController@iframeBeakout')->name('download-iframe-breakout');
    Route::get('/', 'DownloadController@download')->name("download");
});

Route::get('/', function () {
    if (env("APP_ENV", "") !== "production") {
        return view("development");
    } else {
        return redirect("https://metager.de");
    }
});

Route::post('/', function (Request $request) {
    $validatedData = $request->validate([
        'url' => 'required|url|max:255',
    ]);
    $url = $request->input('url', 'https://metager.de');
    $password = md5(env('PROXY_PASSWORD') . $url);
    $url = base64_encode(str_rot13($url));
    $target = urlencode(str_replace("/", "<<SLASH>>", $url));
    return redirect(action('ProxyController@proxyPage', ['password' => $password, 'url' => $target]));
});

Route::get('{password}/{url}', 'ProxyController@proxyPage')->middleware('throttle:60:1')->middleware('checkpw');
# Route that triggers streaming of Download
Route::get('proxy/{password}/{id}/{url}', 'ProxyController@proxy')->middleware('checkpw:true');
Route::post('proxy/{password}/{id}/{url}', 'ProxyController@streamFile')->middleware('checkpw:true');


Route::post('/{url}', function ($url) {
    abort(405);
});