houweiyu преди 4 години
родител
ревизия
3e24ada5b2

+ 3 - 0
fdkanfang-dao/src/main/java/com/fdkanfang/dao/backend/ImageMapper.java

@@ -15,6 +15,9 @@ public interface ImageMapper extends IBaseMapper<ImageEntity, Long> {
     @Select(value = "select * from tb_image where house_id = #{id}")
     List<ImageEntity> findByHouseId(Long id);
 
+    @Select(value = "select * from tb_image where file_name = #{fileName}")
+    ImageEntity findByFileName(String fileName);
+
     @Select(value = "select * from tb_image where house_id = #{houseId} and (floor = #{floor} or #{floor} is null)")
     List<ImageEntity> findByHouseIdAndFloor(Long houseId, Integer floor);
 }

+ 4 - 3
fdkanfang-domain/src/main/java/com/fdkanfang/domain/dto/HouseDto.java

@@ -42,10 +42,8 @@ public class HouseDto {
     @NotBlank(message = "朝向不能为空")
     private String orientation;
 
-    @ApiModelProperty(value = "多文件", required = true )
-    private MultipartFile[] files;
 
-    @ApiModelProperty(value = "文件", required = true )
+    @ApiModelProperty(value = "文件修改后的名称json数组", required = true )
     private List<String> fileNames;
 
     @ApiModelProperty(value = "制作要求", required = false )
@@ -60,6 +58,9 @@ public class HouseDto {
     @ApiModelProperty(value = "房源编号", required = false )
     private Integer num;
 
+    @ApiModelProperty(value = "是否是新建:0->新建,1->更新", required = true )
+    private Integer isCreate;
+
     // 以下好像没有用到
 
     // 操作者

+ 2 - 0
fdkanfang-service/src/main/java/com/fdkanfang/service/backend/ImageService2.java

@@ -12,5 +12,7 @@ public interface ImageService2 extends IBaseMapperService<ImageEntity, Long> {
 
     List<ImageEntity> findByHouseId(Long id);
 
+    ImageEntity findByFileName(String fileName);
+
     List<ImageEntity> findByHouseIdAndFloor(Long houseId, Integer floor);
 }

+ 5 - 0
fdkanfang-service/src/main/java/com/fdkanfang/service/backend/impl/ImageServiceImpl2.java

@@ -32,6 +32,11 @@ public class ImageServiceImpl2 extends BaseMapperServiceImpl<ImageEntity, Long>
     }
 
     @Override
+    public ImageEntity findByFileName(String fileName) {
+        return entityMapper.findByFileName(fileName);
+    }
+
+    @Override
     public List<ImageEntity> findByHouseIdAndFloor(Long houseId, Integer floor) {
         return entityMapper.findByHouseIdAndFloor(houseId, floor);
     }

+ 105 - 13
fdkanfang-web/src/main/java/com/fdkanfang/web/backend/HouseController.java

@@ -35,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.FileCopyUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
@@ -198,7 +199,7 @@ public class HouseController extends BaseController {
         }
         boolean isNewAdd = false;
         HouseEntity house = null;
-        if (param.getId() == null) {
+        if (null != param.getIsCreate() && param.getIsCreate().compareTo(0) == 0) {
             isNewAdd = true;
         }
         house = updateOrInsertHouse(param);
@@ -214,12 +215,68 @@ public class HouseController extends BaseController {
             String imageHighPath = null;
             String imageLowPath = null;
             for (String filename : fileNames) {
-                filename = checkAndChangeFileName(filename);
-                imageHighPath = OUTPATH + sceneCode + "/output_img/" + filename;
-                imageLowPath = OUTPATH + sceneCode + "/output_img_low/" + filename;
+
+//                filename = checkAndChangeFileName(filename);
+                JSONObject jsonObject = JSON.parseObject(filename);
+                if(null == jsonObject){
+                    log.error("转换json对象失败:{}" , filename);
+                    continue;
+                }
+                String origin = jsonObject.getString("origin");
+                String newFileName = jsonObject.getString("name");
+                if(!StringUtils.isNoneEmpty(origin , newFileName)){
+                    continue;
+                }
+
+                //将文件复制到本地,成新的文件名,将数据库中的原照片,都修改成新的文件名
+                ImageEntity imageEntity = imageService2.findByFileName(origin);
+                if(null == imageEntity){
+                    log.warn("照片[{}]不存在" , origin);
+                    continue;
+                }
+                // 获取图片房间类型
+                String imageType = StringUtils.substringBefore(filename, "_");
+                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(index , localPath.length());
+                    String newFileFullPath = localDirectory + newFileName;
+                    File oldImage = new File(localPath);
+                    File newImage = new File(newFileFullPath);
+                    try {
+                        FileCopyUtils.copy(oldImage , newImage);
+                    } catch (IOException e) {
+                        log.error("复制本地照片出现异常");
+                        e.printStackTrace();
+                    }
+                    imageEntity.setLocalPath(newFileFullPath);
+                    imageEntity.setPath(newFileFullPath);
+                }
+                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);
+                    }
+                }
+                int update = imageService2.update(imageEntity);
+                if(update != 1){
+                    log.error("更新照片[{}]的数据失败" , origin);
+                    throw new CommonBaseException(ResultCodeEnum.D101 ,"更新照片数据失败");
+                }
+                imageHighPath = OUTPATH + sceneCode + "/output_img/" + newFileName;
+                imageLowPath = OUTPATH + sceneCode + "/output_img_low/" + newFileName;
                 // 提前把算法的图片路径存到数据库,但不代表算法运行成功,最后好是要看house的状态;
-                String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ filename;
-                String ossVerticalLowPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/low/"+ filename;
+                String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ newFileName;
+                String ossVerticalLowPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/low/"+ newFileName;
                 // 封装垂直校验后的图片到信息到oss
                 ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath);
                 ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath);
@@ -255,6 +312,7 @@ public class HouseController extends BaseController {
         return new R(MsgCode.SUCCESS_CODE, house);
     }
 
+
 //    @RequiresRoles(value = {"admin", "edit", "upload"}, logical = Logical.OR)
     @ApiOperation("生成houseId和场景码")
     @PostMapping(value = "addPosition")
@@ -279,7 +337,8 @@ public class HouseController extends BaseController {
             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/"+ filename;
+            String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/";
             try {
               imageResult  = addAndGetResolutionRate(file , filename , directoryName ,
                       ossVerticalHighPath , houseId);
@@ -330,17 +389,21 @@ public class HouseController extends BaseController {
             return new HashMap<>();
         }
         // 获取图片房间类型
-        String imageType = StringUtils.substringBefore(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;
+        String newFileName = generalRandFileName(filename);
+        log.info("修改后的文件名称为:{}" , newFileName);
+        String fileFullPath = directoryName + newFileName;
+        String oldFileFullPath = directoryName + filename;
+        ossVerticalHighPath = ossVerticalHighPath + newFileName;
         ImageEntity image = new ImageEntity();
         image.setVerticalPath(ossVerticalHighPath);
-        image.setFileName(filename);
+        image.setFileName(newFileName);
         // 这个参数给前端用,只要有文件名就可以了
         image.setPath(fileFullPath);
         image.setLocalPath(fileFullPath);
@@ -348,17 +411,26 @@ public class HouseController extends BaseController {
         image.setHouseId(houseId);
         // 默认所有图片都是一楼
         image.setFloor(1);
-        image.setType(imageType);
+//        image.setType(imageType);
         //将图片保存到本地指定目录filePath eg: /root/data/kanfang/d_9iRDUgn3l/input_img/xxx.jpg
         saveFileToLocal(file , fileFullPath);
+        boolean delResult = FileUtils.deleteFile(oldFileFullPath);
+        if(!delResult){
+            log.error("删除旧文件[{}]失败" , delResult);
+        }
         File localFile = new File(fileFullPath);
         //生成缩略图,并上传到oss
-        String scalImageFullPath = directoryName + "scal_" + filename;
+        String scalImageFullPath = directoryName + "scal_" + newFileName;
         Thumbnails.of(localFile).size(128 , 256).outputQuality(0.8f).toFile(scalImageFullPath);
-        String imageOssPath =  ossPath + file.getOriginalFilename();
+        String imageOssPath =  ossPath + "scal_" + newFileName;
         log.info("生成的照片上传oss的路径:{}" , imageOssPath);
         ossCheckPointUploadUtil.upload2(scalImageFullPath, imageOssPath);
         String imageTotalOssUrl = ossQueryUrl + imageOssPath;
+        //删除缩略图的本地文件
+        boolean delScalResult = FileUtils.deleteFile(scalImageFullPath);
+        if(!delScalResult){
+            log.error("删除本地缩略图[{}]失败" , scalImageFullPath);
+        }
         //照片分辨率
         int totalResolutinRate = getImageResolutinoRate(localFile);
         ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(totalResolutinRate);
@@ -370,10 +442,30 @@ public class HouseController extends BaseController {
         }
         Map<String , Object> result = new HashMap<>();
         result.put("rate" , maxResolutionRate.name());
+        result.put("fileName" , newFileName);
         result.put("ossUrl" , imageTotalOssUrl);
         return result;
     }
 
+    private String generalRandFileName(String fileName){
+        if(StringUtils.isBlank(fileName)){
+            return "";
+        }
+        int index = fileName.lastIndexOf(".");
+        if(index != -1){
+            String name = fileName.substring(0 , index);
+            String form = fileName.substring(index , fileName.length());
+            log.info("照片存名字部分为:{}" , name);
+            if(StringUtils.isNotBlank(name)){
+                Random random = new Random(10);
+                name += System.currentTimeMillis() + random.nextInt(10);
+                name += form;
+                return name;
+            }
+        }
+        return "";
+    }
+
     private int getImageResolutinoRate(File localFile) throws IOException {
         if(null != localFile){
             int totalResolutinRate = -1;

+ 0 - 1
fdkanfang-web/src/main/java/com/fdkanfang/web/shiro/ShiroConfig.java

@@ -76,7 +76,6 @@ public class ShiroConfig {
         filterRuleMap.put("/admin/login", "anon");
         filterRuleMap.put("/user/login", "anon");
         filterRuleMap.put("/test/**", "anon");
-        filterRuleMap.put("/manage/house/**", "anon");
 
 
         // swagger 不拦截