package cn.timer.api.controller.oss; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; 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 cn.timer.api.config.annotation.CurrentUser; import cn.timer.api.config.annotation.UserBean; import cn.timer.api.dto.oss.OssDto; import cn.timer.api.utils.Result; import cn.timer.api.utils.ResultUtil; import cn.timer.api.utils.aliyun.OSSUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @Api(tags = "9.0阿里云OSS操作") @Transactional @RequestMapping(value = "/oss", produces = { "application/json" }) @RestController public class OSSController { @Autowired private OSSUtil oss; /** * 上传普通文件 * * @param userBean * @param moudle */ @PostMapping(value = "/upload") @ApiOperation(value = "上传普通文件", httpMethod = "POST", notes = "接口发布说明") public Result<String> upload(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle, @Param("file") MultipartFile file) { String url = null; String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + file.getOriginalFilename(); if (file == null || file.getSize() <= 0) { return ResultUtil.error("上传的文件为空,请重新选择!"); } else { try { url = oss.uploadFile(path, file.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return ResultUtil.data(url, "上传成功!"); } } /** * 批量上传普通文件 * * @param userBean * @param moudle */ @PostMapping(value = "/uploads") @ApiOperation(value = "批量上传普通文件", httpMethod = "POST", notes = "接口发布说明") public Result<Object> uploads(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle, @Param("files") List<MultipartFile> files) { String url = null; List<String> list = new ArrayList<String>(); for (MultipartFile file : files) { String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + file.getOriginalFilename(); if (file == null || file.getSize() <= 0) { return ResultUtil.error("上传的文件为空,请重新选择!"); } else { try { url = oss.uploadFile(path, file.getInputStream()); list.add(url); } catch (IOException e) { e.printStackTrace(); } } } return ResultUtil.data(list, "上传成功!"); } /** * 上传私密文件 */ @PostMapping(value = "/uploadpri") @ApiOperation(value = "上传私密文件", httpMethod = "POST", notes = "接口发布说明") public Result<String> uploadPrivate(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle, @Param("file") MultipartFile file) { String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + file.getOriginalFilename(); if (file == null || file.getSize() <= 0) { return ResultUtil.error("上传的文件为空,请重新选择!"); } else { try { oss.uploadPrivateFile(path, file.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return ResultUtil.data(path, "上传成功!"); } } // /** // * 下载 // */ // @GetMapping(value = "/download") // @ApiOperation(value = "通过url下载文件到服务器", httpMethod = "GET", notes = "接口发布说明") // public Result<String> download(@CurrentUser UserBean userBean, @RequestParam String url, // @RequestParam String savePath) { // try { // oss.download(url, savePath); // } catch (Exception e) { // return ResultUtil.error("下载失败"); // } // return ResultUtil.data("下载成功"); // } /** * 获取私密文件url(设置为10分钟有效) */ @PostMapping(value = "/getpri") @ApiOperation(value = "获取私密文件url", httpMethod = "POST", notes = "接口发布说明") public Result<String> getUrlPrivate(@CurrentUser UserBean userBean, @RequestParam String moudle, @RequestParam String fileName) { String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName; String url = oss.getUrlP(path); return ResultUtil.data(url, "获取成功"); } /*********** * DELETE * * @param moudle * @param fileName ************/ @DeleteMapping(value = "/delSingle") @ApiOperation(value = "删除单个(谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明") public Result<String> delSingle(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle, @RequestParam(required = false) String fileName) { try { String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName; oss.delSingleFile(path); } catch (Exception e) { e.getStackTrace(); } return ResultUtil.success("删除成功"); } @DeleteMapping(value = "/delFiles") @ApiOperation(value = "删除多个(谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明") public Result<List<String>> delFiles(@CurrentUser UserBean userBean, @RequestBody OssDto ossDto) { List<String> list = null; List<String> keys = null; try { for (String fileName : ossDto.getFileNames()) { keys = new ArrayList<String>(); String key = "8timer2.0/" + userBean.getOrgCode() + "/" + ossDto.getMoudle() + "/" + fileName; keys.add(key); } list = oss.delFiles(keys, ossDto.isQuiet()); } catch (Exception e) { e.getStackTrace(); } return ResultUtil.data(list, "删除成功"); } // @DeleteMapping(value = "/delWithPrefix") // @ApiOperation(value = "删除指定前缀文件(谨慎使用,谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明") // public Result<String> delWithPrefix(@RequestParam String prefix) { // oss.delPrefixWith(prefix); // return ResultUtil.success("删除成功"); // } }