Skip to content

Commit 079df0a

Browse files
committed
chore: seed sample stickers
1 parent f42f218 commit 079df0a

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 org.jboss.logging.Logger;
10+
11+
import java.io.InputStream;
12+
13+
@ApplicationScoped
14+
public class StickerImageSeeder {
15+
16+
private static final Logger LOG = Logger.getLogger(StickerImageSeeder.class);
17+
18+
@Inject
19+
StickerImageService stickerImageService;
20+
21+
@Inject
22+
StickerRepository stickerRepository;
23+
24+
@Transactional
25+
public void onStartup(@Observes StartupEvent ev) {
26+
LOG.info("Starting sticker image seeding...");
27+
28+
try {
29+
seedStickerImage("sticker-001", "/stickers/dd_icon_rgb.png", "Datadog Purple Logo");
30+
seedStickerImage("sticker-002", "/stickers/dd_icon_white.png", "Datadog White Logo");
31+
32+
LOG.info("Sticker image seeding completed successfully");
33+
} catch (Exception e) {
34+
LOG.error("Failed to seed sticker images", e);
35+
}
36+
}
37+
38+
private void seedStickerImage(String stickerId, String resourcePath, String description) {
39+
// Check if sticker exists and doesn't already have an image
40+
Sticker sticker = Sticker.findById(stickerId);
41+
if (sticker == null) {
42+
LOG.warnf("Sticker %s not found, skipping image seeding", stickerId);
43+
return;
44+
}
45+
46+
if (sticker.getImageKey() != null && !sticker.getImageKey().isEmpty()) {
47+
LOG.infof("Sticker %s already has image key %s, skipping", stickerId, sticker.getImageKey());
48+
return;
49+
}
50+
51+
try {
52+
// Load image from resources
53+
InputStream imageStream = getClass().getResourceAsStream(resourcePath);
54+
if (imageStream == null) {
55+
LOG.errorf("Could not find image resource: %s", resourcePath);
56+
return;
57+
}
58+
59+
// Get file size for upload
60+
byte[] imageData = imageStream.readAllBytes();
61+
imageStream.close();
62+
63+
// Upload image to our storage service
64+
InputStream uploadStream = getClass().getResourceAsStream(resourcePath);
65+
String imageKey = stickerImageService.uploadImage(uploadStream, "image/png", imageData.length);
66+
uploadStream.close();
67+
68+
// Update sticker with image key
69+
stickerRepository.updateStickerImageKey(stickerId, imageKey);
70+
71+
LOG.infof("Successfully seeded image for sticker %s with key %s (%s)",
72+
stickerId, imageKey, description);
73+
74+
} catch (Exception e) {
75+
LOG.errorf(e, "Failed to seed image for sticker %s from %s", stickerId, resourcePath);
76+
}
77+
}
78+
}
73 KB
Loading
64.1 KB
Loading

0 commit comments

Comments
 (0)