<?php

namespace App\Traits;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

trait UploadsFile
{
    public function uploadFile(UploadedFile $file, string $folder, ?string $filename = null): string
    {
        $extension = $file->getClientOriginalExtension();
        $name = $filename ?? Str::random(20).'.'.$extension;
        $path = $folder.'/'.$name;

        Storage::disk('s3')->put($path, file_get_contents($file));

        return $path;
    }
}
