Merge branch 'dev/dev' into release/3.0

This commit is contained in:
2024-12-02 14:54:00 +08:00
9 changed files with 314 additions and 62 deletions

View File

@@ -484,6 +484,57 @@ public class MinioUtil {
return null; // or throw an exception
}
}
/**
* 从 MinIO 下载对象到本地路径
*
* @param bucketName 存储桶名称
* @param objectName MinIO 上对象的名称
* @param localFilePath 本地文件路径
*/
public void downloadMinioObjectToLocal(String bucketName, String objectName, String localFilePath) {
File localFile = new File(localFilePath);
File parentDir = localFile.getParentFile();
if (parentDir != null) {
parentDir.mkdirs(); // 创建文件夹,确保路径结构与 MinIO 一致
}
try (InputStream stream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
FileOutputStream out = new FileOutputStream(localFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
log.info("Downloaded object {} to {}", objectName, localFilePath);
} catch (Exception e) {
log.error("Error while downloading object {}: {}", objectName, e.getMessage());
}
}
/**
* 从路径中提取存储桶名称
*
* @param path MinIO 路径
* @return 存储桶名称
*/
public String getBucketNameFromPath(String path) {
int index = path.indexOf("/");
return path.substring(0, index); // 获取第一级路径作为 bucket 名称
}
/**
* 从路径中提取对象名称
*
* @param path MinIO 路径
* @return 对象名称
*/
public String getObjectNameFromPath(String path) {
int index = path.indexOf("/");
return path.substring(index + 1); // 获取路径的其余部分作为对象名称
}
}

View File

@@ -389,20 +389,20 @@ public class SendEmailUtil {
// 根据邮件类型设置不同的主题和模板
String subject = "";
Template template = new Template();
if (type == 1) {
subject = "Upcoming System Upgrade for AiDA 3.0";
template.setTemplateID(UPGRADE_NOTIFICATION_ID);
}else {
subject = "即将到来的AiDA 3.0系统升级";
template.setTemplateID(UPGRADE_NOTIFICATION_ID_CHINESE);
}
// if (type == 1) {
// subject = "Successful System Upgrade and New Features in AiDA 3.1";
// template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID);
// subject = "Upcoming System Upgrade for AiDA 3.0";
// template.setTemplateID(UPGRADE_NOTIFICATION_ID);
// }else {
// subject = "系统升级成功和AiDA 3.1新功能";
// template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID_CHINESE);
// subject = "即将到来的AiDA 3.0系统升级";
// template.setTemplateID(UPGRADE_NOTIFICATION_ID_CHINESE);
// }
if (type == 1) {
subject = "Successful System Upgrade and New Features in AiDA 3.1";
template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID);
}else {
subject = "系统升级成功和AiDA 3.1新功能";
template.setTemplateID(UPGRADE_SUCCESS_NOTIFICATION_ID_CHINESE);
}
template.setTemplateData(buildAccountData(account));
req.setSubject(subject);
@@ -759,6 +759,47 @@ public class SendEmailUtil {
req.setSubject(subject);
req.setTemplate(template);
// 发送邮件
SendEmailResponse resp = client.SendEmail(req);
log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
log.info("邮件发送失败###{}", e.toString());
throw new BusinessException("failed.to.send.mail");
}
}
private final static Long SYSTEM_UPGRADE_CN_ID = 131743L;
private final static Long SYSTEM_UPGRADE_EN_ID = 131744L;
public static void temporaryUpgrade(Account account, String senderAddress, int type) {
try {
// 实例化一个认证对象
Credential cred = new Credential(SECRET_ID, SECRET_KEy);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ses.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SesClient client = new SesClient(cred, "ap-hongkong", clientProfile);
SendEmailRequest req = new SendEmailRequest();
if (StringUtils.isEmpty(senderAddress)) {
senderAddress = CODE_CREATE_SEND_ADDRESS;
}
req.setFromEmailAddress(senderAddress);
req.setDestination(new String[]{account.getUserEmail()});
// 根据邮件类型设置不同的主题和模板
String subject = "";
Template template = new Template();
if (type == 1) {
subject = "AiDA system upgrade completed";
template.setTemplateID(SYSTEM_UPGRADE_EN_ID);
}else {
subject = "AiDA系统升级完成";
template.setTemplateID(SYSTEM_UPGRADE_CN_ID);
}
template.setTemplateData(buildAccountData(account));
req.setSubject(subject);
req.setTemplate(template);
// 发送邮件
SendEmailResponse resp = client.SendEmail(req);
log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp));

View File

@@ -261,5 +261,12 @@ public class AccountController {
return Response.success(true);
}
@PostMapping("temporaryUpgrade")
@ApiOperation(value = "临时升级")
public Response<Boolean> temporaryUpgrade() {
accountService.temporaryUpgrade();
return Response.success(true);
}
}

View File

@@ -245,4 +245,10 @@ public class SavedCollectionController {
public Response<String> likeHistoryRelSketch() {
return Response.success(userLikeGroupService.likeHistoryRelSketch());
}
@ApiOperation(value = "download")
@PostMapping("/download")
public Response<String> download() {
return Response.success(userLikeGroupService.download());
}
}

View File

@@ -186,4 +186,6 @@ public interface AccountService extends IService<Account> {
String googleCallback(String code, HttpSession session);
List<String> getPaidCustomerEmail();
void temporaryUpgrade();
}

View File

@@ -62,4 +62,6 @@ public interface UserLikeGroupService extends IService<UserLikeGroup> {
List<MagicToolResultVO> getRelightResult(List<String> taskIdList);
String likeHistoryRelSketch();
String download();
}

View File

@@ -1961,4 +1961,30 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// return userRepository.save(newUser);
// }
@Override
public void temporaryUpgrade() {
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().ne(Account::getSystemUser, 0);
queryWrapper.lambda().isNotNull(Account::getUserEmail);
// queryWrapper.lambda().eq(Account::getId, 6);
List<Account> accountList = accountMapper.selectList(queryWrapper);
int i = 0;
for (Account account : accountList) {
if (i >= 0) {
try {
// 判断用户语言,调用相应邮件发送方法
if (Language.CHINESE_SIMPLIFIED.name().equals(account.getLanguage())) {
SendEmailUtil.temporaryUpgrade(account, null, 0);
} else {
SendEmailUtil.temporaryUpgrade(account, null, 1);
}
} catch (Exception e) {
// 捕获单个用户的发送失败异常,记录日志但不中断流程
log.error("邮件发送失败,用户邮箱:{},原因:{}", account.getUserEmail(), e.getMessage(), e);
}
}
i ++;
}
}
}

View File

@@ -508,6 +508,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// 校验后获取
generateThroughImageTextDTO.setGenerateType(generate.getGenerateType());
creditsEventsEnum = CreditsEventsEnum.PATTERN;
// 模型迁移SD1.? -> flux,从而产生了不同模型的选择,
// high -> 生成图片质量高,但生成速度慢,每次生成只返回一张图片
// fast -> 生成图片质量低,但生成速度快,每次生成返回四张图片
if (!StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) && generateThroughImageTextDTO.getVersion().equals("high")){
times = 1;
}
}
// Slogan 参数校验
if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.SLOGAN.getRealName())) {
@@ -564,8 +571,14 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
} else if (generateThroughImageTextDTO.getLevel1Type().equals(MOOD_BOARD.getRealName())) {
creditsEventsEnum = CreditsEventsEnum.MOOD_BOARD;
if (!StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) && generateThroughImageTextDTO.getVersion().equals("high")){
times = 1;
}
} else if (generateThroughImageTextDTO.getLevel1Type().equals(SKETCH_BOARD.getRealName())) {
creditsEventsEnum = CreditsEventsEnum.SKETCH_BOARD;
if (!StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) && generateThroughImageTextDTO.getVersion().equals("high")){
times = 1;
}
}
// 2、判断用户当前积分是否够本次生成消耗
@@ -577,16 +590,6 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// 3、生成唯一id 使用uuid,由于uuid重复的几率很小故取消对uuid重复性的校验
String uuid = UUID.randomUUID().toString();
// 模型迁移SD1.? -> flux,从而产生了不同模型的选择,
// high -> 生成图片质量高,但生成速度慢,每次生成只返回一张图片
// fast -> 生成图片质量低,但生成速度快,每次生成返回四张图片
if (!StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) && generateThroughImageTextDTO.getVersion().equals("high")){
times = 1;
}else if (!StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) && generateThroughImageTextDTO.getVersion().equals("fast")){
times = 4;
}
// 除了 Moodboard || Printboard->Pattern(可以区分三种风格) || Sketchboard(Generate Sketch)这三个地方需要区分high || fast之外其他地方保持原样
if (generateThroughImageTextDTO.getLevel1Type().equals("Printboard") && !generateThroughImageTextDTO.getLevel2Type().equals("Pattern")){
generateThroughImageTextDTO.setVersion(null);

View File

@@ -15,8 +15,10 @@ import com.ai.da.model.dto.ToProductImageDTO;
import com.ai.da.model.vo.*;
import com.ai.da.python.PythonService;
import com.ai.da.service.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -30,13 +32,14 @@ import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static cn.hutool.poi.excel.sax.AttributeName.s;
/**
* 服务实现类
*
@@ -258,25 +261,47 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
int i = 0;
// 翻译
String prompt = toProductImageDTO.getPrompt();
String s = "";
StringBuilder sb = new StringBuilder("The best quality, masterpiece, real image.");
if (!StringUtil.isNullOrEmpty(prompt)) {
s = pythonService.promptTranslate(prompt);
}else {
s = "best quality, masterpiece. detailed, high-res, simple background, studio photography, extremely detailed, updo, detailed face, face, close-up, HDR, UHD, 8K realistic, Highly detailed, simple background, Studio lighting";
prompt = pythonService.promptTranslate(prompt);
}
// else {
// s = "best quality, masterpiece. detailed, high-res, simple background, studio photography, extremely detailed, updo, detailed face, face, close-up, HDR, UHD, 8K realistic, Highly detailed, simple background, Studio lighting";
// }
for (ToProductImageVO toProductImageVO : toProductImageDTO.getToProductImageVOList()) {
String taskId;
if (toProductImageVO.getElementType().equals("DesignOutfit")) {
taskId = UUID.randomUUID() + "-" + i + "-" + userHolder.getId();
TDesignPythonOutfit tDesignPythonOutfit = designPythonOutfitMapper.selectById(toProductImageVO.getElementId());
Long designItemId = tDesignPythonOutfit.getDesignItemId();
QueryWrapper<DesignItemDetail> designItemDetailQueryWrapper = new QueryWrapper<>();
designItemDetailQueryWrapper.lambda().eq(DesignItemDetail::getDesignItemId, designItemId);
designItemDetailQueryWrapper.lambda().ne(DesignItemDetail::getType, "Body");
List<DesignItemDetail> designItemDetails = designItemDetailMapper.selectList(designItemDetailQueryWrapper);
String collect = designItemDetails.stream().map(DesignItemDetail::getType).collect(Collectors.joining(","));
Long designId = tDesignPythonOutfit.getDesignId();
Design design = designMapper.selectById(designId);
String productType = "overall";
if (design.getSingleOverall().equals("single")) {
productType = "single";
sb.append(collect);
}else {
if (collect.contains("Tops")) {
sb.append("a handsome man,");
}else {
sb.append("a beautiful women,");
}
sb.append("wearing ").append(collect);
}
if (StringUtils.isEmpty(prompt)) {
sb.append(",8K realistic,HDR");
}else {
sb.append(",").append(prompt).append(",8K realistic,HDR");
}
// 走模型
pythonService.toProductImage(tDesignPythonOutfit.getDesignUrl(), taskId, s, toProductImageDTO.getImageStrength(), productType);
pythonService.toProductImage(tDesignPythonOutfit.getDesignUrl(), taskId, sb.toString(), toProductImageDTO.getImageStrength(), productType);
ToProductImageResult toProductImageResult = new ToProductImageResult();
toProductImageResult.setElementId(tDesignPythonOutfit.getId());
toProductImageResult.setElementType("DesignOutfit");
@@ -290,10 +315,16 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
// toProductImageResult.setUrl(minioUtil.getPresignedUrl(toProductImageResult.getUrl(), 24 * 60));
result.add(toProductImageResult);
}else {
if (StringUtils.isEmpty(prompt)) {
sb.append(",8K realistic,HDR");
}else {
sb.append(",").append(prompt).append(",8K realistic,HDR");
}
taskId = UUID.randomUUID() + "-" + i + "-" + userHolder.getId();
ToProductElement toProductElement = toProductElementMapper.selectById(toProductImageVO.getElementId());
// 走模型
pythonService.toProductImage(toProductElement.getUrl(), taskId, s, toProductImageDTO.getImageStrength(), "overall");
pythonService.toProductImage(toProductElement.getUrl(), taskId, sb.toString(), toProductImageDTO.getImageStrength(), "overall");
ToProductImageResult toProductImageResult = new ToProductImageResult();
toProductImageResult.setElementId(toProductElement.getId());
toProductImageResult.setElementType("ProductElement");
@@ -611,67 +642,150 @@ public class UserLikeGroupServiceImpl extends ServiceImpl<UserLikeGroupMapper, U
public String likeHistoryRelSketch() {
QueryWrapper<UserLikeGroup> qw = new QueryWrapper<>();
List<UserLikeGroup> userLikeGroups = userLikeGroupMapper.selectList(qw);
Map<Long, List<List<Map<String, Object>>>> result = new HashMap<>();
List<List<Map<String, Object>>> result = new ArrayList<>();
for (UserLikeGroup userLikeGroup : userLikeGroups) {
Long accountId = userLikeGroup.getAccountId();
Long collectionId = userLikeGroup.getCollectionId();
QueryWrapper<CollectionElement> collectionElementQueryWrapper = new QueryWrapper<>();
collectionElementQueryWrapper.lambda().eq(CollectionElement::getCollectionId, collectionId);
collectionElementQueryWrapper.lambda().eq(CollectionElement::getLevel1Type, "Sketchboard");
List<CollectionElement> collectionElements = collectionElementMapper.selectList(collectionElementQueryWrapper);
List<String> urlList = collectionElements.stream().map(CollectionElement::getUrl).collect(Collectors.toList());
// 提前转换为Set以提高contains的效率
List<CollectionElement> collectionElements = getCollectionElementsByCollectionId(collectionId);
Set<String> urlSet = collectionElements.stream()
.map(CollectionElement::getUrl)
.collect(Collectors.toSet());
QueryWrapper<UserLike> userLikeQueryWrapper = new QueryWrapper<>();
userLikeQueryWrapper.lambda().eq(UserLike::getUserLikeGroupId, userLikeGroup.getId());
List<UserLike> userLikes = userLikeMapper.selectList(userLikeQueryWrapper);
List<Map<String, Object>> list = new ArrayList<>();
List<UserLike> userLikes = getUserLikesByGroupId(userLikeGroup.getId());
for (UserLike userLike : userLikes) {
QueryWrapper<DesignItemDetail> designItemDetailQueryWrapper = new QueryWrapper<>();
designItemDetailQueryWrapper.lambda().eq(DesignItemDetail::getDesignItemId, userLike.getDesignItemId());
designItemDetailQueryWrapper.lambda().ne(DesignItemDetail::getType, "Body");
List<DesignItemDetail> designItemDetails = designItemDetailMapper.selectList(designItemDetailQueryWrapper);
Map<String, Object> sketch = new HashMap<>();
List<Map<String, Object>> userLikeSketchList = new ArrayList<>();
List<DesignItemDetail> designItemDetails = getDesignItemDetails(userLike.getDesignItemId());
for (DesignItemDetail designItemDetail : designItemDetails) {
Map<String, Object> sketch = new HashMap<>();
String path = designItemDetail.getPath();
// 下载路径到本地
// if (!StringUtils.isEmpty(path)) {
// String bucketName = minioUtil.getBucketNameFromPath(path);
// String objectName = minioUtil.getObjectNameFromPath(path);
// String localBasePath = "C:\\workspace\\fileData\\minio";
// String localFilePath = Paths.get(localBasePath, path).toString();
//
// // 检查文件是否已经存在,避免重复下载
// File localFile = new File(localFilePath);
// if (!localFile.exists()) {
// minioUtil.downloadMinioObjectToLocal(bucketName, objectName, localFilePath);
// } else {
// log.info("File already exists, skipping download: {}", localFilePath);
// }
// }
Long designId = designItemDetail.getDesignId();
Design design = designMapper.selectById(designId);
if (null == design) {
continue;
}
if (design.getSingleOverall().equals("single")) {
continue;
}
if (design.getModelType().equals("System")) {
SysFile sysFile = sysFileMapper.selectById(design.getTemplateId());
if (null == sysFile) {
continue;
}
sketch.put("sex", sysFile.getLevel2Type().equals("Male") ? "Male" : "Female");
} else {
Library library = libraryMapper.selectById(design.getTemplateId());
if (null == library) {
continue;
}
sketch.put("sex", library.getLevel2Type().equals("Female") ? "Female" : "Male");
}
sketch.put("type", designItemDetail.getType());
sketch.put("path", path);
if (urlList.contains(path)) {
sketch.put("beSelected", true);
}else {
sketch.put("beSelected", false);
}
sketch.put("beSelected", urlSet.contains(path));
sketch.put("accountId", accountId);
if (path.contains("aida-sys-image/images/")) {
String searchPath = new String(path);
String replace = searchPath.replace("aida-sys-image/images/", "");
String replace = path.replace("aida-sys-image/images/", "");
String style = getStyleByUrl(replace);
if (!StringUtils.isEmpty(style)) {
sketch.put("style", style);
}
}
userLikeSketchList.add(sketch);
}
list.add(sketch);
}
if (result.containsKey(accountId)) {
result.get(accountId).add(list);
}else {
List<List<Map<String, Object>>> lists = new ArrayList<>();
lists.add(list);
result.put(accountId, lists);
if (CollectionUtil.isNotEmpty(userLikeSketchList)) {
result.add(userLikeSketchList);
}
}
System.out.println(JSONObject.toJSONString(result));
}
// 将结果以美观的JSON形式保存到文件中
saveResultAsPrettyJson(result, "C:\\Users\\10233\\Desktop\\result.json");
return null;
}
@Override
public String download() {
// 下载 aida-collection-element bucket 下所有 Sketchboard 文件夹内容
// String sketchboardLocalPath = "C:\\workspace\\fileData\\minio";
// minioUtil.downloadSketchboardDirectories(sketchboardLocalPath);
//
// // 下载整个 aida-users bucket
// String aidaUsersLocalPath = "C:\\workspace\\fileData\\minio";
// minioUtil.downloadEntireBucket("aida-users", aidaUsersLocalPath);
return null;
}
// 提取公共方法
private List<CollectionElement> getCollectionElementsByCollectionId(Long collectionId) {
QueryWrapper<CollectionElement> qw = new QueryWrapper<>();
qw.lambda().eq(CollectionElement::getCollectionId, collectionId)
.eq(CollectionElement::getLevel1Type, "Sketchboard");
return collectionElementMapper.selectList(qw);
}
private List<UserLike> getUserLikesByGroupId(Long groupId) {
QueryWrapper<UserLike> qw = new QueryWrapper<>();
qw.lambda().eq(UserLike::getUserLikeGroupId, groupId);
return userLikeMapper.selectList(qw);
}
private List<DesignItemDetail> getDesignItemDetails(Long designItemId) {
QueryWrapper<DesignItemDetail> qw = new QueryWrapper<>();
qw.lambda().eq(DesignItemDetail::getDesignItemId, designItemId)
.ne(DesignItemDetail::getType, "Body");
return designItemDetailMapper.selectList(qw);
}
private void saveResultAsPrettyJson(List<List<Map<String, Object>>> result, String filePath) {
// 使用一个 Map<String, Object> 将结果存储为字符串键
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("data", result);
try (FileWriter file = new FileWriter(filePath)) {
// 使用 JSONObject 将转换后的 Map 转为 JSON
JSONObject jsonObject = new JSONObject(resultMap);
// 格式化输出为美观的 JSON
file.write(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
} catch (IOException e) {
e.printStackTrace();
}
}
private String getStyleByUrl(String replace) {
String[] split = replace.split("/");
String tableName = getTableName(split);
if (StringUtils.isEmpty(tableName)) {
return null;
}else {
replace = split[1] + "/" + split[2];
return attributeRetrievalMapper.getStyleByUrl(replace, tableName);
}
}