add ConditionalOnProperty

This commit is contained in:
Ludy87 2025-05-01 21:53:12 +02:00
parent 159f4d8aa4
commit cd232d0b95
No known key found for this signature in database
GPG Key ID: 92696155E0220F94
6 changed files with 11 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.config.security.mail; package stirling.software.SPDF.config.security.mail;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@ -21,6 +22,7 @@ import stirling.software.SPDF.model.api.Email;
*/ */
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true", matchIfMissing = false)
public class EmailService { public class EmailService {
private final JavaMailSender mailSender; private final JavaMailSender mailSender;

View File

@ -2,6 +2,7 @@ package stirling.software.SPDF.config.security.mail;
import java.util.Properties; import java.util.Properties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
@ -20,6 +21,7 @@ import stirling.software.SPDF.model.ApplicationProperties;
@Configuration @Configuration
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true", matchIfMissing = false)
public class MailConfig { public class MailConfig {
private final ApplicationProperties applicationProperties; private final ApplicationProperties applicationProperties;

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.controller.api; package stirling.software.SPDF.controller.api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
@ -27,6 +28,7 @@ import stirling.software.SPDF.model.api.Email;
@RequiredArgsConstructor @RequiredArgsConstructor
@Slf4j @Slf4j
@Tag(name = "General", description = "General APIs") @Tag(name = "General", description = "General APIs")
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true", matchIfMissing = false)
public class EmailController { public class EmailController {
private final EmailService emailService; private final EmailService emailService;
@ -39,9 +41,7 @@ public class EmailController {
* @return ResponseEntity with success or error message. * @return ResponseEntity with success or error message.
*/ */
@PostMapping(consumes = "multipart/form-data", value = "/send-email") @PostMapping(consumes = "multipart/form-data", value = "/send-email")
public ResponseEntity<String> sendEmailWithAttachment( public ResponseEntity<String> sendEmailWithAttachment(@Valid @ModelAttribute Email email) {
@Valid @ModelAttribute
Email email) {
try { try {
// Calls the service to send the email with attachment // Calls the service to send the email with attachment
emailService.sendEmailWithAttachment(email); emailService.sendEmailWithAttachment(email);

View File

@ -424,6 +424,7 @@ public class ApplicationProperties {
@Data @Data
public static class Mail { public static class Mail {
private boolean enabled;
private String host; private String host;
private int port; private int port;
private String username; private String username;

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.model.api; package stirling.software.SPDF.model.api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@ -11,6 +12,7 @@ import lombok.NoArgsConstructor;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode @EqualsAndHashCode
@ConditionalOnProperty(value = "mail.enabled", havingValue = "true", matchIfMissing = false)
public class Email { public class Email {
@Schema( @Schema(
description = "The input file", description = "The input file",

View File

@ -77,6 +77,7 @@ premium:
appId: '' appId: ''
mail: mail:
enabled: true # set to 'true' to enable sending emails
host: smtp.example.com # SMTP server hostname host: smtp.example.com # SMTP server hostname
port: 587 # SMTP server port port: 587 # SMTP server port
username: '' # SMTP server username username: '' # SMTP server username