<?php
namespace App\Http\Traits;
use App\Models\temporaryFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;




trait EditorTrait {
    public function fileUpload(Request $request){
   
        if($request->hasfile('image')){
         
         $diapo = $request->file('image');
         $file = $diapo->getClientOriginalName();
         $folder = uniqid('post', 'true');
         $diapo->storeAs('image/tmp/' . $folder,$file);
           

           TemporaryFile::create([
             'folder' => $folder,
             'file'=> $file           
             ]);
       return  $folder;   
            
           
        }
     return '';

         
}

public function deleteUpload(){

 $tmp_file = TemporaryFile::where('folder',request()->getContent())->first();
 if($tmp_file){
     Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
     $tmp_file->delete();
     return response('');
 }
}
}