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

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

import org.apache.ibatis.annotations.Param;
8
import org.springframework.beans.factory.annotation.Autowired;
yuquan.zhu committed
9
import org.springframework.transaction.annotation.Transactional;
邓实川 committed
10
import org.springframework.web.bind.annotation.DeleteMapping;
邓实川 committed
11
import org.springframework.web.bind.annotation.GetMapping;
yuquan.zhu committed
12
import org.springframework.web.bind.annotation.PostMapping;
邓实川 committed
13
import org.springframework.web.bind.annotation.RequestBody;
yuquan.zhu committed
14 15 16 17 18 19 20
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;
邓实川 committed
21
import cn.timer.api.dto.oss.OssDto;
yuquan.zhu committed
22 23
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
邓实川 committed
24
import cn.timer.api.utils.aliyun.OSSUtil;
yuquan.zhu committed
25 26 27 28 29 30 31 32
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 {
33 34
	@Autowired
	private OSSUtil oss;
yuquan.zhu committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

	/**
	 * 上传普通文件
	 * 
	 * @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 {
52
				url = oss.uploadFile(path, file.getInputStream());
yuquan.zhu committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			} 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 {
78
					url = oss.uploadFile(path, file.getInputStream());
yuquan.zhu committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
					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 {
100
				oss.uploadPrivateFile(path, file.getInputStream());
yuquan.zhu committed
101 102 103 104 105 106 107 108
			} catch (IOException e) {
				e.printStackTrace();
			}
			return ResultUtil.data(path, "上传成功!");
		}
	}

	/**
邓实川 committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	 * 下载
	 */
	@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("下载成功");
	}

	/**
yuquan.zhu committed
124 125 126 127 128 129 130
	 * 获取私密文件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;
131
		String url = oss.getUrlP(path);
yuquan.zhu committed
132 133 134
		return ResultUtil.data(url, "获取成功");
	}

邓实川 committed
135 136 137 138
	/***********
	 * DELETE
	 * 
	 * @param moudle
139
	 * @param fileName
邓实川 committed
140 141 142 143 144 145
	 ************/

	@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) {
146 147 148 149 150 151
		try {
			String path = "8timer2.0/" + userBean.getOrgCode() + "/" + moudle + "/" + fileName;
			oss.delSingleFile(path);
		} catch (Exception e) {
			e.getStackTrace();
		}
邓实川 committed
152 153 154 155 156 157
		return ResultUtil.success("删除成功");
	}

	@DeleteMapping(value = "/delFiles")
	@ApiOperation(value = "删除多个(谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
	public Result<List<String>> delFiles(@CurrentUser UserBean userBean, @RequestBody OssDto ossDto) {
158
		List<String> list = null;
邓实川 committed
159
		List<String> keys = null;
160 161 162 163 164 165 166 167 168
		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();
邓实川 committed
169 170 171 172 173 174 175 176 177 178 179
		}
		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
180
}