|
@@ -0,0 +1,158 @@
|
|
|
+package com.miaxis.file.controller;
|
|
|
+
|
|
|
+import com.miaxis.common.aliyunOSS.AliyunUpload;
|
|
|
+import com.miaxis.common.annotation.Log;
|
|
|
+import com.miaxis.common.core.controller.BaseController;
|
|
|
+import com.miaxis.common.core.domain.Response;
|
|
|
+import com.miaxis.common.core.page.ResponsePageInfo;
|
|
|
+import com.miaxis.common.enums.BusinessTypeEnum;
|
|
|
+import com.miaxis.common.enums.FileUploadTypeEnum;
|
|
|
+import com.miaxis.common.exception.CustomException;
|
|
|
+import com.miaxis.common.utils.EnumUtils;
|
|
|
+import com.miaxis.common.utils.StringUtils;
|
|
|
+import com.miaxis.common.utils.poi.ExcelUtil;
|
|
|
+import com.miaxis.file.domain.FileInfo;
|
|
|
+import com.miaxis.file.service.IFileInfoService;
|
|
|
+import io.swagger.annotations.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 【文件】Controller
|
|
|
+ *
|
|
|
+ * @author miaxis
|
|
|
+ * @date 2021-01-22
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/file/info")
|
|
|
+@Api(tags={"【公共-文件】"})
|
|
|
+public class FileInfoController extends BaseController{
|
|
|
+ @Autowired
|
|
|
+ private IFileInfoService fileInfoService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ */
|
|
|
+ @Log(title = "上传文件", businessType = BusinessTypeEnum.INSERT)
|
|
|
+ @PostMapping
|
|
|
+ @ApiOperation("上传文件")
|
|
|
+ public Response<FileInfo> updateload(MultipartFile file, Integer fileType) throws IOException {
|
|
|
+ FileUploadTypeEnum fileUploadTypeEnum = getPathByType(fileType);
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ //获取最后一个.的位置
|
|
|
+ int lastIndexOf = originalFilename.lastIndexOf(".");
|
|
|
+ //获取文件的后缀名
|
|
|
+ String suffix = originalFilename.substring(lastIndexOf);
|
|
|
+ if (!validateFileSuffix(suffix,fileUploadTypeEnum)){
|
|
|
+ throw new CustomException("文件类型不合法");
|
|
|
+ }
|
|
|
+ String storagefileName = System.currentTimeMillis() + suffix;//文件名
|
|
|
+ String fileUrl = AliyunUpload.uploadForStream(file.getInputStream(), fileUploadTypeEnum.getFilePath(), storagefileName);
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setFilePath(fileUploadTypeEnum.getFilePath());
|
|
|
+ fileInfo.setFileUrl(fileUrl);
|
|
|
+ fileInfoService.save(fileInfo);
|
|
|
+ return Response.success(fileInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Boolean validateFileSuffix(String originalFilenamefile, FileUploadTypeEnum fileUploadTypeEnum) {
|
|
|
+ if (fileUploadTypeEnum == null){
|
|
|
+ throw new CustomException("fileType参数不合法");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(originalFilenamefile)){
|
|
|
+ throw new CustomException("文件名不合法");
|
|
|
+ }
|
|
|
+ String[] allowSuffixs = fileUploadTypeEnum.getSuffix();
|
|
|
+ for(String suffix : allowSuffixs){
|
|
|
+ String fileSuffix = "."+suffix;
|
|
|
+ if (originalFilenamefile.equals(fileSuffix)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private FileUploadTypeEnum getPathByType(Integer fileType) {
|
|
|
+ if (fileType == null){
|
|
|
+ throw new CustomException("文件类型不能为空");
|
|
|
+ }
|
|
|
+ return (FileUploadTypeEnum) EnumUtils.getEnumEntityByCode(FileUploadTypeEnum.class, fileType);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询文件列表
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('file:info:list')")
|
|
|
+ @GetMapping("/list")
|
|
|
+ @ApiOperation("查询文件列表")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
|
|
|
+ @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
|
|
|
+ })
|
|
|
+ public ResponsePageInfo<FileInfo> list(@ModelAttribute FileInfo fileInfo){
|
|
|
+ startPage();
|
|
|
+ List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
|
|
|
+ return toResponsePageInfo(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出文件列表
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('file:info:export')")
|
|
|
+ @Log(title = "文件", businessType = BusinessTypeEnum.EXPORT)
|
|
|
+ @GetMapping("/export")
|
|
|
+ @ApiOperation("导出文件列表Excel")
|
|
|
+ public Response<String> export(@ModelAttribute FileInfo fileInfo){
|
|
|
+ List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
|
|
|
+ ExcelUtil<FileInfo> util = new ExcelUtil<FileInfo>(FileInfo.class);
|
|
|
+ return util.exportExcel(list, "info");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件详细信息
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('file:info:query')")
|
|
|
+ @GetMapping(value = "/{fileId}")
|
|
|
+ @ApiOperation("获取文件详细信息")
|
|
|
+ public Response<FileInfo> getInfo(
|
|
|
+ @ApiParam(name = "fileId", value = "文件参数", required = true)
|
|
|
+ @PathVariable("fileId") Long fileId
|
|
|
+ ){
|
|
|
+ return Response.success(fileInfoService.getById(fileId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改文件
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('file:info:edit')")
|
|
|
+ @Log(title = "文件", businessType = BusinessTypeEnum.UPDATE)
|
|
|
+ @PutMapping
|
|
|
+ @ApiOperation("修改文件")
|
|
|
+ public Response<Integer> edit(@RequestBody FileInfo fileInfo){
|
|
|
+ return toResponse(fileInfoService.updateById(fileInfo) ? 1 : 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除文件
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('file:info:remove')")
|
|
|
+ @Log(title = "文件", businessType = BusinessTypeEnum.DELETE)
|
|
|
+ @DeleteMapping("/{fileIds}")
|
|
|
+ @ApiOperation("删除文件")
|
|
|
+ public Response<Integer> remove(
|
|
|
+ @ApiParam(name = "fileIds", value = "文件ids参数", required = true)
|
|
|
+ @PathVariable Long[] fileIds
|
|
|
+ ){
|
|
|
+ return toResponse(fileInfoService.removeByIds(Arrays.asList(fileIds)) ? 1 : 0);
|
|
|
+ }
|
|
|
+}
|