fix handle upload file on page precheck and postcheck
This commit is contained in:
@@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Postcheck;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PostchecksController extends Controller
|
||||
{
|
||||
@@ -32,41 +34,155 @@ class PostchecksController extends Controller
|
||||
'pressure_high' => 'required|numeric|min:0',
|
||||
'pressure_low' => 'nullable|numeric|min:0',
|
||||
'cabin_temperature' => 'nullable|numeric',
|
||||
'cabin_temperature_image' => 'nullable|string',
|
||||
'cabin_temperature_image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
'ac_condition' => 'nullable|in:' . implode(',', Postcheck::getAcConditionOptions()),
|
||||
'ac_image' => 'nullable|string',
|
||||
'ac_image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
'blower_condition' => 'nullable|in:' . implode(',', Postcheck::getBlowerConditionOptions()),
|
||||
'blower_image' => 'nullable|string',
|
||||
'blower_image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
'evaporator_condition' => 'nullable|in:' . implode(',', Postcheck::getEvaporatorConditionOptions()),
|
||||
'evaporator_image' => 'nullable|string',
|
||||
'evaporator_image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
||||
'compressor_condition' => 'nullable|in:' . implode(',', Postcheck::getCompressorConditionOptions()),
|
||||
'postcheck_notes' => 'nullable|string',
|
||||
'front_image' => 'required|string',
|
||||
'front_image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
||||
]);
|
||||
|
||||
// Pastikan transaction_id sama dengan $transaction->id
|
||||
$postcheck = Postcheck::create([
|
||||
$data = [
|
||||
'transaction_id' => $transaction->id,
|
||||
'postcheck_by' => auth()->id(),
|
||||
'postcheck_at' => now(),
|
||||
'police_number' => $transaction->police_number,
|
||||
'spk_number' => $transaction->spk,
|
||||
'front_image' => $request->front_image,
|
||||
'kilometer' => $request->kilometer,
|
||||
'pressure_high' => $request->pressure_high,
|
||||
'pressure_low' => $request->pressure_low,
|
||||
'cabin_temperature' => $request->cabin_temperature,
|
||||
'cabin_temperature_image' => $request->cabin_temperature_image,
|
||||
'ac_condition' => $request->ac_condition,
|
||||
'ac_image' => $request->ac_image,
|
||||
'blower_condition' => $request->blower_condition,
|
||||
'blower_image' => $request->blower_image,
|
||||
'evaporator_condition' => $request->evaporator_condition,
|
||||
'evaporator_image' => $request->evaporator_image,
|
||||
'compressor_condition' => $request->compressor_condition,
|
||||
'postcheck_notes' => $request->postcheck_notes,
|
||||
]);
|
||||
];
|
||||
|
||||
return redirect()->route('transaction')->with('success', 'Postcheck berhasil disimpan');
|
||||
// Handle file uploads
|
||||
$imageFields = [
|
||||
'front_image', 'cabin_temperature_image', 'ac_image',
|
||||
'blower_image', 'evaporator_image'
|
||||
];
|
||||
|
||||
foreach ($imageFields as $field) {
|
||||
if ($request->hasFile($field) && $request->file($field)->isValid()) {
|
||||
try {
|
||||
$file = $request->file($field);
|
||||
|
||||
// Generate unique filename with transaction ID
|
||||
$filename = time() . '_' . uniqid() . '_' . $transaction->id . '_' . $field . '.' . $file->getClientOriginalExtension();
|
||||
|
||||
// Create directory path: transactions/{transaction_id}/postcheck/
|
||||
$directory = 'transactions/' . $transaction->id . '/postcheck';
|
||||
|
||||
// Ensure base storage directory exists
|
||||
$this->ensureStorageDirectoryExists();
|
||||
|
||||
// Ensure transactions directory exists
|
||||
if (!Storage::disk('public')->exists('transactions')) {
|
||||
Storage::disk('public')->makeDirectory('transactions', 0755, true);
|
||||
Log::info('Created transactions directory');
|
||||
}
|
||||
|
||||
// Ensure transaction ID directory exists
|
||||
$transactionDir = 'transactions/' . $transaction->id;
|
||||
if (!Storage::disk('public')->exists($transactionDir)) {
|
||||
Storage::disk('public')->makeDirectory($transactionDir, 0755, true);
|
||||
Log::info('Created transaction directory: ' . $transactionDir);
|
||||
}
|
||||
|
||||
// Ensure postcheck directory exists
|
||||
if (!Storage::disk('public')->exists($directory)) {
|
||||
Storage::disk('public')->makeDirectory($directory, 0755, true);
|
||||
Log::info('Created postcheck directory: ' . $directory);
|
||||
}
|
||||
|
||||
// Store file in organized directory structure
|
||||
$path = $file->storeAs($directory, $filename, 'public');
|
||||
|
||||
// Store file path
|
||||
$data[$field] = $path;
|
||||
|
||||
// Store metadata
|
||||
$data[$field . '_metadata'] = [
|
||||
'original_name' => $file->getClientOriginalName(),
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'uploaded_at' => now()->toISOString(),
|
||||
'transaction_id' => $transaction->id,
|
||||
'filename' => $filename,
|
||||
];
|
||||
|
||||
Log::info('File uploaded successfully: ' . $path);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Log error for debugging
|
||||
Log::error('File upload failed: ' . $e->getMessage(), [
|
||||
'field' => $field,
|
||||
'file' => $file->getClientOriginalName(),
|
||||
'transaction_id' => $transaction->id,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
return back()->withErrors(['error' => 'Gagal mengupload file: ' . $field . '. Error: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Postcheck::create($data);
|
||||
return redirect()->route('transaction')->with('success', 'Postcheck berhasil disimpan');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Postcheck creation failed: ' . $e->getMessage());
|
||||
return back()->withErrors(['error' => 'Gagal menyimpan data postcheck. Silakan coba lagi.']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the base storage directory exists
|
||||
*/
|
||||
private function ensureStorageDirectoryExists()
|
||||
{
|
||||
$storagePath = storage_path('app/public');
|
||||
|
||||
if (!is_dir($storagePath)) {
|
||||
if (!mkdir($storagePath, 0755, true)) {
|
||||
Log::error('Failed to create storage directory: ' . $storagePath);
|
||||
throw new \Exception('Cannot create storage directory: ' . $storagePath . '. Please run: php fix_permissions.php or manually create the directory.');
|
||||
}
|
||||
Log::info('Created storage directory: ' . $storagePath);
|
||||
}
|
||||
|
||||
// Check if directory is writable
|
||||
if (!is_writable($storagePath)) {
|
||||
Log::error('Storage directory is not writable: ' . $storagePath);
|
||||
throw new \Exception(
|
||||
'Storage directory is not writable: ' . $storagePath . '. ' .
|
||||
'Please run one of these commands from your project root: ' .
|
||||
'1) php fix_permissions.php ' .
|
||||
'2) chmod -R 775 storage/ ' .
|
||||
'3) mkdir -p storage/app/public/transactions/{transaction_id}/postcheck'
|
||||
);
|
||||
}
|
||||
|
||||
// Check if we can create subdirectories
|
||||
$testDir = $storagePath . '/test_' . time();
|
||||
if (!mkdir($testDir, 0755, true)) {
|
||||
Log::error('Cannot create subdirectories in storage: ' . $storagePath);
|
||||
throw new \Exception(
|
||||
'Cannot create subdirectories in storage. ' .
|
||||
'Please check permissions and run: php fix_permissions.php'
|
||||
);
|
||||
}
|
||||
|
||||
// Clean up test directory
|
||||
rmdir($testDir);
|
||||
Log::info('Storage directory is properly configured: ' . $storagePath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,32 +74,32 @@ class PrechecksController extends Controller
|
||||
try {
|
||||
$file = $request->file($field);
|
||||
|
||||
// Generate unique filename
|
||||
$filename = time() . '_' . uniqid() . '_' . $field . '.' . $file->getClientOriginalExtension();
|
||||
// Generate unique filename with transaction ID
|
||||
$filename = time() . '_' . uniqid() . '_' . $transaction->id . '_' . $field . '.' . $file->getClientOriginalExtension();
|
||||
|
||||
// Create directory path
|
||||
$directory = 'prechecks/' . date('Y/m');
|
||||
// Create directory path: transactions/{transaction_id}/precheck/
|
||||
$directory = 'transactions/' . $transaction->id . '/precheck';
|
||||
|
||||
// Ensure base storage directory exists
|
||||
$this->ensureStorageDirectoryExists();
|
||||
|
||||
// Ensure prechecks directory exists
|
||||
if (!Storage::disk('public')->exists('prechecks')) {
|
||||
Storage::disk('public')->makeDirectory('prechecks', 0755, true);
|
||||
Log::info('Created prechecks directory');
|
||||
// Ensure transactions directory exists
|
||||
if (!Storage::disk('public')->exists('transactions')) {
|
||||
Storage::disk('public')->makeDirectory('transactions', 0755, true);
|
||||
Log::info('Created transactions directory');
|
||||
}
|
||||
|
||||
// Ensure year directory exists
|
||||
$yearDir = 'prechecks/' . date('Y');
|
||||
if (!Storage::disk('public')->exists($yearDir)) {
|
||||
Storage::disk('public')->makeDirectory($yearDir, 0755, true);
|
||||
Log::info('Created year directory: ' . $yearDir);
|
||||
// Ensure transaction ID directory exists
|
||||
$transactionDir = 'transactions/' . $transaction->id;
|
||||
if (!Storage::disk('public')->exists($transactionDir)) {
|
||||
Storage::disk('public')->makeDirectory($transactionDir, 0755, true);
|
||||
Log::info('Created transaction directory: ' . $transactionDir);
|
||||
}
|
||||
|
||||
// Ensure month directory exists
|
||||
// Ensure precheck directory exists
|
||||
if (!Storage::disk('public')->exists($directory)) {
|
||||
Storage::disk('public')->makeDirectory($directory, 0755, true);
|
||||
Log::info('Created month directory: ' . $directory);
|
||||
Log::info('Created precheck directory: ' . $directory);
|
||||
}
|
||||
|
||||
// Store file in organized directory structure
|
||||
@@ -114,6 +114,8 @@ class PrechecksController extends Controller
|
||||
'size' => $file->getSize(),
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'uploaded_at' => now()->toISOString(),
|
||||
'transaction_id' => $transaction->id,
|
||||
'filename' => $filename,
|
||||
];
|
||||
|
||||
Log::info('File uploaded successfully: ' . $path);
|
||||
@@ -123,6 +125,7 @@ class PrechecksController extends Controller
|
||||
Log::error('File upload failed: ' . $e->getMessage(), [
|
||||
'field' => $field,
|
||||
'file' => $file->getClientOriginalName(),
|
||||
'transaction_id' => $transaction->id,
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
@@ -164,7 +167,7 @@ class PrechecksController extends Controller
|
||||
'Please run one of these commands from your project root: ' .
|
||||
'1) php fix_permissions.php ' .
|
||||
'2) chmod -R 775 storage/ ' .
|
||||
'3) mkdir -p storage/app/public/prechecks/' . date('Y/m')
|
||||
'3) mkdir -p storage/app/public/transactions/{transaction_id}/precheck'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user