1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api\V1; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
use App\Models\Post; |
8
|
|
|
use App\Models\Author; |
9
|
|
|
use App\Models\Image; |
10
|
|
|
|
11
|
|
|
use Dingo\Api\Routing\Helpers; |
12
|
|
|
|
13
|
|
|
use Validator; |
14
|
|
|
use Illuminate\Support\Facades\Storage; |
15
|
|
|
|
16
|
|
|
class ImagesController extends BaseController |
17
|
|
|
{ |
18
|
|
|
protected $model = \App\Models\Image::class; |
19
|
|
|
|
20
|
|
|
// @todo replace w/ cloud path (url shortener) |
21
|
|
|
protected $urlRootPath = '/api/images'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Download an image by slug. |
25
|
|
|
* |
26
|
|
|
* @return \Illuminate\Http\Response |
27
|
|
|
*/ |
28
|
|
|
public function downloadBySlug($slug) |
29
|
|
|
{ |
30
|
|
|
$image = Image::where('slug', $slug)->firstOrFail(); |
31
|
|
|
|
32
|
|
|
return Storage::disk('local')->download($image->filename); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get an image by slug. |
37
|
|
|
* |
38
|
|
|
* @return \Illuminate\Http\Response |
39
|
|
|
*/ |
40
|
|
|
public function getBySlug($slug) |
41
|
|
|
{ |
42
|
|
|
$image = Image::where('slug', $slug)->firstOrFail(); |
43
|
|
|
|
44
|
|
|
return $this->response->array([ |
45
|
|
|
'status' => 'success', |
46
|
|
|
'message' => '', |
47
|
|
|
'data' => [ |
48
|
|
|
'image' => $image, |
49
|
|
|
], |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Store a new image. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Http\Response |
62
|
|
|
*/ |
63
|
|
|
public function store() |
64
|
|
|
{ |
65
|
|
|
$validator = $this->validator($this->requests); |
66
|
|
|
if ($validator->fails()) { |
67
|
|
|
throw new \Dingo\Api\Exception\StoreResourceFailedException( |
68
|
|
|
'Could not create new resource.', |
69
|
|
|
$validator->errors() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (! $this->requests->hasFile('file')) { |
74
|
|
|
return $this->response->array([ |
75
|
|
|
'status' => 'error', |
76
|
|
|
'message' => 'Invalid request', |
77
|
|
|
'errors' => ['Invalid file.',], |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (! $this->requests->file('file')->isValid()) { |
82
|
|
|
return $this->response->array([ |
83
|
|
|
'status' => 'error', |
84
|
|
|
'message' => 'Invalid request', |
85
|
|
|
'errors' => ['Invalid file upload.',], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$slug = uniqid().'_'.str_slug(explode('.', $this->requests->file->getClientOriginalName())[0]); |
90
|
|
|
$filename = $slug.'.'.$this->requests->file->extension(); |
91
|
|
|
|
92
|
|
|
// move image |
93
|
|
|
$this->requests->file->move( |
94
|
|
|
storage_path('app'), |
95
|
|
|
$filename |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$image = Image::create([ |
99
|
|
|
'slug' => $slug, |
100
|
|
|
'url' => $this->urlRootPath.'/'.$slug, |
101
|
|
|
'filename' => $filename, |
102
|
|
|
'post_id' => $this->requests->post_id, |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
return $this->response->array([ |
106
|
|
|
'status' => 'success', |
107
|
|
|
'message' => 'Image saved.', |
108
|
|
|
'data' => [ |
109
|
|
|
'image' => $image, |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get store validator. |
119
|
|
|
* |
120
|
|
|
* @param \Illuminate\Http\Request $request |
121
|
|
|
* @return \Illuminate\Validation\Validator |
122
|
|
|
*/ |
123
|
|
|
protected function validator(Request $request) |
124
|
|
|
{ |
125
|
|
|
return Validator::make($request->all(), [ |
126
|
|
|
'file' => 'required|file', |
127
|
|
|
'post_id' => 'required|exists:posts,id', |
128
|
|
|
// |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|