Skip to content

Commit

Permalink
fix: same for API events controller
Browse files Browse the repository at this point in the history
  • Loading branch information
blt950 committed Aug 2, 2024
1 parent 9025ce3 commit 302e7e2
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions app/Http/Controllers/API/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,26 @@ public function store(Request $request)

$this->authorize('create', Event::class);

$imageURL = null;
$imageName = null;

if ($request->hasFile('image')) {
$image = $request->file('image');
$imageName = time() . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
$imagePath = $image->getPathName();
$imageName = now()->format('Y-m-d') . '-' . uniqid() . '.' . $image->getClientOriginalExtension();

// Get image dimensions
list($width, $height) = getimagesize($imagePath);
if ($width / $height != 16 / 9) {
list($width, $height) = getimagesize($image->getPathName());
if (round($width / $height, 2) != round(16 / 9, 2)) {
return back()->withErrors(['image' => 'Image must be in 16:9 aspect ratio.'])->withInput();
}

// Store the image
$image->storeAs('images', $imageName, 'public');
$storedPath = $image->storeAs('banners', $imageName, 'public');

// Check if the image was successfully uploaded
if (!Storage::disk('public')->exists('images/' . $imageName)) {
if (!Storage::disk('public')->exists($storedPath)) {
return back()->withErrors(['image' => 'Failed to upload the image.'])->withInput();
}

$imageURL = asset('storage/images/' . $imageName);

}

$event = Event::create([
Expand All @@ -82,7 +80,7 @@ public function store(Request $request)
'recurrence_interval' => $request->input('event_type') == '0' ? null : $request->input('recurrence_interval'),
'recurrence_unit' => $request->input('event_type') == '0' ? null : $request->input('recurrence_unit'),
'recurrence_end_date' => $request->input('event_type') == '0' ? null : $request->input('recurrence_end_date'),
'image' => $imageURL,
'image' => $imageName,
]);

// Ensure area and user association
Expand Down

0 comments on commit 302e7e2

Please sign in to comment.