Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ services:
- 'POSTGRES_USER=myuser'
ports:
- '5432:5432'

minio:
image: 'minio/minio:latest'
ports:
- '9000:9000'
- '9001:9001'
environment:
- MINIO_ROOT_USER=admin
- MINIO_ROOT_PASSWORD=password
command: server /data --console-address ":9001"
volumes:
- minio_data:/data

volumes:
minio_data:
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@
<artifactId>testcontainers-postgresql</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.awspring.cloud</groupId>-->
<!-- <artifactId>spring-cloud-aws-starter-s3</artifactId>-->
<!-- <version>4.0.0</version>-->
<!-- <scope>compile</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -159,6 +153,11 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.42.20</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.example.visacasemanagementsystem.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

import java.net.URI;

@Configuration
public class S3Config {

@Value("${minio.endpoint}")
private String endpoint;

@Value("${minio.accessKey}")
private String accessKey;

@Value("${minio.secretKey}")
private String secretKey;

@Value("${minio.region}")
private String region;


@Bean
public S3Client s3Client () {
return S3Client.builder()
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.serviceConfiguration(S3Configuration.builder()
.pathStyleAccessEnabled(true)
// .checksumValidationEnabled(false)
.build())
.build();
}

@Bean
public S3Presigner s3Presigner () {
return S3Presigner.builder()
.endpointOverride(URI.create(endpoint))
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.serviceConfiguration(S3Configuration.builder()
.pathStyleAccessEnabled(true)
.build())
.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.example.visacasemanagementsystem.file;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;

import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
@Slf4j
public class FileService {

private final S3Client s3Client;
private final S3Presigner s3Presigner;

private static final Set<String> ALLOWED_CONTENT_TYPES = Set.of(
"application/pdf",
"image/jpeg",
"image/png");

private static final long MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB


@Value("${minio.bucketName}")
private String bucketName;

@Value("${minio.corsAllowedOrigins:http://localhost:8080}")
private String corsAllowedOrigins;

public FileService(S3Client s3Client, S3Presigner s3Presigner) {
this.s3Client = s3Client;
this.s3Presigner = s3Presigner;
}

@PostConstruct
public void initializeBucket() {

if (bucketName == null || bucketName.equals("test-bucket")) {
log.info("Skipping bucket initialization (test profile or missing config)");
return;
}
try {
try {
s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
log.info("Bucket '{}' already exists", bucketName);
} catch (S3Exception e) {
if (e.statusCode() == 404) {
s3Client.createBucket(CreateBucketRequest.builder().bucket(bucketName).build());
log.info("Bucket '{}' created successfully", bucketName);
} else { throw e; }
}
} catch (Exception e) {
log.error("Failed to verify/create bucket '{}': {}", bucketName, e.getMessage(), e);
return;
}

try {

List<String> origins = Arrays.stream(corsAllowedOrigins.split(","))
.map(String::trim)
.filter(s -> !s.isBlank())
.toList();

if (origins.isEmpty()) {
log.warn("minio.corsAllowedOrigins is empty; skipping CORS configuration for bucket '{}'", bucketName);
return;
}

CORSRule corsRule = CORSRule.builder()
.allowedOrigins(origins)
.allowedMethods("GET","HEAD")
.allowedHeaders("*")
.build();

s3Client.putBucketCors(PutBucketCorsRequest.builder()
.bucket(bucketName)
.corsConfiguration(CORSConfiguration.builder().corsRules(corsRule).build())
.build());
log.info("CORS configuration applied to bucket '{}'", bucketName);
} catch (S3Exception e) {
log.warn("Failed to configure CORS to bucket '{}': {}", bucketName, e.getMessage(), e);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

public String uploadFile(MultipartFile file) throws IOException {
// Validate file size
if (file.getSize() > MAX_FILE_SIZE_BYTES) {
throw new IOException("File exceeds maximum allowed size of 10 MB");
}

// Validate content type
String contentType = file.getContentType();
if (contentType == null || !ALLOWED_CONTENT_TYPES.contains(contentType)) {
throw new IOException("Unsupported document type: " + contentType
+ ". Allowed: PDF, JPEG, PNG");
}

// Sanitize filename
String original = file.getOriginalFilename();
String safeName = (original == null ? "file"
: original.replaceAll("[^A-Za-z0-9._-]", "_"));

String fileName = UUID.randomUUID() + "_" + safeName;

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.contentType(contentType)
.build();

s3Client.putObject(putObjectRequest,
RequestBody.fromInputStream(file.getInputStream(), file.getSize()));

return fileName;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public String getPresignedDownloadUrl(String fileName) {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.responseContentDisposition("attachment; filename=\"" + fileName + "\"")
.build();

GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(getObjectRequest)
.build();

return s3Presigner.presignGetObject(presignRequest).url().toString();
}

public void deleteFile(String s3Key) {
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
.bucket(bucketName)
.key(s3Key)
.build();
s3Client.deleteObject(deleteObjectRequest);
}


}
Loading