fix(surveyor): perbaikkan upload foto duplikat

This commit is contained in:
majid
2025-02-26 09:38:52 +07:00
parent 3d7da22ac0
commit 77189701d6
2 changed files with 182 additions and 33 deletions

View File

@@ -506,67 +506,67 @@ class SurveyorController extends Controller
{ {
if ($request->hasFile($paramName)) { if ($request->hasFile($paramName)) {
$files = $request->file($paramName); $files = $request->file($paramName);
// Pastikan $files adalah array
if (!is_array($files)) { if (!is_array($files)) {
$files = [$files]; $files = [$files];
} }
$formatFotoData = []; $formatFotoData = [];
$nomor_registrasi = $request->nomor_registrasi; $nomor_registrasi = $request->nomor_registrasi;
// Generate a unique timestamp for this batch
$batchTimestamp = time(); $batchTimestamp = time();
// Create a lookup map of existing file names for faster checking $existingFilePaths = [];
$existingFileNames = [];
if (isset($formatFotojson[$paramName]) && is_array($formatFotojson[$paramName])) { if (isset($formatFotojson[$paramName]) && is_array($formatFotojson[$paramName])) {
foreach ($formatFotojson[$paramName] as $existingFile) { foreach ($formatFotojson[$paramName] as $existingFile) {
if (isset($existingFile['name'])) { if (isset($existingFile['path'])) {
$existingFileNames[$existingFile['name']] = true; $existingFilePaths[] = $existingFile['path'];
} }
} }
} }
foreach ($files as $index => $file) { foreach ($files as $index => $file) {
$originalName = $file->getClientOriginalName(); $originalName = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension(); $extension = $file->getClientOriginalExtension();
$fileNameWithoutExt = pathinfo($originalName, PATHINFO_FILENAME); $fileNameWithoutExt = pathinfo($originalName, PATHINFO_FILENAME);
// Validasi nama file
if (empty($fileNameWithoutExt)) { if (empty($fileNameWithoutExt)) {
$fileNameWithoutExt = "file_{$batchTimestamp}_{$index}"; $fileNameWithoutExt = "file_{$batchTimestamp}_{$index}";
} }
// Check if this file name already exists in formatFotojson $safeFileName = preg_replace('/[^a-zA-Z0-9_-]/', '_', $fileNameWithoutExt);
if (isset($existingFileNames[$fileNameWithoutExt])) { $uniqueFileName = "{$batchTimestamp}_{$index}_{$safeFileName}.{$extension}";
// File already exists, skip it $path = "surveyor/{$paramName}/{$nomor_registrasi}/{$uniqueFileName}";
continue;
if (in_array($path, $existingFilePaths)) {
$counter = 1;
do {
$uniquePath = "surveyor/{$paramName}/{$nomor_registrasi}/{$batchTimestamp}_{$index}_{$safeFileName}_{$counter}.{$extension}";
$counter++;
} while (in_array($uniquePath, $existingFilePaths));
$path = $uniquePath;
} }
// Use batchTimestamp and index to ensure uniqueness in storage $savedPath = $file->storeAs('', $path, 'public');
$uniqueFileName = "{$batchTimestamp}_{$index}.{$extension}";
$existingFilePaths[] = $path;
// Simpan file
$path = $file->storeAs("surveyor/{$paramName}/{$nomor_registrasi}", $uniqueFileName, 'public');
// Add file data // Add file data
$fotoData = [ $fotoData = [
'name' => $fileNameWithoutExt, 'name' => $fileNameWithoutExt,
'path' => $path, 'path' => $savedPath,
'category' => 'lainnya', 'category' => 'lainnya',
'sub' => null, 'sub' => null,
'description' => null, 'description' => null,
'created_by' => Auth::user()->name, 'created_by' => Auth::user()->name,
'created_at' => now()->toDateTimeString(), 'created_at' => now()->toDateTimeString(),
]; ];
$formatFotoData[] = $fotoData; $formatFotoData[] = $fotoData;
// Add to the lookup map to prevent duplicates within the same batch
$existingFileNames[$fileNameWithoutExt] = true;
} }
// Only update if we have new photos to add // Only update if we have new photos to add
if (!empty($formatFotoData)) { if (!empty($formatFotoData)) {
// Struktur JSON yang konsisten // Struktur JSON yang konsisten
@@ -578,11 +578,11 @@ class SurveyorController extends Controller
$formatFotoData $formatFotoData
); );
} }
return $formatFotoData; return $formatFotoData;
} }
} }
return []; return [];
} }

View File

@@ -0,0 +1,149 @@
{
"upload_foto": [
{
"name": "1. Tampak Akses Jalan Menuju Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536517_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:21:57"
},
{
"name": "2. Tampak Akses Jalan Menuju Objek penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536518_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:21:58"
},
{
"name": "3. Tampak Akses Jalan Menuju Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536519_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:21:59"
},
{
"name": "4. Tampak Akses Jalan menuju Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536520_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:00"
},
{
"name": "5. Tampak Akses Jalan menuju Objek penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536520_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:00"
},
{
"name": "6. Tampak Akses Jalan di Dalam Kawasan Perumahan",
"path": "surveyor/upload_foto/REG0000025/1740536521_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:01"
},
{
"name": "8. Tampak Depan Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536522_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:02"
},
{
"name": "9. Tampak Objek Penilaian dari Samping",
"path": "surveyor/upload_foto/REG0000025/1740536522_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:02"
},
{
"name": "10. Tampak Objek Penilaian dari Samping",
"path": "surveyor/upload_foto/REG0000025/1740536523_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:03"
},
{
"name": "11. Tampak Halaman Depan Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536524_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:04"
},
{
"name": "12. Tampak Dalam Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536525_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:05"
},
{
"name": "13. Tampak Dalam Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536525_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:05"
},
{
"name": "14. Tampak Dalam Objek penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536526_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:06"
},
{
"name": "15. Tampak Dalam Objek Penilaian",
"path": "surveyor/upload_foto/REG0000025/1740536526_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:06"
},
{
"name": "16. Tampak Dalam Ojek Penilaian Lantai 2",
"path": "surveyor/upload_foto/REG0000025/1740536527_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:07"
},
{
"name": "17. Tampak Pendamping Lapangan",
"path": "surveyor/upload_foto/REG0000025/1740536527_0.png",
"category": "lainnya",
"sub": null,
"description": null,
"created_by": "Administrator",
"created_at": "2025-02-26 02:22:08"
}
],
"object_jaminan": [{ "foto_objek": null }]
}