Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
shahaibo
2023-11-20 15:06:29 +08:00
7 changed files with 118 additions and 42 deletions

View File

@@ -67,7 +67,7 @@ public class ElementController {
@ApiOperation(value = "生成印花")
@PostMapping("/generatePrint")
public Response<CollectionGeneratePrintVO> generatePrint(@Valid @RequestBody CollectionGeneratePrintDTO generatePrintDTO) {
public Response<GenerateCollectionItemVO> generatePrint(@Valid @RequestBody CollectionGeneratePrintDTO generatePrintDTO) {
return Response.success(collectionElementService.generatePrint(generatePrintDTO));
}

View File

@@ -16,4 +16,14 @@ public class GenerateCollectionItemVO {
@ApiModelProperty("生成的图片是否已经被like")
private Boolean isLiked = Boolean.FALSE;
public GenerateCollectionItemVO() {
}
public GenerateCollectionItemVO(Long generateItemId, String generateItemUrl, Boolean isLiked) {
this.generateItemId = generateItemId;
this.generateItemUrl = generateItemUrl;
this.isLiked = isLiked;
}
}

View File

@@ -62,9 +62,9 @@ public class PythonService {
* @param printPath
* @return
*/
public String generatePrint(List<String> printPath) {
public String generatePrint(List<String> printPath, Long userId) {
//限流校验
AccessLimitUtils.validate("generatePrint", 2);
// AccessLimitUtils.validate("generatePrint", 2);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
@@ -75,33 +75,48 @@ public class PythonService {
Map<String, String> content = Maps.newHashMap();
content.put("fusion_content_path", printPath.get(0));
content.put("fusion_style_path", printPath.get(1));
content.put("fusion_output_path", getPythonOutputPath(
printPath.get(0), PythonToJavaApiOperationTypeEnum.GENERATE_PRINT));
content.put("userid", String.valueOf(userId));
// content.put("fusion_output_path", getPythonOutputPath(
// printPath.get(0), PythonToJavaApiOperationTypeEnum.GENERATE_PRINT));
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":9999/aifda/api/v1.0/fusion_test")
// .url(accessPythonIp + ":9999/aifda/api/v1.0/fusion_test")
.url(accessPythonIp + ":9991/api/image_fusion")
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
Response response;
String responseBody;
try {
log.info("生成印花请求入参content###{}", JSON.toJSONString(content));
log.info("moodboard与printboard图片合成 入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##generatePrint异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("generate.interface.exception");
log.error("PythonService## moodboard与printboard图片合成异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("image.synthesis.failed");
}
//去除限流
AccessLimitUtils.validateOut("generatePrint");
// AccessLimitUtils.validateOut("generatePrint");
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response));
Boolean result = jsonObject.getBoolean("successful");
if (result) {
return content.get("fusion_output_path");
if (jsonObject.getBoolean("successful")) {
try {
if (Objects.nonNull(response.body())) {
responseBody = response.body().string();
JSONObject responseObj = JSON.parseObject(responseBody);
log.info("moodboard与printboard图片合成 python返回###{}",responseObj);
return responseObj.get("data").toString();
}
log.info("生成印花失败###{}", jsonObject);
} catch (IOException | JSONException e) {
log.error("Python moodboard与printboard图片合成异常 {}", e.getMessage());
throw new BusinessException("image.synthesis.failed");
}
log.error("moodboard与printboard图片合成异常###{}", jsonObject);
throw new BusinessException("image.synthesis.failed");
// return content.get("fusion_output_path");
}
log.info("moodboard与printboard图片合成异常###{}", jsonObject);
//生成失败
throw new BusinessException("generate.interface.exception");
throw new BusinessException("image.synthesis.failed");
}
/**
@@ -2240,15 +2255,6 @@ public class PythonService {
log.info("generateSketchOrPrint请求入参content###{}", JSON.toJSONString(content, SerializerFeature.WriteMapNullValue));
response = client.newCall(request).execute();
bodyString = response.body().string();
// bodyString = "{\n" +
// " \"code\": 200,\n" +
// " \"data\": {\n" +
// " \"list\": [\n" +
// " \"aida-users/12/print_1695088687_0.png\"\n" +
// " ]\n" +
// " },\n" +
// " \"msg\": \"OK!\"\n" +
// "}";
} catch (IOException ioException) {
log.error("PythonService##generateSketchOrPrint异常###{}", ExceptionUtil.getThrowableList(ioException));
}

View File

@@ -46,7 +46,7 @@ public interface CollectionElementService extends IService<CollectionElement> {
* @param generatePrintDTO
* @return
*/
CollectionGeneratePrintVO generatePrint(CollectionGeneratePrintDTO generatePrintDTO);
GenerateCollectionItemVO generatePrint(CollectionGeneratePrintDTO generatePrintDTO);
/**
* 保存印花图片

View File

@@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import io.minio.errors.MinioException;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
@@ -33,6 +34,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@@ -173,7 +175,8 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
}
@Override
public CollectionGeneratePrintVO generatePrint(CollectionGeneratePrintDTO generatePrintDTO) {
public GenerateCollectionItemVO generatePrint(CollectionGeneratePrintDTO generatePrintDTO) {
Long userId = UserContext.getUserHolder().getId();
String url1 = null;
String url2 = null;
CollectionElement element1 = selectById(generatePrintDTO.getSelect1Id());
@@ -198,19 +201,30 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
}
List<String> printPath = Arrays.asList(url1, url2);
//调取python 接口
String generateUrl = pythonService.generatePrint(printPath);
String generateUrl = pythonService.generatePrint(printPath,userId);
if (StringUtils.isEmpty(generateUrl)) {
throw new BusinessException("generate.interface.exception");
}
//用户信息
AuthPrincipalVo userInfo = UserContext.getUserHolder();
CollectionElement element = resolveData(generateUrl, generatePrintDTO.getTimeZone(), userInfo);
if (!this.save(element)) {
throw new BusinessException("save.collectionElement.failed");
}
CollectionGeneratePrintVO collectionGeneratePrint = CopyUtil.copyObject(element, CollectionGeneratePrintVO.class);
collectionGeneratePrint.setDesignType(DesignTypeEnum.COLLECTION.getRealName());
return collectionGeneratePrint;
// 保存合成信息到generate表
Generate generate = setGenerate(userId, generatePrintDTO.getTimeZone());
generateService.save(generate);
// 保存合成后的信息到generateDetail
GenerateDetail generateDetail = setGenerateDetail(generate.getId(), generateUrl, generatePrintDTO.getTimeZone());
generateDetailMapper.insert(generateDetail);
// CollectionElement element = resolveData(generateUrl, generatePrintDTO.getTimeZone(), userId);
// if (!this.save(element)) {
// throw new BusinessException("save.collectionElement.failed");
// }
// CollectionGeneratePrintVO collectionGeneratePrint = CopyUtil.copyObject(element, CollectionGeneratePrintVO.class);
// collectionGeneratePrint.setUrl(minioUtil.getPresignedUrl(generateUrl, 24 * 60));
// collectionGeneratePrint.setDesignType(DesignTypeEnum.COLLECTION.getRealName());
// return collectionGeneratePrint;
return new GenerateCollectionItemVO(generateDetail.getId(),
minioUtil.getPresignedUrl(generateUrl, 24 * 60),
generateDetail.getIsLike().equals((byte) 0) ? Boolean.FALSE : Boolean.TRUE);
}
@Override
@@ -284,16 +298,23 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
return Boolean.TRUE;
}
private CollectionElement resolveData(String path, String timeZone, AuthPrincipalVo userInfo) {
File file = new File(path);
private CollectionElement resolveData(String path, String timeZone, Long userId){
// File file = new File(path);
String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
CollectionElement element = new CollectionElement();
element.setAccountId(userInfo.getId());
element.setAccountId(userId);
element.setCollectionId(0L);
element.setName(file.getName());
element.setName(name);
element.setLevel1Type(CollectionLevel1TypeEnum.PRINT_BOARD.getRealName());
element.setUrl(path);
element.setHasPin((byte) 0);
element.setMd5(MD5Utils.encryptFile(path, Boolean.FALSE));
try {
element.setMd5(MD5Utils.encryptFile(minioUtil.download(path)));
} catch (MinioException | IOException e) {
throw new RuntimeException(e);
}
//按时区计算
element.setCreateDate(DateUtil.getByTimeZone(timeZone));
return element;
@@ -774,4 +795,38 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
}
return collectionElement;
}
private Generate setGenerate(Long userId,String timeZone){
Generate generate = new Generate();
generate.setAccountId(userId);
generate.setLevel1Type(CollectionLevel1TypeEnum.PRINT_BOARD.getRealName());
generate.setGenerateType("synthesis");
generate.setModelName("Image Synthesis Model");
generate.setCreateDate(DateUtil.getByTimeZone(timeZone));
return generate;
}
private GenerateDetail setGenerateDetail(Long generateId, String url, String timeZone){
GenerateDetail generateDetail = new GenerateDetail();
generateDetail.setGenerateId(generateId);
generateDetail.setUrl(url);
String md5;
try {
md5 = MD5Utils.encryptFile(minioUtil.download(url));
} catch (MinioException | IOException e) {
throw new RuntimeException(e);
}
// 通过MD5来确认当前图片是否有被like过避免重复like
List<Map<String, Long>> libraryIds = generateDetailMapper.getLibraryIdThroughMD5(md5, CollectionLevel1TypeEnum.PRINT_BOARD.getRealName());
if (libraryIds.isEmpty()){
generateDetail.setIsLike((byte) 0);
}else {
generateDetail.setIsLike((byte) 1);
generateDetail.setLibraryId(libraryIds.get(0).get("library_id"));
}
generateDetail.setMd5(md5);
generateDetail.setCreateDate(DateUtil.getByTimeZone(timeZone));
return generateDetail;
}
}

View File

@@ -185,11 +185,15 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// 2、将like的图片信息存入library
// 2.1、不能重复喜欢
// 2.1.1 判断该图片是否被喜欢过
Library libraryDetail = libraryService.getById(generateDetail.getLibraryId());
if ( (Objects.nonNull(generateDetail.getLibraryId()) && !generateDetail.getLibraryId().equals(0L))
|| Objects.nonNull(libraryDetail)){
throw new BusinessException("duplicate.likes.are.not.allowed");
}
// todo 2.1.2、判断library中是否有相同MD5的图片
// 2.2、添加到library
AuthPrincipalVo userInfo = UserContext.getUserHolder();
Long accountId = userInfo.getId();

View File

@@ -128,6 +128,7 @@ layers.does.not.exists=layers does not exists.
unknown.generate.type=unknown generate type.
the.workspace.lastIndex.not.found=The workspace lastIndex not found.
gender.cannot.be.empty=gender cannot be empty.
image.synthesis.failed=Image synthesis failed.
# 可能会报异常
# Informative: