|
@@ -0,0 +1,135 @@
|
|
|
+package com.museum.web.controller;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.museum.common.constant.TypeCode;
|
|
|
+import com.museum.common.util.AliyunOssUtil;
|
|
|
+import com.museum.common.util.FileUtils;
|
|
|
+import com.museum.common.util.Result;
|
|
|
+import com.museum.domain.entity.BuildingEntity;
|
|
|
+import com.museum.domain.entity.DownloadEntity;
|
|
|
+import com.museum.domain.entity.FileEntity;
|
|
|
+import com.museum.domain.entity.UserEntity;
|
|
|
+import com.museum.domain.request.BuildingRequest;
|
|
|
+import com.museum.domain.request.DownloadRequest;
|
|
|
+import com.museum.domain.request.PageRequest;
|
|
|
+import com.museum.domain.response.DownloadResponse;
|
|
|
+import com.museum.service.DownloadService;
|
|
|
+import com.museum.service.UserService;
|
|
|
+import com.museum.web.aop.WebControllerLog;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.shiro.authz.annotation.Logical;
|
|
|
+import org.apache.shiro.authz.annotation.RequiresRoles;
|
|
|
+import org.apache.shiro.crypto.hash.Hash;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import tk.mybatis.mapper.entity.Condition;
|
|
|
+
|
|
|
+import javax.validation.Valid;
|
|
|
+import java.io.IOException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/5/9 0018 12:17
|
|
|
+ */
|
|
|
+@Log4j2
|
|
|
+@Api(tags = "共用接口")
|
|
|
+@RestController
|
|
|
+//@RequestMapping("manage/common")
|
|
|
+@RequestMapping("/common")
|
|
|
+//@Transactional
|
|
|
+public class CommonController extends BaseController {
|
|
|
+
|
|
|
+// @ApiImplicitParams({
|
|
|
+// @ApiImplicitParam(name = "type", value = "模型类型, roam:自由漫游,part:部件欣赏,building: 建筑物,ico时不用传", required = false),
|
|
|
+// @ApiImplicitParam(name = "files", value = "可以接收多个文件", required = true),
|
|
|
+// })
|
|
|
+ @ApiOperation("上传, 可以接收多个文件")
|
|
|
+ @PostMapping(value = "upload", consumes = {"multipart/form-data"})
|
|
|
+ public Result upload(MultipartFile [] files ) throws IOException {
|
|
|
+
|
|
|
+ if (files == null) {
|
|
|
+ log.error("文件不能为空");
|
|
|
+ return Result.failure("文件不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入本地服务器
|
|
|
+ List<Map<String, String>> uploads = FileUtils.upload(files, OUT_PATH);
|
|
|
+
|
|
|
+ HashMap<Long, String> resultMap = new HashMap<>();
|
|
|
+ String dir = null;
|
|
|
+ int a = 0;
|
|
|
+ for (Map<String, String> map: uploads) {
|
|
|
+ String fileName = map.get("name");
|
|
|
+ String newName = map.get("newName");
|
|
|
+ String ossPath = OSS_PATH+"/image/" + newName;
|
|
|
+ log.info("local {}, ", map.get("path"));
|
|
|
+ // 上传到oss
|
|
|
+ AliyunOssUtil.upload(map.get("path"), ossPath);
|
|
|
+
|
|
|
+ // 保存db
|
|
|
+ FileEntity entity = new FileEntity();
|
|
|
+
|
|
|
+ entity.setFileName(fileName);
|
|
|
+
|
|
|
+ String ossUrl = OSS_DOMAIN+ossPath;
|
|
|
+ entity.setFilePath(ossUrl);
|
|
|
+
|
|
|
+ fileService.save(entity);
|
|
|
+
|
|
|
+ // 返回前端数据
|
|
|
+ resultMap.put(entity.getId(), ossUrl);
|
|
|
+ dir = map.get("dir");
|
|
|
+ ++ a;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除本地服务器物理文件,用删除目录的方式
|
|
|
+ FileUtil.del(OUT_PATH + dir);
|
|
|
+
|
|
|
+// log.info("resultMap {}, ", resultMap);
|
|
|
+
|
|
|
+ // 用swagger测试有bug, 不显示返回值;使用postman可以
|
|
|
+ return Result.success(resultMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("删除文件")
|
|
|
+ @GetMapping("removes/{ids}")
|
|
|
+ public Result removes(@PathVariable String ids) throws IOException {
|
|
|
+
|
|
|
+// // 删除oss物理文件
|
|
|
+// List<FileEntity> files = fileService.findByIds(ids);
|
|
|
+// for (FileEntity entity: files) {
|
|
|
+// String filePath = entity.getFilePath();
|
|
|
+// filePath = OSS_PATH + StringUtils.substringAfterLast(filePath, "/");
|
|
|
+// log.info("remove oss file: {}", filePath);
|
|
|
+// AliyunOssUtil.delete(filePath);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 删除db记录
|
|
|
+// fileService.deleteByIds(ids);
|
|
|
+ removeFile(ids);
|
|
|
+
|
|
|
+ return Result.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ String a = "http://4dkankan.oss-cn-shenzhen.aliyuncs.com/sh_museum/image/20200606_175058527_0.jpg";
|
|
|
+ System.out.println(StringUtils.substringAfterLast(a, "/"));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|