This commit is contained in:
ct
2025-07-15 00:43:37 +08:00
parent e7c6c02785
commit 8bb6705031
9 changed files with 481 additions and 58 deletions

BIN
database/.DS_Store vendored

Binary file not shown.

View File

@@ -2,8 +2,8 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{

Binary file not shown.

View File

@@ -53,7 +53,7 @@ public function run(): void
$csv_path = database_path('seeders/data/g2.csv');
$meme_data = $this->parseCsvFile($csv_path);
$this->command->info('📊 Found ' . count($meme_data) . ' memes to import');
$this->command->info('📊 Found '.count($meme_data).' memes to import');
// Process records individually for PostgreSQL compatibility
$total_processed = 0;
@@ -61,12 +61,13 @@ public function run(): void
$total_failed = 0;
foreach ($meme_data as $index => $meme_record) {
$this->command->info('Processing ' . ($index + 1) . '/' . count($meme_data) . ': ' . $meme_record['filename']);
$this->command->info('Processing '.($index + 1).'/'.count($meme_data).': '.$meme_record['filename']);
// Skip empty or malformed records
if (empty($meme_record['filename']) || empty($meme_record['type']) || empty($meme_record['name'])) {
$this->command->warn("⏭️ Skipping malformed CSV record at line " . ($index + 1) . ": missing filename, type, or name");
$this->command->warn('⏭️ Skipping malformed CSV record at line '.($index + 1).': missing filename, type, or name');
$total_skipped++;
continue;
}
@@ -93,12 +94,12 @@ public function run(): void
}
} else {
$this->command->error("❌ Failed to import: {$meme_record['filename']} - Import returned false");
$this->command->error("🛑 Halting seeder to investigate the issue");
$this->command->error('🛑 Halting seeder to investigate the issue');
throw new \RuntimeException("Import failed for {$meme_record['filename']}");
}
} catch (\Exception $e) {
$this->command->error("❌ Failed to import: {$meme_record['filename']} - {$e->getMessage()}");
$this->command->error("🛑 Halting seeder to investigate the issue");
$this->command->error('🛑 Halting seeder to investigate the issue');
throw $e;
}
}
@@ -144,24 +145,24 @@ private function parseCsvFile(string $csv_path): array
// Use Laravel Excel to parse CSV with proper handling of multi-line fields
$collection = Excel::toCollection(null, $csv_path)->first();
if ($collection->isEmpty()) {
throw new \RuntimeException("CSV file is empty or could not be parsed");
throw new \RuntimeException('CSV file is empty or could not be parsed');
}
// Get headers from first row
$headers = $collection->first()->toArray();
// Convert remaining rows to associative arrays
$meme_data = [];
foreach ($collection->skip(1) as $row) {
$row_array = $row->toArray();
// Skip empty rows
if (empty(array_filter($row_array))) {
continue;
}
// Ensure row has same number of columns as headers
if (count($row_array) === count($headers)) {
$record = array_combine($headers, $row_array);
@@ -213,20 +214,20 @@ private function importSingleMeme(array $meme_record): bool
'save_url', // Mode: just save URL reference
null, // Auto-generate filename
'r2', // Disk (not used for URL mode)
trim($meme_record['name']) . " ({$format})", // Name with format
trim($meme_record['name'])." ({$format})", // Name with format
null, // No specific user
$config['mime'] // MIME type
);
$media_uuids[$format . '_uuid'] = $media->uuid;
$media_urls[$format . '_url'] = $url;
$media_uuids[$format.'_uuid'] = $media->uuid;
$media_urls[$format.'_url'] = $url;
} catch (\Exception $e) {
$this->command->error("Failed to create {$format} media for {$meme_record['filename']}: {$e->getMessage()}");
throw $e;
}
}
//dump($meme_record);
// dump($meme_record);
// // Generate embedding
// try {
@@ -238,7 +239,7 @@ private function importSingleMeme(array $meme_record): bool
// $embedding = null; // Continue without embedding
// }
//dd($embedding);
// dd($embedding);
// Check if record exists one more time within transaction
$existing_meme = MemeMedia::where('original_id', $meme_record['filename'])
@@ -253,12 +254,12 @@ private function importSingleMeme(array $meme_record): bool
// Check for null description before creating record
if ($meme_record['description'] === null) {
$this->command->error("❌ NULL DESCRIPTION DETECTED:");
$this->command->error('❌ NULL DESCRIPTION DETECTED:');
$this->command->error(" Filename: {$meme_record['filename']}");
$this->command->error(" Name: {$meme_record['name']}");
$this->command->error(" Description field is NULL in CSV data");
$this->command->error(" CSV row data: " . json_encode($meme_record));
$this->command->error("🛑 HALTING SEEDER - FIX THE CSV DATA");
$this->command->error(' Description field is NULL in CSV data');
$this->command->error(' CSV row data: '.json_encode($meme_record));
$this->command->error('🛑 HALTING SEEDER - FIX THE CSV DATA');
throw new \RuntimeException("NULL description found for {$meme_record['filename']} - CSV data needs to be fixed");
}
@@ -287,13 +288,13 @@ private function importSingleMeme(array $meme_record): bool
]);
$meme_media->duration = MemeMediaMaintenance::getDurationUsingFfmpeg($meme_media);
//$meme_media->embedding = $embedding;
// $meme_media->embedding = $embedding;
$meme_media->save();
// Add keywords as tags
$this->attachKeywordsAsTags($meme_media, $meme_record['keywords']);
$this->command->info('✅ Imported: ' . trim($meme_record['name']));
$this->command->info('✅ Imported: '.trim($meme_record['name']));
return true;
} catch (\Exception $e) {
@@ -307,7 +308,7 @@ private function attachKeywordsAsTags(MemeMedia $meme_media, array $keywords): v
try {
$meme_media->attachTags($keywords, 'meme_media');
} catch (\Exception $e) {
$this->command->warn("Failed to attach tags to meme media '{$meme_media->name}': " . $e->getMessage());
$this->command->warn("Failed to attach tags to meme media '{$meme_media->name}': ".$e->getMessage());
Log::warning('Failed to attach tags', [
'category_id' => $meme_media->id,
'keywords' => $keywords,
@@ -321,7 +322,7 @@ private function attachKeywordsAsTags(MemeMedia $meme_media, array $keywords): v
*/
private function generateCdnUrl(string $base_filename, string $extension): string
{
return self::CDN_BASE_URL . "/{$extension}/{$base_filename}.{$extension}";
return self::CDN_BASE_URL."/{$extension}/{$base_filename}.{$extension}";
}
/**