File "MediaUploadingTrait.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Controllers/Traits/MediaUploadingTrait.php
File size: 2.77 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Http\Controllers\Traits;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

trait MediaUploadingTrait
{
    public function storeMedia(Request $request)
    {
        // Validates file size
        if (request()->has('size')) {
            $this->validate(request(), [
                'file' => 'max:' . request()->input('size') * 1024,
            ]);
        }
        // If width or height is preset - we are validating it as an image
        if (request()->has('width') || request()->has('height')) {
            $this->validate(request(), [
                'file' => sprintf(
                    'image|dimensions:max_width=%s,max_height=%s',
                    request()->input('width', 100000),
                    request()->input('height', 100000)
                ),
            ]);
        }

        $path = storage_path('tmp/uploads');

        try {
            if (!file_exists($path)) {
                mkdir($path, 0755, true);
            }
        } catch (\Exception $e) {
        }

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

        $name = uniqid() . '_' . trim($file->getClientOriginalName());

        $file->move($path, $name);

        return response()->json([
            'name'          => $name,
            'original_name' => $file->getClientOriginalName(),
        ]);
    }

    public function storePDF($file, $path, $disk)
    {

        // Get filename with the extension
        $filenameWithExt = $file->getClientOriginalName();
        //Get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        // Get just ext
        $extension = $file->getClientOriginalExtension();
        // Filename to store
        $fileNameToStore = time().rand(0,9999).'.'.$extension;
        // Upload Image
        if ($disk){
            $path = $file->storeAs($path,$fileNameToStore, $disk);
        } else{
            $path = $file->storeAs($path, $fileNameToStore);
        }
        return $path;
    }

    public function storeImage($file, $path, $disk)
    {

        // Get filename with the extension
        $filenameWithExt = $file->getClientOriginalName();
        //Get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        // Get just ext
        $extension = $file->getClientOriginalExtension();
        // Filename to store
        $fileNameToStore = $filename.'_'.time().rand(0,99).'.'.$extension;
        // Upload Image
        if ($disk){
            $path = $file->storeAs($path,$fileNameToStore, $disk);
        } else{
            $path = $file->storeAs($path,$fileNameToStore);
        }
        return $path;
    }

    public function deletePDF($file,$disk)
    {
        if (Storage::disk($disk)->exists($file)){
            Storage::disk($disk)->delete($file);
        }
    }
}