File size: 2,855 Bytes
40dca3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php


namespace App\Services;


use App\Models\ContentImage as ContentImageModel;
use Google_Service_Drive_Permission;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class ContentImageServices extends BaseServices
{
    private  $url_update_link = "https://api-update-img-render.onrender.com/save-content-image";

    public function __construct(ContentImageModel $model)
    {
        parent::__construct($model);
    }

    public function index($request)
    {
        $limit = $request->get('limit', ContentImageModel::LIMIT_PAGE);
        $query = $this->model;
        return $query->paginate($limit);
    }

    public function findByChapterId($request, $id)
    {
        $limit = $request->get('limit', 1);
        $query = ContentImageModel::query();
        $query = $query->where('chapter_id', $id)->orderBy('id', 'asc');
        return $query->paginate($limit);
    }

    public function show($chapterNumber)
    {

    }

    public function save(array $attributes)
    {
        $entity = null;
        if (!empty($attributes['id'])) {
            $entity = $this->model->where('id', $attributes['id'])->first();
            if ($entity) {
                $entity->fill($attributes)->save();
            }
        } else {
            $entity = $this->model->create($attributes);
        }
        if($entity){
            $result['id'] = $entity->id;
            $result['link_img'] = $this->getGGId($entity->link_img);
            $json = json_encode($result);
            Http::withBody($json, 'application/json')
                ->post($this->url_update_link);
        }
        return $entity;
    }

    public function uploadGGDrive($request, &$comic)
    {
        $file = [];
        $file['link_img']['file'] = $request->file('link_img');

        $driveService = Storage::disk('google');

        $newPermission = app()->make('googlePermission');

        $folderId = app()->make('googleFolderId');

        $file['link_img']['url'] = [];

        if (isset($file['link_img']['file'])) {
            foreach ($file['link_img']['file'] as $itemImg) {
                $fileToUpload = $this->postGGDrive($driveService, $itemImg, $folderId);
                if ($fileToUpload) {
                    $driveService->permissions->create($fileToUpload->id, $newPermission);
                    $file['link_img']['url'][] = 'https://lh3.googleusercontent.com/d/' . $fileToUpload->id . '=w1000';
                }

            }
        }


        return $file;
    }

    public function delete($id)
    {
        $entity = $this->model
            ->where('id', $id)->first();
        // xoa img tren ggdrive
        $result = collect($entity)->only(['link_img','link_img_backup']);
        $this->deteleGGDrive($result->toArray());
        return !empty($entity) ? $entity->delete() : null;
    }


}