<?php

namespace App\Services;

use App\Models\File;
use Illuminate\Support\Facades\Storage;

class FileService
{
    /**
     * @param $path
     * @param $file
     * @return File
     */
    public function saveUploadedFile($path, $file): File
    {
        Storage::disk('public')->put($path . '/' .$file->getClientOriginalName(), $file->getContent());
        return File::query()->updateOrCreate([
            'link' => url('/storage/') . '/' . $path . '/' .$file->getClientOriginalName(),
            'path' => $path . '/' .$file->getClientOriginalName(),
            'user_id' => auth()->user()->id,
            'original_name' => $file->getClientOriginalName(),
            'mime_type' => $file->getClientMimeType(),
        ]);
    }
}