1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
@RestController @RequestMapping("/qiniu") public class QiniuController { @Autowired private QiniuService qiniuService; @Autowired private ImageProcessingService imageProcessingService;
@PostMapping("/upload") public ResponseEntity<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) { try { String key = generateFileKey(file.getOriginalFilename()); FileUploadRequest request = FileUploadRequest.builder() .fileName(file.getOriginalFilename()) .contentType(file.getContentType()) .fileSize(file.getSize()) .key(key) .build(); FileUploadResponse response = qiniuService.uploadFile(request, file.getBytes()); Map<String, Object> result = new HashMap<>(); result.put("success", response.isSuccess()); result.put("key", response.getKey()); result.put("url", response.getUrl()); result.put("message", response.getMessage()); return ResponseEntity.ok(result); } catch (Exception e) { log.error("文件上传失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "上传失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/file/{key}") public ResponseEntity<Map<String, Object>> getFileInfo(@PathVariable String key) { try { FileInfo fileInfo = qiniuService.getFileInfo(key); Map<String, Object> response = new HashMap<>(); response.put("success", fileInfo != null); response.put("fileInfo", fileInfo); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取文件信息失败: key={}", key, e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取文件信息失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@DeleteMapping("/file/{key}") public ResponseEntity<Map<String, Object>> deleteFile(@PathVariable String key) { try { boolean success = qiniuService.deleteFile(key); Map<String, Object> response = new HashMap<>(); response.put("success", success); response.put("message", success ? "删除成功" : "删除失败"); return ResponseEntity.ok(response); } catch (Exception e) { log.error("删除文件失败: key={}", key, e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "删除失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@GetMapping("/files") public ResponseEntity<Map<String, Object>> listFiles( @RequestParam(required = false) String prefix, @RequestParam(defaultValue = "100") int limit) { try { List<FileInfo> files = qiniuService.listFiles(prefix, limit); Map<String, Object> response = new HashMap<>(); response.put("success", true); response.put("files", files); response.put("total", files.size()); return ResponseEntity.ok(response); } catch (Exception e) { log.error("获取文件列表失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "获取文件列表失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
@PostMapping("/image/process") public ResponseEntity<Map<String, Object>> processImage(@RequestBody ImageProcessingRequest request) { try { ImageProcessingResponse response = imageProcessingService.processImage(request); Map<String, Object> result = new HashMap<>(); result.put("success", response.isSuccess()); result.put("processedUrl", response.getProcessedUrl()); result.put("originalUrl", response.getOriginalUrl()); result.put("message", response.getMessage()); result.put("result", response.getResult()); return ResponseEntity.ok(result); } catch (Exception e) { log.error("图片处理失败", e); Map<String, Object> response = new HashMap<>(); response.put("success", false); response.put("message", "图片处理失败: " + e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } }
private String generateFileKey(String originalFilename) { String extension = ""; if (originalFilename != null && originalFilename.contains(".")) { extension = originalFilename.substring(originalFilename.lastIndexOf(".")); } return UUID.randomUUID().toString().replace("-", "") + extension; } }
|