StorageRepository.php 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?php

namespace App\Repositories;

use Illuminate\Support\Str;
use Storage;

abstract class StorageRepository
{
    protected $model;

    abstract public function get();

    /**
     * Display specified resource.
     *
     * @param  varchar  $with
     * @param  uuid  $id
     * @return \Illuminate\Http\Response
     */
    public function findId($id = null, $with = null)
    {
        return $this->model
            ->when($with, function ($query) use ($with) {
                return $query->with($with);
            })
            ->when($id, function ($query) use ($id) {
                return $query->where('id', $id);
            })
            ->first();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
39
    public function store($request, $tipe, $is_active = 'on')
40 41 42 43 44 45 46 47 48
    {
        $setting = webprofilesetting();

        $data = $request->except('_token');
        $data['id'] = Str::uuid()->toString();

        if ($request->hasFile($tipe)) {
            $cover = $request->file($tipe);
            $extension = $cover->guessClientExtension();
49
            $data['slug'] = str_slug($data['title']) . '.' . $extension;
50 51 52 53 54 55 56 57 58
            $filename = $data['id'] . '.' . $extension;
            if ($setting['external_storage'] == 1) {
                Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            } else {
                Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            }
            $data[$tipe] = $filename;
        }

59 60 61 62 63
        if ($is_active == 'on') {
            array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;
        }
        $data['userid_created'] = auth()->user()->id;
        
64 65 66 67 68 69 70 71 72 73
        return $this->model->create($data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
74
    public function update($request, $model, $tipe, $is_active = 'on')
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    {
        $setting = webprofilesetting();

        $data = $request->except('_token');

        if ($request->hasFile($tipe)) {
            $cover = $request->file($tipe);
            $extension = $cover->guessClientExtension();
            $filename = $model->id . '.' . $extension;

            if ($setting['external_storage'] == 1) {
                Storage::disk('storage')->put($setting['directory'] . '/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            } else {
                Storage::disk('local')->put('public/' . $tipe . '/' . $filename, file_get_contents($cover->getRealPath()));
            }

            $data[$tipe] = $filename;
        }

94 95 96
        if ($is_active == 'on') {
            array_key_exists('is_active', $data) ? $data['is_active'] = 1 : $data['is_active'] = 0;
        }
97
        $data['userid_updated'] = auth()->user()->id;
98
        
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        return $model->update($data);
    }

    /**
     * Show the specified resource in storage.
     *
     * @param  uuid  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return $this->model->where('user_id', $id)->first();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  Model  $model
     * @return \Illuminate\Http\Response
     */
    public function destroy($model, $tipe)
    {
        $setting = webprofilesetting();

123
        if ($model->$tipe) {
124
            if ($setting['external_storage'] == 1) {
125
                Storage::disk('storage')->delete($setting['directory'] . '/' . $tipe . '/' . $model->$tipe);
126
            } else {
127
                Storage::disk('local')->delete('public/' . $tipe . '/' . $model->$tipe);
128 129 130 131 132 133
            }
        }

        $model->delete($model->id);
    }
}