dengsixing před 1 rokem
rodič
revize
f697975710

+ 2 - 0
src/main/java/com/fdkankan/jp/xspace/common/constant/NasPathConstant.java

@@ -5,6 +5,8 @@ public class NasPathConstant {
     public final static String BASE_PATH = "/mnt/xspace/";
     public final static String UNITY_WORK_PATH = BASE_PATH + "unity/";
 
+    public final static String temp_path = BASE_PATH + "temp/";
+
 
 
 

+ 29 - 0
src/main/java/com/fdkankan/jp/xspace/controller/FileController.java

@@ -0,0 +1,29 @@
+package com.fdkankan.jp.xspace.controller;
+
+import com.fdkankan.jp.xspace.common.Result;
+import com.fdkankan.jp.xspace.common.annotation.CheckPlatformAdminPermit;
+import com.fdkankan.jp.xspace.service.IFileService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/file")
+public class FileController extends BaseController{
+
+    @Autowired
+    private IFileService fileService;
+
+    @PostMapping("/upload")
+    public Result upload(@RequestParam(value = "file")MultipartFile file,
+                         @RequestParam(value = "num") String num,
+                         @RequestParam(value = "fileName") String fileName) throws IOException {
+      return Result.success(fileService.uploadXspaceSceneFile(file,num,fileName, getUser()));
+    }
+
+}

+ 12 - 0
src/main/java/com/fdkankan/jp/xspace/service/IFileService.java

@@ -0,0 +1,12 @@
+package com.fdkankan.jp.xspace.service;
+
+import com.fdkankan.jp.xspace.entity.User;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.IOException;
+
+public interface IFileService {
+
+    String uploadXspaceSceneFile(MultipartFile file, String num, String fileName, User user) throws IOException;
+
+}

+ 2 - 0
src/main/java/com/fdkankan/jp/xspace/service/ISceneXspaceService.java

@@ -25,4 +25,6 @@ public interface ISceneXspaceService extends IService<SceneXspace> {
 
     Result sync(List<String> nums, User user);
 
+    SceneXspace getByNumAndUserId(String num, long userId);
+
 }

+ 60 - 0
src/main/java/com/fdkankan/jp/xspace/service/impl/FileServiceImpl.java

@@ -0,0 +1,60 @@
+package com.fdkankan.jp.xspace.service.impl;
+
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.StrUtil;
+import com.fdkankan.fyun.face.FYunFileServiceInterface;
+import com.fdkankan.jp.xspace.common.ResultCode;
+import com.fdkankan.jp.xspace.common.constant.NasPathConstant;
+import com.fdkankan.jp.xspace.common.constant.OSSPathConstant;
+import com.fdkankan.jp.xspace.common.exception.BusinessException;
+import com.fdkankan.jp.xspace.entity.SceneXspace;
+import com.fdkankan.jp.xspace.entity.User;
+import com.fdkankan.jp.xspace.entity.XspaceUser;
+import com.fdkankan.jp.xspace.service.IFileService;
+import com.fdkankan.jp.xspace.service.ISceneXspaceService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.UUID;
+
+@Service
+@Slf4j
+public class FileServiceImpl implements IFileService {
+
+    @Autowired
+    private ISceneXspaceService sceneXspaceService;
+    @Resource
+    private FYunFileServiceInterface fYunFileService;
+
+
+    @Override
+    public String uploadXspaceSceneFile(MultipartFile file, String num, String fileName, User user) throws IOException {
+        SceneXspace sceneXspace = sceneXspaceService.getByNumAndUserId(num, user.getId());
+        if(Objects.isNull(sceneXspace)){
+            throw new BusinessException(ResultCode.SCENE_NOT_EXIT);
+        }
+
+        String url = null;
+        String localPath = null;
+        try {
+            String ossKey = String.format(OSSPathConstant.XSPACE_SCENE_FORMAT, num, sceneXspace.getSerial()) + fileName;
+            String extName = FileUtil.extName(fileName);
+            localPath = NasPathConstant.temp_path + UUID.randomUUID() + "." + extName;
+            FileUtil.mkParentDirs(localPath);
+            file.transferTo(new File(localPath));
+            url = fYunFileService.uploadFile(localPath, ossKey);
+        }finally {
+            if(StrUtil.isNotEmpty(localPath) && FileUtil.exist(localPath)){
+                FileUtil.del(localPath);
+            }
+        }
+        return url;
+    }
+}

+ 5 - 0
src/main/java/com/fdkankan/jp/xspace/service/impl/SceneXspaceServiceImpl.java

@@ -144,4 +144,9 @@ public class SceneXspaceServiceImpl extends ServiceImpl<ISceneXspaceMapper, Scen
 
         return Result.success();
     }
+
+    @Override
+    public SceneXspace getByNumAndUserId(String num, long userId) {
+        return this.getOne(new LambdaQueryWrapper<SceneXspace>().eq(SceneXspace::getNum, num).eq(SceneXspace::getUserId, userId));
+    }
 }