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

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

邓实川 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120
//	/**
//	 * 下载
//	 */
//	@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
121 122

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

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

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

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