OSSController.java 5.72 KB
Newer Older
yuquan.zhu committed
1 2 3 4 5 6
package cn.timer.api.controller.oss;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

7
import cn.timer.api.service.OSSService;
yuquan.zhu committed
8
import org.apache.ibatis.annotations.Param;
9
import org.springframework.beans.factory.annotation.Autowired;
yuquan.zhu committed
10
import org.springframework.transaction.annotation.Transactional;
11
import org.springframework.web.bind.annotation.*;
yuquan.zhu committed
12 13 14 15
import org.springframework.web.multipart.MultipartFile;

import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
邓实川 committed
16
import cn.timer.api.dto.oss.OssDto;
yuquan.zhu committed
17 18
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
邓实川 committed
19
import cn.timer.api.utils.aliyun.OSSUtil;
yuquan.zhu committed
20 21 22 23 24 25 26 27
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 {
28 29
	@Autowired
	private OSSUtil oss;
30 31
	@Autowired
	private OSSService ossService;
yuquan.zhu committed
32 33

	/**
34
	 * 上传附件
35
	 *
yuquan.zhu committed
36 37 38 39 40
	 * @param userBean
	 * @param moudle
	 */
	@PostMapping(value = "/upload")
	@ApiOperation(value = "上传普通文件", httpMethod = "POST", notes = "接口发布说明")
41
	@ResponseBody
42
	public  Result<String> upload(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,@Param("file") MultipartFile file) throws Exception {
43 44 45
		Result<String> data = ossService.upload(userBean.getOrgCode(), moudle, file);

		      return data;
yuquan.zhu committed
46
	}
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
	/**
	 * 上传审批文件
	 *
	 * @param userBean
	 * @param moudle
	 */
	@PostMapping(value = "/approveUpload")
	@ApiOperation(value = "上传普通文件", httpMethod = "POST", notes = "接口发布说明")
	@ResponseBody
	public  Result<String> approveUpload(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,@RequestParam("filename") MultipartFile file) throws Exception {
		Result<String> data = ossService.upload(userBean.getOrgCode(), moudle, file);

		return data;
	}

yuquan.zhu committed
62 63 64

	/**
	 * 批量上传普通文件
65
	 *
yuquan.zhu committed
66 67 68 69 70 71
	 * @param userBean
	 * @param moudle
	 */
	@PostMapping(value = "/uploads")
	@ApiOperation(value = "批量上传普通文件", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> uploads(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
72
								  @Param("files") List<MultipartFile> files) {
73 74 75 76
		Result<Object> data = ossService.uploads(userBean.getOrgCode(), moudle, files);

		      return data;

yuquan.zhu committed
77 78 79 80 81 82 83 84
	}

	/**
	 * 上传私密文件
	 */
	@PostMapping(value = "/uploadpri")
	@ApiOperation(value = "上传私密文件", httpMethod = "POST", notes = "接口发布说明")
	public Result<String> uploadPrivate(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
85
										@Param("file") MultipartFile file) {
86
		String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/"+ file.getOriginalFilename();
yuquan.zhu committed
87 88 89 90
		if (file == null || file.getSize() <= 0) {
			return ResultUtil.error("上传的文件为空,请重新选择!");
		} else {
			try {
91
				oss.uploadPrivateFile(path, file.getInputStream());
yuquan.zhu committed
92 93 94 95 96 97 98
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ResultUtil.data(path, "上传成功!");
		}
	}

邓实川 committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
//	/**
//	 * 下载
//	 */
//	@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("下载成功");
//	}
邓实川 committed
113 114

	/**
yuquan.zhu committed
115 116 117 118 119
	 * 获取私密文件url(设置为10分钟有效)
	 */
	@PostMapping(value = "/getpri")
	@ApiOperation(value = "获取私密文件url", httpMethod = "POST", notes = "接口发布说明")
	public Result<String> getUrlPrivate(@CurrentUser UserBean userBean, @RequestParam String moudle,
120
										@RequestParam String fileName) {
yuquan.zhu committed
121
		String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName;
122
		String url = oss.getUrlP(path);
yuquan.zhu committed
123 124 125
		return ResultUtil.data(url, "获取成功");
	}

邓实川 committed
126 127
	/***********
	 * DELETE
128
	 *
邓实川 committed
129
	 * @param moudle
130
	 * @param fileName
邓实川 committed
131 132 133 134 135
	 ************/

	@DeleteMapping(value = "/delSingle")
	@ApiOperation(value = "删除单个(谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
	public Result<String> delSingle(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle,
136
									@RequestParam(required = false) String fileName) {
137 138 139 140 141 142
		try {
			String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName;
			oss.delSingleFile(path);
		} catch (Exception e) {
			e.getStackTrace();
		}
邓实川 committed
143 144 145 146 147 148
		return ResultUtil.success("删除成功");
	}

	@DeleteMapping(value = "/delFiles")
	@ApiOperation(value = "删除多个(谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
	public Result<List<String>> delFiles(@CurrentUser UserBean userBean, @RequestBody OssDto ossDto) {
149 150
		List<String> list = null;
		try {
mobh committed
151
			List<String> keys = new ArrayList<String>();
152 153 154 155 156 157 158
			for (String fileName : ossDto.getFileNames()) {
				String key = "8timer2.0/" + userBean.getOrgCode() + "/" + ossDto.getMoudle() + "/" + fileName;
				keys.add(key);
			}
			list = oss.delFiles(keys, ossDto.isQuiet());
		} catch (Exception e) {
			e.getStackTrace();
邓实川 committed
159 160 161 162 163 164 165 166 167 168 169
		}
		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("删除成功");
//	}

yuquan.zhu committed
170
}