From 76c8f11250d690c93d58c4538c30d6365c1bb55a Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:58:15 +0800 Subject: [PATCH] Add File --- .../java/com/agentsflex/core/image/Image.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java b/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java new file mode 100644 index 0000000..eeac542 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/image/Image.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.agentsflex.core.image; + +import com.agentsflex.core.llm.client.HttpClient; +import com.agentsflex.core.util.IOUtil; +import com.agentsflex.core.util.StringUtil; + +import java.io.File; +import java.util.Arrays; +import java.util.Base64; + +public class Image { + + /** + * The base64-encoded JSON of the generated image + */ + private String b64Json; + + /** + * The URL of the generated image + */ + private String url; + + /** + * The data of image + */ + private byte[] bytes; + + public static Image ofUrl(String url) { + Image image = new Image(); + image.setUrl(url); + return image; + } + + public static Image ofBytes(byte[] bytes) { + Image image = new Image(); + image.setBytes(bytes); + return image; + } + + public String getB64Json() { + return b64Json; + } + + public void setB64Json(String b64Json) { + this.b64Json = b64Json; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public byte[] getBytes() { + return bytes; + } + + public void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + public byte[] readBytes() { + return bytes; + } + + public void writeToFile(File file) { + if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { + throw new IllegalStateException("Can not mkdirs for path: " + file.getParentFile().getAbsolutePath()); + } + if (this.bytes != null && this.bytes.length > 0) { + IOUtil.writeBytes(this.bytes, file); + } else if (this.b64Json != null) { + byte[] bytes = Base64.getDecoder().decode(b64Json); + IOUtil.writeBytes(bytes, file); + } else if (StringUtil.hasText(this.url)) { + byte[] bytes = new HttpClient().getBytes(this.url); + IOUtil.writeBytes(bytes, file); + } + } + + @Override + public String toString() { + return "Image{" + + "b64Json='" + b64Json + '\'' + + ", url='" + url + '\'' + + ", bytes=" + Arrays.toString(bytes) + + '}'; + } +}