|
| 1 | +package com.datadoghq.stickerlandia.stickeraward.sticker; |
| 2 | + |
| 3 | +import com.datadoghq.stickerlandia.stickeraward.sticker.entity.Sticker; |
| 4 | +import io.quarkus.runtime.StartupEvent; |
| 5 | +import jakarta.enterprise.context.ApplicationScoped; |
| 6 | +import jakarta.enterprise.event.Observes; |
| 7 | +import jakarta.inject.Inject; |
| 8 | +import jakarta.transaction.Transactional; |
| 9 | +import java.io.InputStream; |
| 10 | +import org.jboss.logging.Logger; |
| 11 | + |
| 12 | +/** |
| 13 | + * Service responsible for seeding sticker images during application startup. This class loads |
| 14 | + * default sticker images from the classpath and uploads them to the configured storage service, |
| 15 | + * associating them with existing stickers. |
| 16 | + */ |
| 17 | +@ApplicationScoped |
| 18 | +public class StickerImageSeeder { |
| 19 | + |
| 20 | + private static final Logger LOG = Logger.getLogger(StickerImageSeeder.class); |
| 21 | + |
| 22 | + @Inject StickerImageService stickerImageService; |
| 23 | + |
| 24 | + @Inject StickerRepository stickerRepository; |
| 25 | + |
| 26 | + /** |
| 27 | + * Handles the startup event to seed sticker images. This method is called during application |
| 28 | + * startup and ensures that default sticker images are loaded and associated with existing |
| 29 | + * stickers. |
| 30 | + * |
| 31 | + * @param ev the startup event |
| 32 | + */ |
| 33 | + @Transactional |
| 34 | + public void onStartup(@Observes StartupEvent ev) { |
| 35 | + LOG.info("Starting sticker image seeding..."); |
| 36 | + |
| 37 | + try { |
| 38 | + seedStickerImage("sticker-001", "/stickers/dd_icon_rgb.png", "Datadog Purple Logo"); |
| 39 | + seedStickerImage("sticker-002", "/stickers/dd_icon_white.png", "Datadog White Logo"); |
| 40 | + |
| 41 | + LOG.info("Sticker image seeding completed successfully"); |
| 42 | + } catch (Exception e) { |
| 43 | + LOG.error("Failed to seed sticker images", e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private void seedStickerImage(String stickerId, String resourcePath, String description) { |
| 48 | + // Check if sticker exists and doesn't already have an image |
| 49 | + Sticker sticker = Sticker.findById(stickerId); |
| 50 | + if (sticker == null) { |
| 51 | + LOG.warnf("Sticker %s not found, skipping image seeding", stickerId); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (sticker.getImageKey() != null && !sticker.getImageKey().isEmpty()) { |
| 56 | + LOG.infof( |
| 57 | + "Sticker %s already has image key %s, skipping", |
| 58 | + stickerId, sticker.getImageKey()); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + // Load image from resources |
| 64 | + InputStream imageStream = getClass().getResourceAsStream(resourcePath); |
| 65 | + if (imageStream == null) { |
| 66 | + LOG.errorf("Could not find image resource: %s", resourcePath); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Get file size for upload |
| 71 | + byte[] imageData = imageStream.readAllBytes(); |
| 72 | + imageStream.close(); |
| 73 | + |
| 74 | + // Upload image to our storage service |
| 75 | + InputStream uploadStream = getClass().getResourceAsStream(resourcePath); |
| 76 | + String imageKey = |
| 77 | + stickerImageService.uploadImage(uploadStream, "image/png", imageData.length); |
| 78 | + uploadStream.close(); |
| 79 | + |
| 80 | + // Update sticker with image key |
| 81 | + stickerRepository.updateStickerImageKey(stickerId, imageKey); |
| 82 | + |
| 83 | + LOG.infof( |
| 84 | + "Successfully seeded image for sticker %s with key %s (%s)", |
| 85 | + stickerId, imageKey, description); |
| 86 | + |
| 87 | + } catch (Exception e) { |
| 88 | + LOG.errorf(e, "Failed to seed image for sticker %s from %s", stickerId, resourcePath); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments