mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-24 07:00:49 +08:00
Introduce pluggable email and image upload
This commit is contained in:
24
src/main/java/com/openisle/service/CosImageUploader.java
Normal file
24
src/main/java/com/openisle/service/CosImageUploader.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.openisle.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* ImageUploader implementation using Tencent Cloud COS.
|
||||
* For simplicity this demo just returns a URL composed of the base URL and file name.
|
||||
*/
|
||||
@Service
|
||||
public class CosImageUploader extends ImageUploader {
|
||||
|
||||
private final String baseUrl;
|
||||
|
||||
public CosImageUploader(@Value("${cos.base-url:https://example.com}") String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String upload(byte[] data, String filename) {
|
||||
// In a real implementation you would call COS SDK here
|
||||
return baseUrl + "/" + filename;
|
||||
}
|
||||
}
|
||||
14
src/main/java/com/openisle/service/EmailSender.java
Normal file
14
src/main/java/com/openisle/service/EmailSender.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.openisle.service;
|
||||
|
||||
/**
|
||||
* Abstract email sender used to deliver emails.
|
||||
*/
|
||||
public abstract class EmailSender {
|
||||
/**
|
||||
* Send an email to a recipient.
|
||||
* @param to recipient email address
|
||||
* @param subject email subject
|
||||
* @param text email body
|
||||
*/
|
||||
public abstract void sendEmail(String to, String subject, String text);
|
||||
}
|
||||
14
src/main/java/com/openisle/service/ImageUploader.java
Normal file
14
src/main/java/com/openisle/service/ImageUploader.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.openisle.service;
|
||||
|
||||
/**
|
||||
* Abstract service for uploading images.
|
||||
*/
|
||||
public abstract class ImageUploader {
|
||||
/**
|
||||
* Upload an image and return its accessible URL.
|
||||
* @param data image binary data
|
||||
* @param filename name of the file
|
||||
* @return accessible URL of the uploaded file
|
||||
*/
|
||||
public abstract String upload(byte[] data, String filename);
|
||||
}
|
||||
@@ -12,13 +12,14 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class EmailService {
|
||||
public class ResendEmailSender extends EmailSender {
|
||||
|
||||
@Value("${resend.api.key}")
|
||||
private String apiKey;
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Override
|
||||
public void sendEmail(String to, String subject, String text) {
|
||||
String url = "https://api.resend.com/emails"; // hypothetical endpoint
|
||||
|
||||
@@ -78,4 +78,15 @@ public class UserService {
|
||||
.filter(User::isVerified)
|
||||
.filter(user -> passwordEncoder.matches(password, user.getPassword()));
|
||||
}
|
||||
|
||||
public Optional<User> findByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
public User updateAvatar(String username, String avatarUrl) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
user.setAvatar(avatarUrl);
|
||||
return userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user