-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3Controller.java
More file actions
62 lines (53 loc) · 2.6 KB
/
Copy pathS3Controller.java
File metadata and controls
62 lines (53 loc) · 2.6 KB
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
package com.app.controller;
import com.app.service.S3Service;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
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 org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/files")
public class S3Controller {
private final S3Service s3Service;
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file,
@RequestParam(value = "isReadPublicly", defaultValue = "false") boolean isReadPublicly) {
boolean isUploaded = s3Service.uploadFile(file, isReadPublicly);
if (isUploaded) {
return ResponseEntity.ok("File uploaded successfully: " + file.getOriginalFilename());
} else {
return ResponseEntity.status(500).body("Failed to upload file: " + file.getOriginalFilename());
}
}
@GetMapping("/download/{key}")
public StreamingResponseBody downloadFile(@PathVariable String key, HttpServletResponse httpResponse) {
httpResponse.setContentType("application/octet-stream");
httpResponse.setHeader("Content-Disposition", String.format("inline; filename=\"%s\"", key));
CompletableFuture<ByteArrayInputStream> byteArrayInputStreamCompletableFuture = s3Service.downloadFileAsStream(key);
return outputStream -> {
ByteArrayInputStream byteArrayInputStream = byteArrayInputStreamCompletableFuture.join();
if (byteArrayInputStream != null) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = byteArrayInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
} else {
// Handle the case where the stream is null
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String errorMessage = "Failed to download the key. Please try again later.";
outputStream.write(errorMessage.getBytes());
outputStream.flush();
}
};
}
}