| /** | 
|  * Copyright (c) 2018 人人开源 All rights reserved. | 
|  * | 
|  * https://www.renren.io | 
|  * | 
|  * 版权所有,侵权必究! | 
|  */ | 
|   | 
| package com.zt.modules.oss.controller; | 
|   | 
| import cn.hutool.core.convert.Convert; | 
| import com.zt.common.annotation.LogOperation; | 
| import com.zt.common.annotation.QueryParam; | 
| import com.zt.common.constant.Constant; | 
| import com.zt.common.db.query.QueryFilter; | 
| import com.zt.common.exception.ErrorCode; | 
| import com.zt.common.servlet.PageResult; | 
| import com.zt.common.servlet.Result; | 
| import com.zt.core.oss.dto.OssDto; | 
| import com.zt.modules.oss.cloud.LocalStorageService; | 
| import com.zt.modules.oss.constant.OssConstant; | 
| import com.zt.modules.oss.enums.CloudChannel; | 
| import com.zt.modules.oss.model.QdSysOss; | 
| import com.zt.modules.oss.model.SysOss; | 
| import com.zt.modules.oss.service.QdSysOssService; | 
| import com.zt.modules.oss.service.SysOssService; | 
| import io.swagger.annotations.Api; | 
| import io.swagger.annotations.ApiImplicitParam; | 
| import io.swagger.annotations.ApiImplicitParams; | 
| import io.swagger.annotations.ApiOperation; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.http.HttpStatus; | 
| import org.springframework.http.ResponseEntity; | 
| import org.springframework.web.bind.annotation.*; | 
| import org.springframework.web.multipart.MultipartFile; | 
| import springfox.documentation.annotations.ApiIgnore; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 文件上传 | 
|  * | 
|  * @author hehz | 
|  */ | 
| @RestController | 
| @RequestMapping("sys/oss") | 
| @Api(tags = "文件上传") | 
| public class SysOssController { | 
|     @Autowired | 
|     private SysOssService sysOssService; | 
|     @Autowired | 
|     private LocalStorageService localStorageService; | 
|     @Autowired | 
|     private QdSysOssService qdSysOssService; | 
|   | 
|     @GetMapping("page") | 
|     @ApiOperation(value = "分页") | 
|     @ApiImplicitParams({ | 
|             @ApiImplicitParam(name = Constant.Q.PAGE, value = Constant.QV.PAGE, required = true, dataType = Constant.QT.INT), | 
|             @ApiImplicitParam(name = Constant.Q.LIMIT, value = Constant.QV.LIMIT, required = true, dataType = Constant.QT.INT), | 
|             @ApiImplicitParam(name = Constant.Q.ORDER_FIELD, value = Constant.QV.ORDER_FIELD, dataType = Constant.QT.STRING), | 
|             @ApiImplicitParam(name = Constant.Q.ORDER, value = Constant.QV.ORDER, dataType = Constant.QT.STRING), | 
|             @ApiImplicitParam(name = "fileName", value = "文件名", dataType = Constant.QT.STRING, format = "name^LK")}) | 
| //    @RequiresPermissions("sys:oss:all") | 
|     public PageResult<SysOss> page(@ApiIgnore @QueryParam QueryFilter queryFilter) { | 
|   | 
|         return PageResult.ok(sysOssService.page(queryFilter)); | 
|     } | 
|   | 
|     @PostMapping("uploadNew") | 
|     @ApiOperation(value = "上传文件") | 
|     @LogOperation("文件上传模块--->上传") | 
| //    @RequiresPermissions("sys:oss:all") | 
|     public Result<OssDto> uploadNew(@RequestBody Map<String, Object> fileData) throws Exception { | 
|         if (fileData.isEmpty()) { | 
|             return Result.error(ErrorCode.UPLOAD_FILE_EMPTY); | 
|         } | 
|         // 上传文件 | 
|         OssDto oss = sysOssService.uploadNew(fileData); | 
|         return Result.ok(oss); | 
|     } | 
|   | 
|     @PostMapping("upload") | 
|     @ApiOperation(value = "上传文件") | 
|     @LogOperation("文件上传模块--->上传") | 
| //    @RequiresPermissions("sys:oss:all") | 
|     public Result<OssDto> upload(@RequestParam("file") MultipartFile file) throws Exception { | 
|         if (file.isEmpty()) { | 
|             return Result.error(ErrorCode.UPLOAD_FILE_EMPTY); | 
|         } | 
|   | 
|         // 上传文件 | 
|         OssDto oss = sysOssService.upload(file); | 
|   | 
|         return Result.ok(oss); | 
|     } | 
|   | 
|   | 
|     @DeleteMapping | 
|     @ApiOperation(value = "删除") | 
|     @LogOperation("文件上传模块--->删除") | 
| //    @RequiresPermissions("sys:oss:all") | 
|     public Result delete(@RequestBody Long[] ids) { | 
|         for (Long id : ids) { | 
|             sysOssService.deleteTemporary(id); | 
|         } | 
|         return Result.ok(); | 
|     } | 
|   | 
|     @RequestMapping(value = "/content") | 
|     public void content(HttpServletRequest request, HttpServletResponse response) throws Exception { | 
|         String id = request.getParameter(OssConstant.ID); | 
|         if (StringUtils.isNotEmpty(id)) { | 
|             SysOss oss = sysOssService.getById(Long.parseLong(id)); | 
|             if (CloudChannel.LOCAL.getValue().equals(oss.getChannel())) { // 本地 | 
|                 System.out.println(CloudChannel.LOCAL.getValue()); | 
|                 System.out.println(oss.getChannel()); | 
|                 localStorageService.content(oss, response); | 
|             } | 
|         } | 
|     } | 
|   | 
|     @RequestMapping(value = "/content2") | 
|     public ResponseEntity<byte[]> content2(HttpServletRequest request) throws Exception { | 
|         //String id = fileId; | 
|         String id = request.getParameter("fileId"); | 
|         if (StringUtils.isNotEmpty(id)) { | 
|             SysOss oss = sysOssService.getById(Convert.toLong(id)); | 
|             if (CloudChannel.LOCAL.getValue().equals(oss.getChannel())) { // 本地 | 
|                 System.out.println(CloudChannel.LOCAL.getValue()); | 
|                 System.out.println(oss.getChannel()); | 
|                 return localStorageService.content2(oss); | 
|             } | 
|         } | 
|         String errorMessage = "文件不存在"; | 
|         return new ResponseEntity<>(errorMessage.getBytes(), HttpStatus.NOT_FOUND); | 
|     } | 
|   | 
|     @RequestMapping(value = "/commFile") | 
|     public void getCommFile(HttpServletRequest request, HttpServletResponse response) throws Exception { | 
|         String fileFlag = request.getParameter("fileFlag"); | 
|         if (StringUtils.isNotEmpty(fileFlag)) { | 
|             localStorageService.getCommFile(fileFlag, response); | 
|         } | 
|     } | 
|   | 
|     @RequestMapping(value = "/qdContent") | 
|     public void qdContent(HttpServletRequest request, HttpServletResponse response) throws Exception { | 
|         String id = request.getParameter("id"); | 
|         if (StringUtils.isNotEmpty(id)) { | 
|             QdSysOss qdSysOss = qdSysOssService.getById(Long.parseLong(id)); | 
|             if (CloudChannel.LOCAL.getValue().equals(qdSysOss.getChannel())) { | 
|                 System.out.println("qd:" + CloudChannel.LOCAL.getValue()); | 
|                 System.out.println("qd:" + qdSysOss.getChannel()); | 
|                 localStorageService.qdContent(qdSysOss, response); | 
|             } | 
|         } | 
|     } | 
|   | 
|     @GetMapping("/removeBase64File") | 
|     @ResponseBody | 
|     public Result<String> removeBase64File() { | 
|         try { | 
|             sysOssService.removeBase64File(); | 
|         } catch (Exception e) { | 
|   | 
|         } | 
|         return Result.ok("OK"); | 
|     } | 
| } |