This commit is contained in:
ct
2025-06-20 13:03:52 +08:00
parent eef45fdc9d
commit b502120091
22 changed files with 426 additions and 164 deletions

View File

@@ -52,7 +52,7 @@ public function run(): void
$csv_path = database_path('seeders/data/webm_metadata.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;
@@ -60,12 +60,10 @@ 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']);
$meme_record['keywords'] = $this->stringToCleanArray($meme_record['keywords']);
try {
// Check for duplicates OUTSIDE of transaction
$base_filename = pathinfo($meme_record['filename'], PATHINFO_FILENAME);
@@ -158,6 +156,7 @@ private function stringToCleanArray($string)
return array_filter(array_map(function ($item) {
$item = trim($item); // Remove whitespace
$item = preg_replace('/[^\w\s]/', '', $item); // Remove punctuation
return trim(preg_replace('/\s+/', ' ', $item)); // Clean extra spaces
}, explode(',', $string)), function ($value) {
return $value !== '';
@@ -192,13 +191,13 @@ 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;
@@ -208,7 +207,7 @@ private function importSingleMeme(array $meme_record): bool
// Generate embedding
try {
$embedding = CloudflareAI::getVectorEmbeddingBgeSmall(
$meme_record['name'] . ' ' . $meme_record['description'] . ' ' . implode(' ', $meme_record['keywords'])
$meme_record['name'].' '.$meme_record['description'].' '.implode(' ', $meme_record['keywords'])
);
} catch (\Exception $e) {
$this->command->warn("Failed to generate embedding for {$meme_record['filename']}: {$e->getMessage()}");
@@ -256,7 +255,7 @@ private function importSingleMeme(array $meme_record): bool
// 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) {
@@ -270,11 +269,11 @@ 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());
Log::warning("Failed to attach tags", [
$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,
'error' => $e->getMessage()
'error' => $e->getMessage(),
]);
}
}
@@ -284,7 +283,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}";
}
/**