|
@@ -3,7 +3,6 @@ package com.fdkanfang.web.backend;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.copier.CopyOptions;
|
|
import cn.hutool.core.bean.copier.CopyOptions;
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
-import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.fdkanfang.common.constant.ConstantFilePath;
|
|
import com.fdkanfang.common.constant.ConstantFilePath;
|
|
@@ -35,9 +34,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
-import org.springframework.util.FileCopyUtils;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import javax.mail.MessagingException;
|
|
import javax.mail.MessagingException;
|
|
@@ -215,76 +212,77 @@ public class HouseController extends BaseController {
|
|
String imageHighPath = null;
|
|
String imageHighPath = null;
|
|
String imageLowPath = null;
|
|
String imageLowPath = null;
|
|
for (String filename : fileNames) {
|
|
for (String filename : fileNames) {
|
|
-
|
|
|
|
-// filename = checkAndChangeFileName(filename);
|
|
|
|
JSONObject jsonObject = JSON.parseObject(filename);
|
|
JSONObject jsonObject = JSON.parseObject(filename);
|
|
if(null == jsonObject){
|
|
if(null == jsonObject){
|
|
log.error("转换json对象失败:{}" , filename);
|
|
log.error("转换json对象失败:{}" , filename);
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
+ //原始照片在oss的路径
|
|
String origin = jsonObject.getString("origin");
|
|
String origin = jsonObject.getString("origin");
|
|
String newFileName = jsonObject.getString("name");
|
|
String newFileName = jsonObject.getString("name");
|
|
if(!StringUtils.isNoneEmpty(origin , newFileName)){
|
|
if(!StringUtils.isNoneEmpty(origin , newFileName)){
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
-
|
|
|
|
- //将文件复制到本地,成新的文件名,将数据库中的原照片,都修改成新的文件名
|
|
|
|
- ImageEntity imageEntity = imageService2.findByFileName(origin);
|
|
|
|
- if(null == imageEntity){
|
|
|
|
- log.warn("照片[{}]不存在" , origin);
|
|
|
|
|
|
+ checkAndChangeFileName(newFileName);
|
|
|
|
+ //从oss获取照片,然后用新的名字下载到本地
|
|
|
|
+ Map<String , Object> ossInfo = getOssBucketInfo(origin);
|
|
|
|
+ if(CollectionUtils.isEmpty(ossInfo)){
|
|
|
|
+ log.info("oss数据解析失败,跳过");
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- // 获取图片房间类型
|
|
|
|
- String imageType = StringUtils.substringBefore(newFileName, "_");
|
|
|
|
- imageEntity.setType(imageType);
|
|
|
|
- if(StringUtils.isNotBlank(imageEntity.getLocalPath())){
|
|
|
|
- String localPath = imageEntity.getLocalPath();
|
|
|
|
- int index = localPath.indexOf(imageEntity.getFileName());
|
|
|
|
- if(index == -1){
|
|
|
|
- log.warn("照片本地路径格式有误");
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- String localDirectory = localPath.substring(0 , index);
|
|
|
|
- String newFileFullPath = localDirectory + newFileName;
|
|
|
|
- log.info("将进行文件名称修改,将[{}]修改成:[{}]" , localPath , newFileFullPath);
|
|
|
|
- File oldImage = new File(localPath);
|
|
|
|
- File newImage = new File(newFileFullPath);
|
|
|
|
- try {
|
|
|
|
- FileCopyUtils.copy(oldImage , newImage);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.error("复制本地照片出现异常");
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- boolean delLocal = FileUtils.deleteFile(localPath);
|
|
|
|
- if(!delLocal){
|
|
|
|
- log.error("删除改名之前的照片[{}]失败" , localPath);
|
|
|
|
- }
|
|
|
|
- imageEntity.setLocalPath(newFileFullPath);
|
|
|
|
- imageEntity.setPath(newFileFullPath);
|
|
|
|
|
|
+ String bucketName = (String) ossInfo.get("ossBucket");
|
|
|
|
+ String ossObjKey = (String) ossInfo.get("ossKey");
|
|
|
|
+ if(!StringUtils.isNoneBlank(bucketName , ossObjKey)){
|
|
|
|
+ log.info("oss的bucketName数据解析失败,跳过");
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- imageEntity.setFileName(newFileName);
|
|
|
|
- if(StringUtils.isNotBlank(imageEntity.getVerticalPath())){
|
|
|
|
- String verticalPath = imageEntity.getVerticalPath();
|
|
|
|
- int tmpIndex = verticalPath.indexOf(origin);
|
|
|
|
- if(tmpIndex != -1){
|
|
|
|
- String newVerticalPath = verticalPath.substring(0 ,tmpIndex);
|
|
|
|
- newVerticalPath += newFileName;
|
|
|
|
- imageEntity.setVerticalPath(newVerticalPath);
|
|
|
|
- }
|
|
|
|
|
|
+ String oldImageName = "";
|
|
|
|
+ int index = ossObjKey.lastIndexOf("/");
|
|
|
|
+ if(index == -1){
|
|
|
|
+ log.error("[{}]获取不到改名前的名称" , ossObjKey);
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- int update = imageService2.update(imageEntity);
|
|
|
|
- if(update != 1){
|
|
|
|
- log.error("更新照片[{}]的数据失败" , origin);
|
|
|
|
- throw new CommonBaseException(ResultCodeEnum.D101 ,"更新照片数据失败");
|
|
|
|
|
|
+ oldImageName = ossObjKey.substring(index);
|
|
|
|
+ log.info("照片改名前的名称为:{}" , oldImageName);
|
|
|
|
+ //这里改名
|
|
|
|
+ String newFilePath = directoryName + newFileName;
|
|
|
|
+ String oldFilePath = directoryName + oldImageName;
|
|
|
|
+ File oldImageFile = new File(oldFilePath);
|
|
|
|
+ File newImageFile = new File(newFilePath);
|
|
|
|
+ try {
|
|
|
|
+ org.apache.commons.io.FileUtils.copyFile(oldImageFile, newImageFile);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("改名操作出现异常,跳过");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
|
|
+ boolean del = FileUtils.deleteFile(oldFilePath);
|
|
|
|
+ log.info("删除的文件目录为:{},删除结果为:{}" , oldFilePath , del);
|
|
|
|
+ //用于四维看看的
|
|
imageHighPath = OUTPATH + sceneCode + "/output_img/" + newFileName;
|
|
imageHighPath = OUTPATH + sceneCode + "/output_img/" + newFileName;
|
|
|
|
+ //用于sketch,sketch的image表需要存储这个路径,给前端使用
|
|
imageLowPath = OUTPATH + sceneCode + "/output_img_low/" + newFileName;
|
|
imageLowPath = OUTPATH + sceneCode + "/output_img_low/" + newFileName;
|
|
// 提前把算法的图片路径存到数据库,但不代表算法运行成功,最后好是要看house的状态;
|
|
// 提前把算法的图片路径存到数据库,但不代表算法运行成功,最后好是要看house的状态;
|
|
String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ newFileName;
|
|
String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ newFileName;
|
|
String ossVerticalLowPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/low/"+ newFileName;
|
|
String ossVerticalLowPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/low/"+ newFileName;
|
|
- // 封装垂直校验后的图片到信息到oss
|
|
|
|
- ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath);
|
|
|
|
- ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath);
|
|
|
|
|
|
+ //TODO:这里需要计算分辨率
|
|
|
|
+ //照片分辨率
|
|
|
|
+ int totalResolutinRate = 0;
|
|
|
|
+ try {
|
|
|
|
+ totalResolutinRate = getImageResolutinoRate(newImageFile);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("计算照片[{}]分辨率出异常" , newFilePath);
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(totalResolutinRate);
|
|
|
|
+ log.info("照片{}的像素为:{}" , newFilePath , totalResolutinRate);
|
|
|
|
+
|
|
|
|
+ addNewImage(param.getId() , newFileName , newFilePath , ossVerticalLowPath, maxResolutionRate);
|
|
|
|
+ // 封装垂直校验后的图片到信息到oss,key为照片在本地的路径,value为照片在oss中的目录路径
|
|
|
|
+ //给四维看看的前端加载使用
|
|
|
|
+ ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath); //四维看看用的,文件太大
|
|
|
|
+ //给sketch的前端使用
|
|
|
|
+ ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath); //sketch用的,文件适中
|
|
needSendMqMsg = true;
|
|
needSendMqMsg = true;
|
|
}
|
|
}
|
|
insertScene(param.getImageMaxRate() , house);
|
|
insertScene(param.getImageMaxRate() , house);
|
|
@@ -317,6 +315,38 @@ public class HouseController extends BaseController {
|
|
return new R(MsgCode.SUCCESS_CODE, house);
|
|
return new R(MsgCode.SUCCESS_CODE, house);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public Map<String ,Object> getOssBucketInfo(String ossUrl){
|
|
|
|
+ Map<String , Object> result = new HashMap<>();
|
|
|
|
+ if(StringUtils.isBlank(ossUrl)){
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ log.info("oss资源的路径为:{}" , ossUrl);
|
|
|
|
+ int bucketStar = ossUrl.indexOf("://");
|
|
|
|
+ if(bucketStar == -1){
|
|
|
|
+ log.error("oss路径没有://");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ bucketStar += 3;
|
|
|
|
+ int buckeEnd = ossUrl.indexOf(".");
|
|
|
|
+ if(buckeEnd == -1){
|
|
|
|
+ log.error("oss路径没有./");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ String buckeName = ossUrl.substring(bucketStar , buckeEnd);
|
|
|
|
+ log.info("解析的buckeName={}" , buckeName);
|
|
|
|
+ int keyStart = ossUrl.indexOf("com/");
|
|
|
|
+ if(keyStart == -1){
|
|
|
|
+ log.error("oss路径没有com/");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ keyStart += 5;
|
|
|
|
+ String key = ossUrl.substring(keyStart , ossUrl.length());
|
|
|
|
+ log.info("解析出来的oss key={}" , key);
|
|
|
|
+ result.put("ossBucket" , buckeName);
|
|
|
|
+ result.put("ossKey" , key);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
// @RequiresRoles(value = {"admin", "edit", "upload"}, logical = Logical.OR)
|
|
// @RequiresRoles(value = {"admin", "edit", "upload"}, logical = Logical.OR)
|
|
@ApiOperation("生成houseId和场景码")
|
|
@ApiOperation("生成houseId和场景码")
|
|
@@ -326,7 +356,7 @@ public class HouseController extends BaseController {
|
|
return new R(MsgCode.SUCCESS_CODE, generateHouseIdAndSceneNum());
|
|
return new R(MsgCode.SUCCESS_CODE, generateHouseIdAndSceneNum());
|
|
}
|
|
}
|
|
|
|
|
|
- @ApiOperation("上传照片")
|
|
|
|
|
|
+ @ApiOperation("从OSS上下载原始照片并生成缩略图")
|
|
@PostMapping(value = "uploadImages")
|
|
@PostMapping(value = "uploadImages")
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@ApiImplicitParams({
|
|
@ApiImplicitParams({
|
|
@@ -334,24 +364,11 @@ public class HouseController extends BaseController {
|
|
@ApiImplicitParam(name = "sceneCode", value = "场景码", paramType = "query", required = true, dataType = "String"),
|
|
@ApiImplicitParam(name = "sceneCode", value = "场景码", paramType = "query", required = true, dataType = "String"),
|
|
@ApiImplicitParam(name = "houseId", value = "房间ID", paramType = "query", required = true, dataType = "Long")
|
|
@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;
|
|
|
|
- String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/";
|
|
|
|
- try {
|
|
|
|
- imageResult = addAndGetResolutionRate(file , filename , directoryName ,
|
|
|
|
- ossVerticalHighPath , houseId);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- log.error("保存照片到本地和持久化image对象出现异常:{}" , e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return Result.success(imageResult);
|
|
|
|
|
|
+ public Result uploadImages(@RequestParam(name = "sceneCode") String sceneCode,
|
|
|
|
+ @RequestParam(name = "ossBucketName") String ossBucketName,
|
|
|
|
+ @RequestParam(name = "ossObjectKey") String ossObjectKey){
|
|
|
|
+
|
|
|
|
+ return Result.success("成功" , genScaleImage(ossBucketName , ossObjectKey , sceneCode));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -383,6 +400,45 @@ public class HouseController extends BaseController {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public String genScaleImage(String ossBucketName , String ossObjectKey , String sceneCode){
|
|
|
|
+ String scalImageOssUrl = "";
|
|
|
|
+ if(!StringUtils.isNoneEmpty(ossBucketName)){
|
|
|
|
+ log.error("oss的bucketName 和ossKey不能为空");
|
|
|
|
+ return scalImageOssUrl;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ String fileName = "";
|
|
|
|
+ int index = ossObjectKey.lastIndexOf("/");
|
|
|
|
+ if(index != -1){
|
|
|
|
+ fileName = ossObjectKey.substring(index + 1 , ossObjectKey.length());
|
|
|
|
+ }else{
|
|
|
|
+ log.error("ossObjectKey[{}]的格式不正确,无法提取文件名" , ossObjectKey);
|
|
|
|
+ }
|
|
|
|
+ String directoryName = OUTPATH + sceneCode + File.separator + "input_img"+ File.separator;
|
|
|
|
+ String localFile = ossCheckPointUploadUtil.downloadFileFromOss(directoryName , "",
|
|
|
|
+ ossBucketName , ossObjectKey);
|
|
|
|
+ if(StringUtils.isNotBlank(localFile)){
|
|
|
|
+ //生成缩略图,并上传到oss
|
|
|
|
+ String scalImageFullPath = directoryName + "scal_" + fileName;
|
|
|
|
+// Thumbnails.of(localFile).scale(0.3).outputQuality(0.8f).toFile(scalImageFullPath);
|
|
|
|
+ Thumbnails.of(localFile).size(980,980).toFile(scalImageFullPath);
|
|
|
|
+ String imageOssPath = ossPath + "scal_" + fileName;
|
|
|
|
+ log.info("生成的照片上传oss的路径:{}" , imageOssPath);
|
|
|
|
+ ossCheckPointUploadUtil.upload2(scalImageFullPath, imageOssPath);
|
|
|
|
+ scalImageOssUrl = ossQueryUrl + imageOssPath;
|
|
|
|
+ //删除缩略图的本地文件
|
|
|
|
+ boolean delScalResult = FileUtils.deleteFile(scalImageFullPath);
|
|
|
|
+ if(!delScalResult){
|
|
|
|
+ log.error("删除本地缩略图[{}]失败" , scalImageFullPath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("从oss上下载照片到本地出现异常");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return scalImageOssUrl;
|
|
|
|
+ }
|
|
|
|
+
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
// public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String filename,String directoryName ,
|
|
// public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String filename,String directoryName ,
|
|
public Map<String ,Object> addAndGetResolutionRate(CommonsMultipartFile file , String filename,
|
|
public Map<String ,Object> addAndGetResolutionRate(CommonsMultipartFile file , String filename,
|
|
@@ -393,9 +449,6 @@ public class HouseController extends BaseController {
|
|
log.error("房源id或者文件为空,无法生成image记录");
|
|
log.error("房源id或者文件为空,无法生成image记录");
|
|
return new HashMap<>();
|
|
return new HashMap<>();
|
|
}
|
|
}
|
|
- // 获取图片房间类型
|
|
|
|
-// String imageType = StringUtils.substringBefore(filename, "_");
|
|
|
|
-
|
|
|
|
File dir = new File(directoryName);
|
|
File dir = new File(directoryName);
|
|
if(!dir.exists()){
|
|
if(!dir.exists()){
|
|
dir.mkdirs();
|
|
dir.mkdirs();
|
|
@@ -452,6 +505,31 @@ public class HouseController extends BaseController {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void addNewImage(long houseId,
|
|
|
|
+ String imageFileName , String imageLocalPath ,
|
|
|
|
+ String ossVerticalHighPath,
|
|
|
|
+ ImageResolutionRate maxResolutionRate){
|
|
|
|
+ ImageEntity image = new ImageEntity();
|
|
|
|
+ // 获取图片房间类型
|
|
|
|
+ String imageType = StringUtils.substringBefore(imageFileName, "_");
|
|
|
|
+ image.setType(imageType);
|
|
|
|
+ image.setVerticalPath(ossVerticalHighPath);
|
|
|
|
+ image.setFileName(imageFileName);
|
|
|
|
+ // 这个参数给前端用,只要有文件名就可以了
|
|
|
|
+ image.setPath(imageLocalPath);
|
|
|
|
+ image.setLocalPath(imageLocalPath);
|
|
|
|
+
|
|
|
|
+ image.setHouseId(houseId);
|
|
|
|
+ // 默认所有图片都是一楼
|
|
|
|
+ image.setFloor(1);
|
|
|
|
+
|
|
|
|
+ image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
|
|
|
|
+ int insert = imageService2.save(image);
|
|
|
|
+ if(insert != 1){
|
|
|
|
+ throw new CommonBaseException(ResultCodeEnum.D101, "新增image记录失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private String generalRandFileName(String fileName){
|
|
private String generalRandFileName(String fileName){
|
|
if(StringUtils.isBlank(fileName)){
|
|
if(StringUtils.isBlank(fileName)){
|
|
return "";
|
|
return "";
|