package com.webmanage.controller;
|
|
import com.webmanage.common.Result;
|
import com.webmanage.service.MinioService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.core.io.InputStreamResource;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.http.ResponseEntity;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.InputStream;
|
import java.net.URLEncoder;
|
import java.nio.charset.StandardCharsets;
|
|
/**
|
* 文件上传下载控制器
|
*
|
* @author webmanage
|
* @date 2024-08-07
|
*/
|
@Slf4j
|
@RestController
|
@RequestMapping("/file")
|
@Api(tags = "文件管理")
|
public class FileController {
|
|
@Autowired
|
private MinioService minioService;
|
|
/**
|
* 上传文件
|
*/
|
@PostMapping("/upload")
|
@ApiOperation("上传文件")
|
public Result<String> uploadFile(
|
@ApiParam("文件") @RequestParam("file") MultipartFile file,
|
@ApiParam("文件夹") @RequestParam(defaultValue = "common") String folder) {
|
try {
|
if (file.isEmpty()) {
|
return Result.error("文件不能为空");
|
}
|
|
// 检查文件大小(限制为100MB)
|
if (file.getSize() > 100 * 1024 * 1024) {
|
return Result.error("文件大小不能超过100MB");
|
}
|
|
String fileName = minioService.uploadFile(file, folder);
|
return Result.success("文件上传成功", fileName);
|
} catch (Exception e) {
|
log.error("文件上传失败: ", e);
|
return Result.error("文件上传失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 下载文件
|
*/
|
@GetMapping("/download/{fileName}")
|
@ApiOperation("下载文件")
|
public ResponseEntity<InputStreamResource> downloadFile(
|
@ApiParam("文件名") @PathVariable String fileName,
|
@ApiParam("原始文件名") @RequestParam(required = false) String originalName) {
|
try {
|
InputStream inputStream = minioService.downloadFile(fileName);
|
|
// 如果没有提供原始文件名,从路径中提取
|
if (originalName == null || originalName.isEmpty()) {
|
String[] parts = fileName.split("/");
|
originalName = parts[parts.length - 1];
|
}
|
|
// 设置响应头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
headers.setContentDispositionFormData("attachment",
|
URLEncoder.encode(originalName, StandardCharsets.UTF_8.toString()));
|
|
InputStreamResource resource = new InputStreamResource(inputStream);
|
|
return ResponseEntity.ok()
|
.headers(headers)
|
.body(resource);
|
} catch (Exception e) {
|
log.error("文件下载失败: ", e);
|
return ResponseEntity.notFound().build();
|
}
|
}
|
|
/**
|
* 获取文件预览URL
|
*/
|
@GetMapping("/preview/{fileName}")
|
@ApiOperation("获取文件预览URL")
|
public Result<String> getPreviewUrl(@ApiParam("文件名") @PathVariable String fileName) {
|
try {
|
String previewUrl = minioService.getPreviewUrl(fileName);
|
return Result.success("获取预览URL成功", previewUrl);
|
} catch (Exception e) {
|
log.error("获取预览URL失败: ", e);
|
return Result.error("获取预览URL失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 获取文件下载URL
|
*/
|
@GetMapping("/download-url/{fileName}")
|
@ApiOperation("获取文件下载URL")
|
public Result<String> getDownloadUrl(@ApiParam("文件名") @PathVariable String fileName) {
|
try {
|
String downloadUrl = minioService.getDownloadUrl(fileName);
|
return Result.success("获取下载URL成功", downloadUrl);
|
} catch (Exception e) {
|
log.error("获取下载URL失败: ", e);
|
return Result.error("获取下载URL失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 删除文件
|
*/
|
@DeleteMapping("/delete/{fileName}")
|
@ApiOperation("删除文件")
|
public Result<Boolean> deleteFile(@ApiParam("文件名") @PathVariable String fileName) {
|
try {
|
minioService.deleteFile(fileName);
|
return Result.success("文件删除成功", true);
|
} catch (Exception e) {
|
log.error("文件删除失败: ", e);
|
return Result.error("文件删除失败: " + e.getMessage());
|
}
|
}
|
|
/**
|
* 检查文件是否存在
|
*/
|
@GetMapping("/exists/{fileName}")
|
@ApiOperation("检查文件是否存在")
|
public Result<Boolean> fileExists(@ApiParam("文件名") @PathVariable String fileName) {
|
try {
|
boolean exists = minioService.fileExists(fileName);
|
return Result.success("检查完成", exists);
|
} catch (Exception e) {
|
log.error("检查文件是否存在失败: ", e);
|
return Result.error("检查文件是否存在失败: " + e.getMessage());
|
}
|
}
|
}
|