zwhui 1 ano atrás
pai
commit
8fcf5d67bf

+ 42 - 8
midjourney/src/main/java/com/yhlxj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -3,8 +3,6 @@ package com.yhlxj.service.midjourney.impl;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
-import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.io.file.FileNameUtil;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.NumberUtil;
 import cn.hutool.http.HttpRequest;
@@ -32,7 +30,6 @@ import com.yhlxj.service.midjourney.MidjourneyAccountService;
 import com.yhlxj.service.midjourney.MidjourneyService;
 import com.yhlxj.service.midjourney.MidjourneyUserSettingsService;
 import com.yhlxj.util.COSUtil;
-import io.undertow.util.FileUtils;
 import lombok.Data;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -52,7 +49,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
-import java.nio.file.DirectoryStream;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -1460,7 +1457,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
                 .stream().map(item -> JSONUtil.toBean((String) item, MidjourneyUserQueue.class)).collect(Collectors.toList());
         list.forEach(item -> {
             try {
-                item.setBase64Array(new JSONArray(encodeImageToBase64("/mnt/picture/"+item.getQueueId()+"."+item.getFileExt())).toString());
+                item.setBase64Array(JSONUtil.toJsonStr(List.of(encodeImageToBase64("/mnt/picture/"+item.getQueueId()+"."+item.getFileExt(),item.getFileExt()))));
                 Long rank = redisService.zRank(RedisService.key.MIDJOURNEY_QUEUE_LIMIT.getName() + userId, item.getQueueId());
                 item.setPosition((rank != null) ? rank + 1 : 1);
             } catch (IOException e) {
@@ -1502,7 +1499,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             return 12;
         } else {
             // 18:00-23:59 多等8分钟
-            return 1;
+            return 8;
         }
     }
 
@@ -1519,6 +1516,43 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         redisService.zRemoveRangeByScore(RedisService.key.MIDJOURNEY_QUEUE_LIMIT.getName() + userId, 0, currentTimeSeconds);
     }
 
+    /**
+     * 将 Base64 字符串内容写入指定的文本文件。
+     * 如果文件已存在,它将被覆盖。
+     *
+     * @param base64Content 要写入的 Base64 字符串。
+     */
+    public static void writeBase64ToFile(String base64Content, Long queueId) {
+        try {
+            Path path = Paths.get("/mnt/picture/" + queueId + ".txt");
+            // 使用 UTF-8 编码将字符串写入文件。
+            Files.writeString(path, base64Content, StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            System.err.println("写入文件时发生 I/O 错误: " + queueId);
+            e.printStackTrace();
+        }
+    }
+    /**
+     * 从指定的文本文件中读取 Base64 字符串。
+     *
+     * @param filePath 源文件的完整路径 (例如, "/path/to/data.txt")。
+     * @return 文件中的字符串内容;如果文件不存在或发生读取错误,则返回 null。
+     */
+    public static String readBase64FromFile(String filePath) {
+        try {
+            Path path = Paths.get(filePath);
+            if (!Files.exists(path)) {
+                System.err.println("文件不存在: " + path);
+                return "";
+            }
+            // 使用 UTF-8 编码从文件读取所有内容到字符串。
+            return Files.readString(path, StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            System.err.println("读取文件时发生 I/O 错误: " + filePath);
+            e.printStackTrace();
+            return "";
+        }
+    }
 
     /**
      * base64图片存本地
@@ -1539,14 +1573,14 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         }
         return null;
     }
-    public static String encodeImageToBase64(String imagePathStr) throws IOException {
+    public static String encodeImageToBase64(String imagePathStr,String fileExt) throws IOException {
         Path imagePath = Paths.get(imagePathStr);
         if (!Files.exists(imagePath)) {
             throw new IOException("文件未找到: " + imagePathStr);
         }
         // 1. 读取文件所有字节
         byte[] imageBytes = Files.readAllBytes(imagePath);
-        return Base64.getEncoder().encodeToString(imageBytes);
+        return "data:image/"+fileExt+";base64,"+Base64.getEncoder().encodeToString(imageBytes);
     }
 
 }