[FIX] 修復廣告圖片上傳失敗與優化錯誤提示
1. 修正正式環境 Nginx client_max_body_size 限制導致的上傳失敗問題(已於 Host 端同步修正)。 2. 在 AdvertisementController@store 加入 try-catch 區塊與 Log 紀錄,確保圖片處理異常時回傳 JSON 錯誤。 3. 優化前端上傳 AJAX 邏輯,當發生 413 (檔案太大) 或 500 錯誤時提供明確的提示訊息。
This commit is contained in:
parent
12226dd7d0
commit
0685c71141
@ -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)
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user