Add image-gen module: Spring AI ImageModel options on the wire, a moderation gate, and safe storage of generated images in MinIO (sniffed content type, byte and pixel caps, server-chosen keys, capped URL fetcher); per-provider cost table dated 2026-10-28. ImageModel and ModerationModel are hand-written fakes; MinIO is real via Testcontainers; no image provider called.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7rnC7jppGGdHGESvy
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.ankurm.imagegen;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.ai.image.Image;
|
||||
import org.springframework.ai.image.ImageGeneration;
|
||||
import org.springframework.ai.image.ImageModel;
|
||||
import org.springframework.ai.image.ImageOptions;
|
||||
import org.springframework.ai.image.ImagePrompt;
|
||||
import org.springframework.ai.image.ImageResponse;
|
||||
import org.springframework.ai.openai.metadata.OpenAiImageGenerationMetadata;
|
||||
|
||||
/**
|
||||
* prompt check -> ImageModel -> bytes -> verify -> store. Each arrow is a place something can be
|
||||
* refused, and every refusal is a {@link RejectedException} with a stable reason code.
|
||||
*
|
||||
* <p>If one image of several fails verification nothing is stored: a half-saved batch is harder to
|
||||
* reason about than a retry.
|
||||
*/
|
||||
public final class ImageService {
|
||||
|
||||
private final ImageModel model;
|
||||
private final Function<ImageRequest, ImageOptions> options;
|
||||
private final PromptGate gate;
|
||||
private final ImagePolicy policy;
|
||||
private final ImageVerifier verifier;
|
||||
private final RemoteFetcher fetcher;
|
||||
private final ImageStore store;
|
||||
|
||||
public ImageService(ImageModel model, Function<ImageRequest, ImageOptions> options, PromptGate gate,
|
||||
ImagePolicy policy, RemoteFetcher fetcher, ImageStore store) {
|
||||
this.model = model;
|
||||
this.options = options;
|
||||
this.gate = gate;
|
||||
this.policy = policy;
|
||||
this.verifier = new ImageVerifier(policy);
|
||||
this.fetcher = fetcher;
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public List<StoredImage> generate(ImageRequest request) {
|
||||
gate.check(request.prompt());
|
||||
ImageOptions o = options.apply(request);
|
||||
ImageResponse response = model.call(new ImagePrompt(request.prompt(), o));
|
||||
|
||||
record Pending(byte[] bytes, Sniffed sniffed, String revised) {}
|
||||
List<Pending> verified = new ArrayList<>();
|
||||
for (ImageGeneration g : response.getResults()) {
|
||||
byte[] bytes = bytesOf(g.getOutput());
|
||||
Sniffed sniffed = verifier.verify(bytes);
|
||||
String revised = g.getMetadata() instanceof OpenAiImageGenerationMetadata m ? m.getRevisedPrompt() : null;
|
||||
verified.add(new Pending(bytes, sniffed, revised));
|
||||
}
|
||||
List<StoredImage> stored = new ArrayList<>();
|
||||
for (Pending p : verified) {
|
||||
stored.add(store.put(request.owner(), p.bytes(), p.sniffed(), p.revised()));
|
||||
}
|
||||
return stored;
|
||||
}
|
||||
|
||||
byte[] bytesOf(Image image) {
|
||||
if (image.getB64Json() != null) {
|
||||
String b64 = image.getB64Json();
|
||||
// Check the size from the string length BEFORE decoding, so a 2 GB string never becomes a 1.5 GB array.
|
||||
long approx = (long) b64.length() * 3 / 4;
|
||||
if (approx > policy.maxBytes() + 3) {
|
||||
throw new RejectedException("too-large", "base64 payload decodes to about " + approx + " bytes > cap " + policy.maxBytes());
|
||||
}
|
||||
try {
|
||||
return Base64.getDecoder().decode(b64);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new RejectedException("bad-base64", "payload is not valid base64");
|
||||
}
|
||||
}
|
||||
if (image.getUrl() != null) {
|
||||
return fetcher.fetch(image.getUrl());
|
||||
}
|
||||
throw new RejectedException("empty-result", "provider returned neither b64_json nor url");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user