OSSController.java 7.28 KB
Newer Older
yuquan.zhu committed
1 2 3
package cn.timer.api.controller.oss;

import java.io.IOException;
4
import java.util.*;
yuquan.zhu committed
5

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

import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
邓实川 committed
17
import cn.timer.api.dto.oss.OssDto;
yuquan.zhu committed
18 19
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
邓实川 committed
20
import cn.timer.api.utils.aliyun.OSSUtil;
yuquan.zhu committed
21 22 23
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

24 25
import javax.servlet.http.HttpServletRequest;

yuquan.zhu committed
26 27 28 29 30
@Api(tags = "9.0阿里云OSS操作")
@Transactional
@RequestMapping(value = "/oss", produces = { "application/json" })
@RestController
public class OSSController {
31 32
	@Autowired
	private OSSUtil oss;
33 34
	@Autowired
	private OSSService ossService;
yuquan.zhu committed
35 36

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

		      return data;
yuquan.zhu committed
49
	}
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	/**
	 * 上传审批文件
	 *
	 * @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
65 66 67

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

		      return data;

yuquan.zhu committed
80 81 82 83 84 85 86 87
	}

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

邓实川 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
//	/**
//	 * 下载
//	 */
//	@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
116 117

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

邓实川 committed
129 130
	/***********
	 * DELETE
131
	 *
邓实川 committed
132
	 * @param moudle
133
	 * @param fileName
邓实川 committed
134 135 136 137 138
	 ************/

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

	@DeleteMapping(value = "/delFiles")
	@ApiOperation(value = "删除多个(谨慎使用,谨慎使用)", httpMethod = "DELETE", notes = "接口发布说明")
	public Result<List<String>> delFiles(@CurrentUser UserBean userBean, @RequestBody OssDto ossDto) {
152 153
		List<String> list = null;
		try {
mobh committed
154
			List<String> keys = new ArrayList<String>();
155 156 157 158 159 160 161
			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
162 163 164 165 166 167 168 169 170 171 172
		}
		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("删除成功");
//	}

173 174 175 176 177 178 179 180 181

    @PostMapping(value = "/uploadOss")
    @ApiOperation(value = "上传普通文件", httpMethod = "POST", notes = "接口发布说明")
    @ResponseBody
    public Result<String> uploadOss(@CurrentUser UserBean userBean, @RequestParam(required = false) String moudle, @Param("file") MultipartFile file) throws Exception {
        Result<String> data = ossService.upload(userBean.getOrgCode(), moudle, file);

        return data;
    }
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

	@PostMapping(value = "/uploadImg")
	@ApiOperation(value = "上传图片", httpMethod = "POST", notes = "接口发布说明")
	@ResponseBody
	public Result<String> uploadImg(@RequestParam(required = true) String param,@RequestParam(required = false) String moudle, @Param("file") MultipartFile file) throws Exception {
		if (file == null || file.getSize() <= 0) {
			return ResultUtil.error("上传的文件为空,请重新选择!");
		}
		Set<String> typeSet = new HashSet<>();
		typeSet.add(".jpg");
		typeSet.add(".png");
		typeSet.add(".gif");
		String fileName = file.getOriginalFilename();
		int index = fileName.lastIndexOf(".");
		String fileType = fileName.substring(index);
		//校验图片类型是否有效
		if (!typeSet.contains(fileType)) {
			return ResultUtil.error("只能上传图片文件!");
		}
		JSONObject jsonObject = JSON.parseObject(param);
		Integer id = jsonObject.getInteger("id");
		String orgid = jsonObject.getString("orgid");
		moudle=id+"";

		Result<String> data = ossService.upload(1, moudle, file);
		return data;
	}


yuquan.zhu committed
211
}