templates.service.OssController.ftl Maven / Gradle / Ivy
package ${packageName}.service.party;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ServerWebExchange;
import ${packageName}.common.annotation.Inner;
import ${packageName}.common.constants.Constants;
import org.yes.tools.core.annotation.MultipartForm;
import org.yes.tools.core.exception.YesBaseException;
import org.yes.tools.core.result.CommonResult;
import org.yes.tools.core.vo.ImageVo;
import org.yes.tools.utils.OssUtils;
import reactor.core.publisher.Mono;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
/**
* @author Co.
* @name OssController
* @date 2023/2/23 16:05
*/
@Slf4j
@Tag(name = "对象储存控制器")
@RestController
public class OssController {
@Inner
@Operation(summary = "普通上传")
<#if isSpringCloud == 1>
@PostMapping("party/oss/ordinaryUpload")
<#else >
@PostMapping("api/party/oss/ordinaryUpload")
#if>
public Mono> ordinaryUpload(@MultipartForm(name = "file", type = MultipartFile.class) Object file,
@MultipartForm(name = "type", type = Integer.class) @Parameter(description = "1分类 2商品 3供应商 4标的") Integer type) throws ExecutionException, InterruptedException {
Mono fileMono = (Mono) file;
return fileMono.flatMap(multipartFile -> {
try {
ImageVo upload = OssUtils.ordinaryUpload(multipartFile, "co/test", "test");
return Mono.just(CommonResult.success(upload));
} catch (IOException e) {
return Mono.error(new RuntimeException(e));
}
});
}
@Inner
@Operation(summary = "分片上传")
<#if isSpringCloud == 1>
@PostMapping("party/oss/burstUpload")
<#else >
@PostMapping("api/party/oss/burstUpload")
#if>
public Mono> burstUpload(@MultipartForm(name = "file", type = File.class) Object obj,
@MultipartForm(name = "type", type = Integer.class) @Parameter(description = "1分类 2商品 3市场 4档口 5铜锣助手用户") Integer type) {
Mono fileMono = (Mono) obj;
return fileMono.flatMap(file -> {
ImageVo imageVo = new ImageVo();
try {
CompleteMultipartUploadResult result = OssUtils.burstUpload(file, "co/test", "test");
imageVo.setUrl("/" + result.getKey());
imageVo.setFileName(file.getName());
file.delete();
return Mono.just(CommonResult.success(imageVo));
} catch (IOException e) {
return Mono.error(new YesBaseException(e.getMessage()));
}
});
}
@Inner
@Operation(summary = "下载文件")
<#if isSpringCloud == 1>
@PostMapping("party/oss/download")
<#else >
@PostMapping("api/party/oss/download")
#if>
public ResponseEntity download(@RequestParam("path") String path, ServerWebExchange exchange) throws IOException {
int length = path.split("/").length;
InputStream content = OssUtils.download(path,"test");
HttpHeaders headers = new HttpHeaders();
Optional mediaType = MediaTypeFactory.getMediaType(path);
headers.setContentType(mediaType.orElse(MediaType.APPLICATION_OCTET_STREAM));
String downloadFileName = URLEncoder.encode(path.split("/")[length - 1], "UTF-8");
headers.setContentDispositionFormData("attachment", downloadFileName);
byte[] bytes = IOUtils.toByteArray(content);
content.close();
return new ResponseEntity(bytes, headers, HttpStatus.OK);
}
@Inner
@Operation(summary = "播放视频")
<#if isSpringCloud == 1>
@PostMapping("party/oss/play/video")
<#else >
@PostMapping("api/party/oss/play/video")
#if>
public ResponseEntity video(@RequestParam("path") String path, ServerWebExchange exchange) throws IOException {
int length = path.split("/").length;
InputStream content = OssUtils.download(path, "test");
HttpHeaders headers = new HttpHeaders();
Optional mediaType = MediaTypeFactory.getMediaType(path);
headers.setContentType(mediaType.orElse(MediaType.APPLICATION_OCTET_STREAM));
String downloadFileName = URLEncoder.encode(path.split("/")[length - 1], "UTF-8");
headers.setContentDispositionFormData("attachment", downloadFileName);
byte[] bytes = IOUtils.toByteArray(content);
content.close();
return new ResponseEntity(bytes, headers, HttpStatus.OK);
}
}