-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3FileController.java
More file actions
100 lines (78 loc) · 3.47 KB
/
Copy pathS3FileController.java
File metadata and controls
100 lines (78 loc) · 3.47 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
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
package demo.codeexample.s3FileStorage.web;
import demo.codeexample.s3FileStorage.application.S3FileService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Controller
public class S3FileController {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(S3FileController.class);
private final S3FileService s3Service;
public S3FileController(S3FileService s3Service) {
this.s3Service = s3Service;
}
@GetMapping("/files")
public String filesPage(Model model) {
model.addAttribute("isAuthenticated", true);
return "files";
}
@GetMapping("/api/files")
@ResponseBody
public List<String> listFiles() {
return s3Service.listFiles();
}
@DeleteMapping("/api/files")
@ResponseBody
public void deleteFile(@RequestParam String fileName) {
s3Service.deleteFile("my-bucket", fileName);
}
@GetMapping("/api/files/upload-url")
@ResponseBody
public Map<String, String> getUploadUrl(@RequestParam String fileName, @RequestParam String contentType) {
String url = s3Service.generatePresignedUploadUrl(fileName, contentType);
return Map.of("url", url);
}
@GetMapping("/api/files/download-url")
@ResponseBody
public Map<String, String> getDownloadUrl(@RequestParam String fileName) {
String url = s3Service.generatePresignedDownloadUrl(fileName);
return Map.of("url", url);
}
@GetMapping("/api/files/project-poster/{projectId}")
public String getProjectPoster(@PathVariable Long projectId) {
try {
// 1. Hämta listan säkert
var keys = s3Service.findFileKeysByProjectId(projectId);
// 2. Kolla om listan är null eller tom INNAN vi kör get(0) eller getFirst()
if (keys == null || keys.isEmpty()) {
log.warn("Ingen bild hittades i databasen för projekt {}", projectId);
return "redirect:/images/Hexagonalt_filmproduktionslogotyp.png";
}
String fileName = keys.get(0);
// 3. Generera URL
String presignedUrl = s3Service.generatePresignedDownloadUrl(fileName);
if (presignedUrl == null) {
return "redirect:/images/Hexagonalt_filmproduktionslogotyp.png";
}
return "redirect:" + presignedUrl;
} catch (Exception e) {
// Detta gör att vi ser EXAKT vad som går fel i IntelliJ-konsolen
log.error("Krasch vid hämtning av bild för projekt " + projectId, e);
return "redirect:/images/Hexagonalt_filmproduktionslogotyp.png"; // Vid fel, visa placeholder istället för att ge 500
}
}
@PostMapping("/api/files/callback")
@ResponseBody
@Transactional
public Map<String, String> uploadCallback(
@RequestParam Long projectId,
@RequestParam String fileName,
@RequestParam String contentType) {
log.info("Callback received: Project {} saves file {} and has been uploaded successfully", projectId, fileName);
// Here you could perform further processing, like saving metadata to a database
s3Service.saveFileMetadata(projectId, fileName, contentType);
return Map.of("status", "success", "message", "Callback received for " + fileName);
}
}