<?php

use Illuminate\Support\Facades\Route;
use ErlandMuchasaj\LaravelFileUploader\FileUploader;
use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/files', function (Request $request) {
    return view('files');
})->name('files');


Route::post('/files', function (Request $request) {

    $max_size = (int) ini_get('upload_max_filesize') * 1000;

    $extensions = implode(',', FileUploader::images());

    $request->validate([
        'file' => [
            'required',
            'file',
            'image',
            'mimes:' . $extensions,
            'max:'.$max_size,
        ]
    ]);

    $file = $request->file('file');

    $response = FileUploader::store($file);

    return redirect()
        ->back()
        ->with('success','File has been uploaded.')
        ->with('file', $response);
})->name('files.store');
