-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_screenshots.php
More file actions
405 lines (338 loc) · 15.1 KB
/
Copy pathdebug_screenshots.php
File metadata and controls
405 lines (338 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* Script untuk debug dan perbaikan tampilan screenshots di project
*/
// Bootstrap Laravel
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
// Import model dan facades
use App\Models\Project;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
echo "=== DEBUG SCREENSHOTS PROJECT ===\n\n";
// Fungsi untuk memeriksa project
function checkProject($projectId) {
try {
$project = Project::findOrFail($projectId);
echo "Project ditemukan: {$project->title} (ID: {$project->id})\n";
echo "------------------------------\n";
// Cek kolom screenshots di database
$rawProject = DB::table('projects')->where('id', $projectId)->first();
echo "Raw screenshots dari database: " . ($rawProject->screenshots ?? 'NULL') . "\n\n";
// Cek hasil cast oleh model
$modelScreenshots = $project->screenshots;
echo "Screenshots setelah di-cast oleh model: \n";
if (is_array($modelScreenshots)) {
echo "Tipe data: Array dengan " . count($modelScreenshots) . " item\n";
foreach ($modelScreenshots as $index => $screenshot) {
echo " " . ($index + 1) . ". $screenshot\n";
// Cek apakah file/URL valid
if (filter_var($screenshot, FILTER_VALIDATE_URL)) {
echo " - Valid URL\n";
// Coba akses URL
$headers = @get_headers($screenshot);
if ($headers && strpos($headers[0], '200') !== false) {
echo " - URL dapat diakses (200 OK)\n";
} else {
echo " - URL tidak dapat diakses: " . ($headers[0] ?? 'Unknown error') . "\n";
}
} else if (Storage::disk('public')->exists(str_replace('storage/', '', $screenshot))) {
echo " - File lokal ditemukan\n";
echo " - Path lengkap: " . Storage::disk('public')->path(str_replace('storage/', '', $screenshot)) . "\n";
} else {
echo " - PERINGATAN: Bukan URL valid dan file tidak ditemukan\n";
// Cek apakah perlu ditambahkan storage/
if (Storage::disk('public')->exists($screenshot)) {
echo " - File ditemukan di storage/app/public/{$screenshot}\n";
echo " - Seharusnya diakses dengan: " . asset('storage/' . $screenshot) . "\n";
}
}
}
} else if (is_string($modelScreenshots)) {
echo "Tipe data: String\n";
echo "Nilai: $modelScreenshots\n";
// Coba parse JSON
$parsed = json_decode($modelScreenshots, true);
if (json_last_error() === JSON_ERROR_NONE) {
echo "String dapat di-parse sebagai JSON\n";
echo "Hasil parse: " . print_r($parsed, true) . "\n";
} else {
echo "String bukan JSON valid: " . json_last_error_msg() . "\n";
}
} else if (is_null($modelScreenshots)) {
echo "Tipe data: NULL\n";
} else {
echo "Tipe data tidak dikenal: " . gettype($modelScreenshots) . "\n";
echo "Nilai: " . print_r($modelScreenshots, true) . "\n";
}
echo "\n";
// Cek implementasi view
echo "Simulasi render di view:\n";
echo "------------------------------\n";
if (!empty($project->screenshots) && count($project->screenshots) > 0) {
foreach ($project->screenshots as $index => $screenshot) {
echo "Rendering screenshot " . ($index + 1) . ": $screenshot\n";
// Cek apakah perlu menambahkan storage/
if (!filter_var($screenshot, FILTER_VALIDATE_URL) && !str_starts_with($screenshot, 'storage/') && !str_starts_with($screenshot, '/storage/')) {
echo " - URL yang akan dirender: " . asset('storage/' . $screenshot) . "\n";
} else {
echo " - URL yang akan dirender: $screenshot\n";
}
}
} else {
echo "Tidak ada screenshots untuk dirender\n";
}
return $project;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
return null;
}
}
// Fungsi untuk memperbaiki screenshots
function fixScreenshots($projectId) {
try {
$project = Project::findOrFail($projectId);
$screenshots = $project->screenshots;
echo "Mencoba memperbaiki screenshots untuk project: {$project->title}\n";
echo "------------------------------\n";
// Jika screenshots adalah string, coba parse
if (is_string($screenshots)) {
$parsed = json_decode($screenshots, true);
if (json_last_error() === JSON_ERROR_NONE) {
echo "Memperbaiki screenshots dari string JSON menjadi array\n";
$project->screenshots = $parsed;
$project->save();
echo "Berhasil menyimpan screenshots sebagai array\n";
} else {
echo "Gagal parse JSON: " . json_last_error_msg() . "\n";
}
}
// Jika screenshots adalah array
else if (is_array($screenshots)) {
$fixed = [];
$changed = false;
foreach ($screenshots as $index => $screenshot) {
// Jika screenshot adalah URL lengkap, biarkan
if (filter_var($screenshot, FILTER_VALIDATE_URL)) {
$fixed[] = $screenshot;
}
// Jika screenshot adalah path lokal tanpa storage/
else if (!str_starts_with($screenshot, 'storage/') && !str_starts_with($screenshot, '/storage/')) {
echo "Memperbaiki path screenshot " . ($index + 1) . ": $screenshot\n";
// Cek apakah file ada
if (Storage::disk('public')->exists($screenshot)) {
$fixed[] = $screenshot; // Simpan path relatif, view akan menambahkan storage/
echo " - File ditemukan, menyimpan path relatif: $screenshot\n";
} else {
echo " - File tidak ditemukan, mencoba cari di lokasi lain\n";
// Coba cari di storage/app/public/projects
if (Storage::disk('public')->exists('projects/' . basename($screenshot))) {
$newPath = 'projects/' . basename($screenshot);
$fixed[] = $newPath;
$changed = true;
echo " - File ditemukan di lokasi alternatif: $newPath\n";
} else {
// Tetap simpan path asli
$fixed[] = $screenshot;
echo " - File tidak ditemukan, menyimpan path asli\n";
}
}
} else {
// Path sudah memiliki storage/, hapus prefixnya
$cleanPath = str_replace(['storage/', '/storage/'], '', $screenshot);
$fixed[] = $cleanPath;
$changed = true;
echo "Menghapus prefix 'storage/' dari screenshot " . ($index + 1) . "\n";
}
}
if ($changed) {
$project->screenshots = $fixed;
$project->save();
echo "Berhasil menyimpan screenshots yang diperbaiki\n";
} else {
echo "Tidak ada perubahan yang diperlukan pada screenshots\n";
}
} else {
echo "Screenshots kosong atau format tidak valid\n";
}
return $project;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
return null;
}
}
// Fungsi untuk menambahkan contoh screenshots
function addSampleScreenshots($projectId) {
try {
$project = Project::findOrFail($projectId);
echo "Menambahkan contoh screenshots ke project: {$project->title}\n";
echo "------------------------------\n";
// Contoh screenshots dari Unsplash
$sampleScreenshots = [
'https://images.unsplash.com/photo-1517292987719-0369a794ec0f?q=80&w=1000',
'https://images.unsplash.com/photo-1573867639040-6dd25fa5f597?q=80&w=1000',
'https://images.unsplash.com/photo-1581291518633-83b4ebd1d83e?q=80&w=1000',
'https://images.unsplash.com/photo-1551288049-bebda4e38f71?q=80&w=1000'
];
$project->screenshots = $sampleScreenshots;
$project->save();
echo "Berhasil menambahkan " . count($sampleScreenshots) . " contoh screenshots\n";
return $project;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
return null;
}
}
// Fungsi untuk menambahkan screenshots dari folder lokal
function addLocalScreenshots($projectId) {
try {
$project = Project::findOrFail($projectId);
echo "Menambahkan screenshots lokal ke project: {$project->title}\n";
echo "------------------------------\n";
// Daftar file di folder projects
$files = Storage::disk('public')->files('projects');
$imageFiles = array_filter($files, function($file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
return in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'gif']);
});
// Pilih 4 file gambar secara acak
shuffle($imageFiles);
$selectedImages = array_slice($imageFiles, 0, 4);
if (empty($selectedImages)) {
echo "Tidak ada file gambar yang ditemukan di folder projects\n";
return null;
}
echo "File gambar yang ditemukan:\n";
foreach ($selectedImages as $index => $image) {
echo " " . ($index + 1) . ". $image\n";
}
// Simpan path gambar ke database
$project->screenshots = $selectedImages;
$project->save();
echo "Berhasil menambahkan " . count($selectedImages) . " screenshots lokal\n";
return $project;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
return null;
}
}
// Fungsi untuk memeriksa dan memperbaiki tampilan
function checkAndFixView() {
echo "Memeriksa kode view untuk tampilan screenshots...\n";
$projectDetailPath = base_path('resources/views/pages/project-detail.blade.php');
if (!file_exists($projectDetailPath)) {
echo "File view project-detail.blade.php tidak ditemukan!\n";
return;
}
$content = file_get_contents($projectDetailPath);
// Cek apakah ada masalah dengan kode view
if (strpos($content, '<img src="{{ $screenshot }}') !== false) {
echo "MASALAH DITEMUKAN: Path gambar tidak menggunakan asset('storage/')\n";
// Perbaiki kode view
$fixedContent = str_replace(
'<img src="{{ $screenshot }}"',
'<img src="{{ filter_var($screenshot, FILTER_VALIDATE_URL) ? $screenshot : asset(\'storage/\' . $screenshot) }}"',
$content
);
// Simpan perubahan
file_put_contents($projectDetailPath . '.fixed', $fixedContent);
echo "Kode view diperbaiki dan disimpan di: " . $projectDetailPath . ".fixed\n";
echo "Untuk menerapkan perubahan, jalankan perintah:\n";
echo "mv " . $projectDetailPath . ".fixed " . $projectDetailPath . "\n";
} else if (strpos($content, "asset('storage/' . \$screenshot)") !== false ||
strpos($content, 'asset("storage/" . $screenshot)') !== false) {
echo "Kode view sudah benar, menggunakan asset('storage/')\n";
} else {
echo "Tidak dapat menentukan masalah pada kode view. Periksa manual file:\n";
echo $projectDetailPath . "\n";
}
}
// Menu utama
function showMenu() {
echo "\n=== MENU ===\n";
echo "1. Periksa screenshots project\n";
echo "2. Perbaiki screenshots project\n";
echo "3. Tambahkan contoh screenshots (URL)\n";
echo "4. Tambahkan screenshots dari folder lokal\n";
echo "5. Periksa dan perbaiki kode view\n";
echo "6. Keluar\n";
echo "Pilih menu (1-6): ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
fclose($handle);
return trim($line);
}
// Fungsi untuk memilih project
function selectProject() {
// Ambil daftar project
$projects = Project::select('id', 'title')->orderBy('id')->get();
if ($projects->isEmpty()) {
echo "Tidak ada project yang tersedia.\n";
return null;
}
echo "\n=== DAFTAR PROJECT ===\n";
foreach ($projects as $index => $project) {
echo ($index + 1) . ". [{$project->id}] {$project->title}\n";
}
echo "\nPilih project (1-" . count($projects) . ") atau masukkan ID project: ";
$handle = fopen("php://stdin", "r");
$line = trim(fgets($handle));
fclose($handle);
// Cek apakah input adalah ID langsung
if (is_numeric($line) && $line > count($projects)) {
try {
$project = Project::findOrFail($line);
return $project->id;
} catch (\Exception $e) {
echo "Project dengan ID $line tidak ditemukan.\n";
return null;
}
}
$choice = (int)$line;
if ($choice < 1 || $choice > count($projects)) {
echo "Pilihan tidak valid.\n";
return null;
}
return $projects[$choice - 1]->id;
}
// Fungsi utama
function main() {
while (true) {
$choice = showMenu();
if ($choice == '6') {
echo "\nTerima kasih telah menggunakan script ini!\n";
exit(0);
}
if ($choice == '5') {
checkAndFixView();
} else {
$projectId = selectProject();
if (!$projectId) {
continue;
}
switch ($choice) {
case '1':
checkProject($projectId);
break;
case '2':
fixScreenshots($projectId);
break;
case '3':
addSampleScreenshots($projectId);
break;
case '4':
addLocalScreenshots($projectId);
break;
default:
echo "\nPilihan tidak valid. Silakan coba lagi.\n";
}
}
echo "\nTekan Enter untuk melanjutkan...";
$handle = fopen("php://stdin", "r");
fgets($handle);
fclose($handle);
}
}
// Jalankan program
main();