fixed image locale storage on minio
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.krylov.refound.controller;
|
||||
|
||||
import com.krylov.refound.service.FileStorageService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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 org.springframework.web.util.UriUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/files")
|
||||
@RequiredArgsConstructor
|
||||
public class FileStorageController {
|
||||
private final FileStorageService fileStorageService;
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
String objectName = fileStorageService.uploadFile(file);
|
||||
return ResponseEntity.ok(objectName); // фронт сохранит objectName как ключ картинки
|
||||
}
|
||||
|
||||
@GetMapping("/{objectName}")
|
||||
public ResponseEntity<byte[]> downloadFile(@PathVariable String objectName) {
|
||||
FileStorageService.DownloadedFile downloadedFile = fileStorageService.downloadFile(objectName);
|
||||
|
||||
String encodedFilename = UriUtils.encode(downloadedFile.originalFilename(), StandardCharsets.UTF_8);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFilename);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentType(MediaType.parseMediaType(downloadedFile.contentType()))
|
||||
.body(downloadedFile.content());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{objectName}")
|
||||
public ResponseEntity<Void> deleteFile(@PathVariable String objectName) {
|
||||
fileStorageService.deleteFile(objectName);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user