-
Notifications
You must be signed in to change notification settings - Fork 0
Implement file handling for incidents with S3/MinIO support #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
29d529e
Implement file handling for incidents with S3/MinIO support
SandraNelj 2b9c29c
Run mvn spotless
SandraNelj 94faf63
Fix file handling edge cases and improve deletion logic
SandraNelj 11ee6ba
Merge branch 'main' into feature/document
SandraNelj 570845e
Added space between two methods that made the quality check fail, run…
SandraNelj 5e59659
Merge remote-tracking branch 'origin/feature/document' into feature/d…
SandraNelj b7dcb14
Added space between two methods that made the quality check fail, run…
SandraNelj 201e4dd
Removed okhttp import in pom.xml
SandraNelj aa9bde8
Added correct dependencies for okhttp and mvn clean install is ok.
SandraNelj 5683c99
refactor: restructure service layer and fix CodeRabbit feedback
SandraNelj ca3e319
refactor: improve file upload consistency and error handling
SandraNelj 031a7e6
fix: add rollback cleanup for partial file uploads
SandraNelj f7727ac
fix: cleanup uploaded files on transaction failure
SandraNelj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/org/example/team6backend/config/MinioConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package org.example.team6backend.config; | ||
|
|
||
| import io.minio.MinioClient; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class MinioConfig { | ||
|
|
||
| @Value("${minio.url}") | ||
| private String url; | ||
|
|
||
| @Value("${minio.access-key}") | ||
| private String accessKey; | ||
|
|
||
| @Value("${minio.secret-key}") | ||
| private String secretKey; | ||
|
|
||
| @Bean | ||
| public MinioClient minioClient() { | ||
| return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/example/team6backend/document/controller/DocumentController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package org.example.team6backend.document.controller; | ||
|
|
||
| import org.example.team6backend.document.entity.Document; | ||
| import org.example.team6backend.document.service.DocumentService; | ||
| import org.example.team6backend.incident.entity.Incident; | ||
| import org.example.team6backend.incident.service.IncidentService; | ||
| import org.example.team6backend.security.CustomUserDetails; | ||
| import org.example.team6backend.user.entity.AppUser; | ||
| import org.springframework.core.io.InputStreamResource; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.List; | ||
|
|
||
| @Controller | ||
| @RequestMapping("/documents") | ||
| public class DocumentController { | ||
|
|
||
| private final DocumentService documentService; | ||
| private final IncidentService incidentService; | ||
|
|
||
| public DocumentController(DocumentService documentService, IncidentService incidentService) { | ||
| this.documentService = documentService; | ||
| this.incidentService = incidentService; | ||
| } | ||
|
|
||
| @PostMapping("/upload/{incidentId}") | ||
| public String uploadFile(@PathVariable Long incidentId, @RequestParam("files") List<MultipartFile> files, | ||
| @AuthenticationPrincipal CustomUserDetails userDetails) { | ||
|
|
||
| AppUser user = userDetails.getUser(); | ||
| Incident incident = incidentService.getById(incidentId, user); | ||
|
|
||
| for (MultipartFile file : files) { | ||
| if (!file.isEmpty()) { | ||
| documentService.uploadFile(file, incident); | ||
| } | ||
| } | ||
| return "redirect:/incidents/" + incidentId; | ||
| } | ||
|
|
||
| @GetMapping("/download/{incidentId}") | ||
| public ResponseEntity<InputStreamResource> downloadFile(@PathVariable Long incidentId, | ||
| @AuthenticationPrincipal CustomUserDetails userDetails) { | ||
| AppUser user = userDetails.getUser(); | ||
| Incident incident = incidentService.getById(incidentId, user); | ||
|
|
||
| List<Document> documents = documentService.getDocumentsByIncident(incident); | ||
| if (documents.isEmpty()) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| Document document = documents.get(0); | ||
|
|
||
| InputStream stream = documentService.downloadFile(document.getFileKey()); | ||
|
|
||
| return ResponseEntity.ok() | ||
| .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + document.getFileName() + "\"") | ||
| .contentType(MediaType.parseMediaType(document.getContentType())).body(new InputStreamResource(stream)); | ||
|
|
||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
src/main/java/org/example/team6backend/document/entity/Document.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package org.example.team6backend.document.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import org.example.team6backend.incident.entity.Incident; | ||
|
|
||
| @Entity | ||
| public class Document { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "file_name") | ||
| private String fileName; | ||
| @Column(name = "content_type") | ||
| private String contentType; | ||
| @Column(name = "file_key") | ||
| private String fileKey; | ||
| @Column(name = "file_size") | ||
| private Long fileSize; | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(name = "incident_id") | ||
| private Incident incident; | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getFileName() { | ||
| return fileName; | ||
| } | ||
|
|
||
| public String getContentType() { | ||
| return contentType; | ||
| } | ||
|
|
||
| public String getFileKey() { | ||
| return fileKey; | ||
| } | ||
|
|
||
| public Long getFileSize() { | ||
| return fileSize; | ||
| } | ||
|
|
||
| public Incident getIncident() { | ||
| return incident; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public void setFileName(String fileName) { | ||
| this.fileName = fileName; | ||
| } | ||
|
|
||
| public void setContentType(String contentType) { | ||
| this.contentType = contentType; | ||
| } | ||
|
|
||
| public void setFileKey(String fileKey) { | ||
| this.fileKey = fileKey; | ||
| } | ||
|
|
||
| public void setFileSize(Long fileSize) { | ||
| this.fileSize = fileSize; | ||
| } | ||
|
|
||
| public void setIncident(Incident incident) { | ||
| this.incident = incident; | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
src/main/java/org/example/team6backend/document/repository/DocumentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package org.example.team6backend.document.repository; | ||
|
|
||
| import org.example.team6backend.document.entity.Document; | ||
| import org.example.team6backend.incident.entity.Incident; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface DocumentRepository extends JpaRepository<Document, Long> { | ||
| List<Document> findByIncident(Incident incident); | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/example/team6backend/document/service/DocumentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.example.team6backend.document.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.example.team6backend.document.entity.Document; | ||
| import org.example.team6backend.document.repository.DocumentRepository; | ||
| import org.example.team6backend.incident.entity.Incident; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import java.io.InputStream; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class DocumentService { | ||
|
|
||
| private final S3Service s3Service; | ||
| private final DocumentRepository documentRepository; | ||
|
|
||
| /** Upload file */ | ||
| public Document uploadFile(MultipartFile file, Incident incident) { | ||
|
|
||
| String fileKey = UUID.randomUUID() + "_" + file.getOriginalFilename(); | ||
| boolean uploaded = false; | ||
|
|
||
| try { | ||
| s3Service.uploadFile(fileKey, file); | ||
| uploaded = true; | ||
|
|
||
| Document document = new Document(); | ||
| document.setFileName(file.getOriginalFilename()); | ||
| document.setContentType(file.getContentType()); | ||
| document.setFileKey(fileKey); | ||
| document.setFileSize(file.getSize()); | ||
| document.setIncident(incident); | ||
|
|
||
| return documentRepository.save(document); | ||
|
|
||
| } catch (Exception e) { | ||
| if (uploaded) { | ||
| try { | ||
| s3Service.deleteFile(fileKey); | ||
| } catch (Exception cleanupEx) { | ||
| log.warn("Failed to cleanup S3 file: {}", fileKey, cleanupEx); | ||
| } | ||
| } | ||
| throw new RuntimeException("File upload failed", e); | ||
| } | ||
|
SandraNelj marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** Download file */ | ||
| public InputStream downloadFile(String objectKey) { | ||
| return s3Service.downloadFile(objectKey); | ||
| } | ||
|
|
||
| /** Delete file */ | ||
| public void deleteFile(Document document) { | ||
| s3Service.deleteFile(document.getFileKey()); | ||
| documentRepository.delete(document); | ||
| } | ||
|
|
||
| /** Fetch all files connected to one incident */ | ||
| public List<Document> getDocumentsByIncident(Incident incidentId) { | ||
| return documentRepository.findByIncident(incidentId); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
src/main/java/org/example/team6backend/document/service/S3Service.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.example.team6backend.document.service; | ||
|
|
||
| import io.minio.*; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.InputStream; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class S3Service { | ||
|
|
||
| private final MinioClient minioClient; | ||
|
|
||
| @Value("${minio.bucket}") | ||
| private String bucketName; | ||
|
|
||
| @PostConstruct | ||
| public void init() { | ||
| try { | ||
| boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); | ||
|
|
||
| if (!exists) { | ||
| minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Could not initialize minio service", e); | ||
| } | ||
| } | ||
|
|
||
| /** Upload file to MinIO */ | ||
| public void uploadFile(String fileKey, MultipartFile file) { | ||
| try { | ||
| minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey) | ||
| .stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to upload file " + fileKey, e); | ||
| } | ||
| } | ||
|
|
||
| /** Fetch file from MinIO */ | ||
| public InputStream downloadFile(String fileKey) { | ||
| try { | ||
| return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileKey).build()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to download file " + fileKey, e); | ||
| } | ||
| } | ||
|
|
||
| /** Delete file from MinIO */ | ||
| public void deleteFile(String fileKey) { | ||
| try { | ||
| minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileKey).build()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to delete file " + fileKey, e); | ||
| } | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.