|
@@ -3,6 +3,7 @@ package com.fdkanfang.web.backend;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.bean.copier.CopyOptions;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fdkanfang.common.constant.ConstantFilePath;
|
|
@@ -24,6 +25,7 @@ import com.fdkanfang.web.mq.config.RabbitConfig;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import io.swagger.annotations.*;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.shiro.authz.annotation.Logical;
|
|
|
import org.apache.shiro.authz.annotation.RequiresRoles;
|
|
@@ -35,17 +37,14 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import javax.mail.MessagingException;
|
|
|
import javax.validation.Valid;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -71,6 +70,12 @@ public class HouseController extends BaseController {
|
|
|
@Value("${admin.email.account}")
|
|
|
private String adminEmailAccount;
|
|
|
|
|
|
+ @Value("${oss.image.file.path}")
|
|
|
+ private String ossPath;
|
|
|
+
|
|
|
+ @Value("${oss.query.url}")
|
|
|
+ private String ossQueryUrl;
|
|
|
+
|
|
|
@Autowired
|
|
|
private HouseService2 houseService2;
|
|
|
|
|
@@ -95,6 +100,9 @@ public class HouseController extends BaseController {
|
|
|
@Autowired
|
|
|
private UserService2 userService2;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OssCheckPointUploadUtil ossCheckPointUploadUtil;
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -219,16 +227,9 @@ public class HouseController extends BaseController {
|
|
|
// 封装垂直校验后的图片到信息到oss
|
|
|
ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath);
|
|
|
ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath);
|
|
|
- try {
|
|
|
- ImageResolutionRate tmpResolutionRate = addAndGetResolutionRate(file , directoryName , ossVerticalHighPath , house.getId());
|
|
|
- maxResolutionRate = tmpResolutionRate;
|
|
|
- needSendMqMsg = true;
|
|
|
- } catch (IOException e) {
|
|
|
- needSendMqMsg = false;
|
|
|
- log.error("保存照片到本地和持久化image对象出现异常:{}" , e);
|
|
|
- }
|
|
|
+ needSendMqMsg = true;
|
|
|
}
|
|
|
- insertScene(maxResolutionRate , house);
|
|
|
+ insertScene(param.getImageMaxRate() , house);
|
|
|
if (needSendMqMsg) {
|
|
|
HashMap<Object, Object> mqMap = new HashMap<>();
|
|
|
mqMap.put("ossImageHigh", ossVerticalImageHighMap);
|
|
@@ -257,11 +258,40 @@ public class HouseController extends BaseController {
|
|
|
}
|
|
|
return new R(MsgCode.SUCCESS_CODE, house);
|
|
|
}
|
|
|
+
|
|
|
+ @ApiOperation("上传照片")
|
|
|
+ @PostMapping(value = "uploadImages")
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void insertScene(ImageResolutionRate maxResolutionRate , HouseEntity house){
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "file", value = "文件", paramType = "query", required = true, dataType = "List"),
|
|
|
+ @ApiImplicitParam(name = "sceneCode", value = "场景码", paramType = "query", required = true, dataType = "String"),
|
|
|
+ @ApiImplicitParam(name = "houseId", value = "房间ID", paramType = "query", required = true, dataType = "Long")
|
|
|
+ })
|
|
|
+ public Result uploadImages(@RequestParam(name = "file") CommonsMultipartFile file ,
|
|
|
+ @RequestParam(name = "sceneCode") String sceneCode,
|
|
|
+ @RequestParam(name = "houseId") Long houseId){
|
|
|
+ Map<String, Object> imageResult = new HashMap<>();
|
|
|
+ if(null != file){
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ filename = checkAndChangeFileName(filename);
|
|
|
+ String directoryName = OUTPATH + sceneCode + File.separator + "input_img"+ File.separator;
|
|
|
+ String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ filename;
|
|
|
+ try {
|
|
|
+ imageResult = addAndGetResolutionRate(file , filename , directoryName ,
|
|
|
+ ossVerticalHighPath , houseId);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("保存照片到本地和持久化image对象出现异常:{}" , e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.success(imageResult);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void insertScene(String maxResolutionRate , HouseEntity house){
|
|
|
SceneProEntity sceneProEntity = new SceneProEntity();
|
|
|
SceneProEditEntity sceneProEditEntity = new SceneProEditEntity();
|
|
|
- if(ImageResolutionRate.TWO_K.equals(maxResolutionRate)){
|
|
|
+ if(ImageResolutionRate.TWO_K.name().equals(maxResolutionRate)){
|
|
|
//2k
|
|
|
sceneProEntity.setSceneScheme(11);
|
|
|
}else{
|
|
@@ -286,18 +316,22 @@ public class HouseController extends BaseController {
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String directoryName ,
|
|
|
- String ossVerticalHighPath , Long houseId) throws IOException {
|
|
|
+// public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String filename,String directoryName ,
|
|
|
+ public Map<String ,Object> addAndGetResolutionRate(CommonsMultipartFile file , String filename,
|
|
|
+ String directoryName ,
|
|
|
+ String ossVerticalHighPath ,
|
|
|
+ Long houseId) throws IOException {
|
|
|
if(null == file || null == houseId){
|
|
|
log.error("房源id或者文件为空,无法生成image记录");
|
|
|
- return null;
|
|
|
+ return new HashMap<>();
|
|
|
}
|
|
|
- String filename = file.getOriginalFilename();
|
|
|
- filename = checkAndChangeFileName(filename);
|
|
|
-
|
|
|
// 获取图片房间类型
|
|
|
String imageType = StringUtils.substringBefore(filename, "_");
|
|
|
|
|
|
+ File dir = new File(directoryName);
|
|
|
+ if(!dir.exists()){
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
// 本地图片保存位置: /root/data/kanfang/场景码/input_img/xxxx.jpg
|
|
|
String fileFullPath = directoryName + filename;
|
|
|
ImageEntity image = new ImageEntity();
|
|
@@ -312,16 +346,53 @@ public class HouseController extends BaseController {
|
|
|
image.setFloor(1);
|
|
|
image.setType(imageType);
|
|
|
//将图片保存到本地指定目录filePath eg: /root/data/kanfang/d_9iRDUgn3l/input_img/xxx.jpg
|
|
|
- int resolutionRate = FileUtils.downloanAndGetResolutionRate(file.getInputStream(), fileFullPath , true);
|
|
|
+ saveFileToLocal(file , fileFullPath);
|
|
|
+ File localFile = new File(fileFullPath);
|
|
|
+ //生成缩略图,并上传到oss
|
|
|
+ String scalImageFullPath = directoryName + "scal_" + filename;
|
|
|
+ Thumbnails.of(localFile).size(128 , 256).outputQuality(0.8f).toFile(scalImageFullPath);
|
|
|
+ String imageOssPath = ossPath + file.getOriginalFilename();
|
|
|
+ log.info("生成的照片上传oss的路径:{}" , imageOssPath);
|
|
|
+ ossCheckPointUploadUtil.upload2(scalImageFullPath, imageOssPath);
|
|
|
+ String imageTotalOssUrl = ossQueryUrl + imageOssPath;
|
|
|
//照片分辨率
|
|
|
- ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(resolutionRate);
|
|
|
- log.info("照片{}的像素为:{}" , fileFullPath , resolutionRate);
|
|
|
+ int totalResolutinRate = getImageResolutinoRate(localFile);
|
|
|
+ ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(totalResolutinRate);
|
|
|
+ log.info("照片{}的像素为:{}" , fileFullPath , totalResolutinRate);
|
|
|
image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
|
|
|
int insert = imageService2.save(image);
|
|
|
if(insert != 1){
|
|
|
throw new CommonBaseException(ResultCodeEnum.D101, "新增image记录失败");
|
|
|
}
|
|
|
- return maxResolutionRate;
|
|
|
+ Map<String , Object> result = new HashMap<>();
|
|
|
+ result.put("rate" , maxResolutionRate.name());
|
|
|
+ result.put("ossUrl" , imageTotalOssUrl);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getImageResolutinoRate(File localFile) throws IOException {
|
|
|
+ if(null != localFile){
|
|
|
+ int totalResolutinRate = -1;
|
|
|
+ SimpleImageInfo simpleImageInfo = new SimpleImageInfo(localFile);
|
|
|
+ if(null != simpleImageInfo){
|
|
|
+ if(simpleImageInfo.getWidth() < simpleImageInfo.getHeight()){
|
|
|
+ totalResolutinRate = simpleImageInfo.getHeight();
|
|
|
+ }else{
|
|
|
+ totalResolutinRate = simpleImageInfo.getWidth();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return totalResolutinRate;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveFileToLocal(CommonsMultipartFile file , String localFullPath) throws IOException {
|
|
|
+
|
|
|
+ if(StringUtils.isBlank(localFullPath) || null == file){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ File newFile = new File(localFullPath);
|
|
|
+ file.transferTo(newFile);
|
|
|
}
|
|
|
|
|
|
private String checkAndChangeFileName(String filename){
|