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 uploadFile( @ApiParam("文件") @RequestParam("file") MultipartFile file, @ApiParam("文件夹") @RequestParam(defaultValue = "common") String folder) { try { if (file.isEmpty()) { return Result.error("文件不能为空"); } // 检查文件大小(限制为100MB) if (file.getSize() > 500 * 1024 * 1024) { return Result.error("文件大小不能超过500MB"); } 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 downloadFileByPath( @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(); } } /** * 下载文件(查询参数方式) */ @GetMapping("/download") @ApiOperation("下载文件(查询参数)") public ResponseEntity downloadFile( @ApiParam("文件名") @RequestParam 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 getPreviewUrlByPath(@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("/preview") @ApiOperation("获取文件预览URL(查询参数)") public Result getPreviewUrl(@ApiParam("文件名") @RequestParam 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 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 deleteFileByPath(@ApiParam("文件名") @PathVariable String fileName) { try { minioService.deleteFile(fileName); return Result.success("文件删除成功", true); } catch (Exception e) { log.error("文件删除失败: ", e); return Result.error("文件删除失败: " + e.getMessage()); } } /** * 删除文件(查询参数方式) */ @DeleteMapping("/delete") @ApiOperation("删除文件(查询参数)") public Result deleteFile(@ApiParam("文件名") @RequestParam 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 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()); } } }