diff --git a/app/Http/Controllers/Admin/AdvertisementController.php b/app/Http/Controllers/Admin/AdvertisementController.php index 2c30434..87b7a94 100644 --- a/app/Http/Controllers/Admin/AdvertisementController.php +++ b/app/Http/Controllers/Admin/AdvertisementController.php @@ -76,42 +76,59 @@ class AdvertisementController extends AdminController 'end_at' => 'nullable|date|after_or_equal:start_at', ]); - $user = auth()->user(); - $file = $request->file('file'); - - if ($request->type === 'image') { - $path = $this->storeAsWebp($file, 'ads'); - } else { - $path = $file->store('ads', 'public'); - } + try { + $user = auth()->user(); + $file = $request->file('file'); + + if ($request->type === 'image') { + $path = $this->storeAsWebp($file, 'ads'); + } else { + $path = $file->store('ads', 'public'); + } - if ($user->isSystemAdmin()) { - $companyId = $request->filled('company_id') ? $request->company_id : null; - } else { - $companyId = $user->company_id; - } + if ($user->isSystemAdmin()) { + $companyId = $request->filled('company_id') ? $request->company_id : null; + } else { + $companyId = $user->company_id; + } - $advertisement = Advertisement::create([ - 'company_id' => $companyId, - 'name' => $request->name, - 'type' => $request->type, - 'duration' => (int) $request->duration, - 'url' => Storage::disk('public')->url($path), - 'is_active' => true, - 'start_at' => $request->start_at, - 'end_at' => $request->end_at, - ]); - - if ($request->wantsJson()) { - session()->flash('success', __('Advertisement created successfully.')); - return response()->json([ - 'success' => true, - 'message' => __('Advertisement created successfully.'), - 'data' => $advertisement + $advertisement = Advertisement::create([ + 'company_id' => $companyId, + 'name' => $request->name, + 'type' => $request->type, + 'duration' => (int) $request->duration, + 'url' => Storage::disk('public')->url($path), + 'is_active' => true, + 'start_at' => $request->start_at, + 'end_at' => $request->end_at, ]); - } - return redirect()->back()->with('success', __('Advertisement created successfully.')); + if ($request->wantsJson()) { + return response()->json([ + 'success' => true, + 'message' => __('Advertisement created successfully.'), + 'data' => $advertisement + ]); + } + + return redirect()->back()->with('success', __('Advertisement created successfully.')); + + } catch (\Exception $e) { + \Log::error('Advertisement Upload Error: ' . $e->getMessage(), [ + 'user_id' => auth()->id(), + 'file_name' => $request->file('file')?->getClientOriginalName(), + 'trace' => $e->getTraceAsString() + ]); + + if ($request->wantsJson()) { + return response()->json([ + 'success' => false, + 'message' => __('Failed to process advertisement file: ') . $e->getMessage() + ], 500); + } + + return redirect()->back()->with('error', __('Failed to process advertisement file.')); + } } public function update(Request $request, Advertisement $advertisement) diff --git a/resources/views/admin/ads/index.blade.php b/resources/views/admin/ads/index.blade.php index cd0c993..ebfdbaa 100644 --- a/resources/views/admin/ads/index.blade.php +++ b/resources/views/admin/ads/index.blade.php @@ -1139,30 +1139,39 @@ $baseRoute = 'admin.data-config.advertisements'; }); xhr.addEventListener('load', () => { + let result; try { - const result = JSON.parse(xhr.responseText); - if (xhr.status >= 200 && xhr.status < 300 && result.success) { - // 進度跳到 100% - if (bar) bar.style.width = '100%'; - this.uploadProgress = 100; - this.isAdModalOpen = false; - // 延遲 300ms 讓進度條動畫完成後再重整 - setTimeout(() => { - location.reload(); - }, 300); - } else { - window.showToast?.(result.message || 'Error', 'error'); - this.isSubmitting = false; - this.uploadProgress = 0; - if (bar) { - bar.style.width = '0%'; - bar.style.opacity = '0'; - bar.classList.remove('loading'); - } - } + result = JSON.parse(xhr.responseText); } catch (e) { - console.error('Failed to parse response', e); - window.showToast?.('System Error', 'error'); + console.error('Failed to parse response', e, xhr.responseText); + let errorMsg = 'System Error'; + if (xhr.status === 413) { + errorMsg = '{{ __("File is too large for the server configuration") }} (Nginx 413)'; + } else if (xhr.status >= 500) { + errorMsg = '{{ __("Server Internal Error") }} (500)'; + } + window.showToast?.(errorMsg, 'error'); + this.isSubmitting = false; + this.uploadProgress = 0; + if (bar) { + bar.style.width = '0%'; + bar.style.opacity = '0'; + bar.classList.remove('loading'); + } + return; + } + + if (xhr.status >= 200 && xhr.status < 300 && result.success) { + // 進度跳到 100% + if (bar) bar.style.width = '100%'; + this.uploadProgress = 100; + this.isAdModalOpen = false; + // 延遲 300ms 讓進度條動畫完成後再重整 + setTimeout(() => { + location.reload(); + }, 300); + } else { + window.showToast?.(result.message || 'Error', 'error'); this.isSubmitting = false; this.uploadProgress = 0; if (bar) {