diff --git a/eagle-oj-mail/pom.xml b/eagle-oj-mail/pom.xml deleted file mode 100644 index 504260a..0000000 --- a/eagle-oj-mail/pom.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - eagle-oj - com.eagleoj - 1.0 - - 4.0.0 - - eagle-oj-mail - - - - com.sun.mail - javax.mail - 1.6.1 - - - org.springframework.boot - spring-boot-starter-mail - - - \ No newline at end of file diff --git a/eagle-oj-mail/src/main/java/com/eagleoj/mail/Main.java b/eagle-oj-mail/src/main/java/com/eagleoj/mail/Main.java deleted file mode 100644 index d224b6c..0000000 --- a/eagle-oj-mail/src/main/java/com/eagleoj/mail/Main.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.eagleoj.mail; - -import org.springframework.mail.javamail.JavaMailSenderImpl; -import org.springframework.mail.javamail.MimeMessageHelper; - -import javax.mail.internet.MimeMessage; - -/** - * @author Smith - **/ -public class Main { - public static void main(String[] args) throws Exception { - JavaMailSenderImpl sender = new JavaMailSenderImpl(); - sender.setHost("smtpdm.aliyun.com"); - sender.setPort(80); - sender.setUsername("mail@eagleoj.com"); - sender.setPassword("v2l8CRlTPtLvz6ocp2Bv"); - sender.testConnection(); - MimeMessage message = sender.createMimeMessage(); - MimeMessageHelper helper = new MimeMessageHelper(message); - helper.setTo("chendingchao1@126.com"); - helper.setText("Thank you for ordering!"); - helper.setSubject("xx"); - helper.setFrom("mail@eagleoj.com"); - sender.send(message); - } -} diff --git a/eagle-oj-web/pom.xml b/eagle-oj-web/pom.xml index 9351512..42fc7c5 100644 --- a/eagle-oj-web/pom.xml +++ b/eagle-oj-web/pom.xml @@ -79,6 +79,15 @@ jaxb-api 2.3.0 + + com.sun.mail + javax.mail + 1.6.1 + + + org.springframework.boot + spring-boot-starter-mail + com.eagleoj eagle-oj-judge diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/controller/IndexController.java b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/IndexController.java index dff52e8..7f8a464 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/controller/IndexController.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/IndexController.java @@ -1,6 +1,5 @@ package com.eagleoj.web.controller; -import com.eagleoj.web.setting.SettingKeyMapper; import com.eagleoj.web.setting.SettingService; import com.eagleoj.web.entity.AttachmentEntity; import com.eagleoj.web.entity.ResponseEntity; diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/controller/SettingController.java b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/SettingController.java index 1b2fd0c..e07c349 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/controller/SettingController.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/SettingController.java @@ -2,8 +2,11 @@ import com.eagleoj.judge.LanguageEnum; import com.eagleoj.web.cache.CacheController; +import com.eagleoj.web.controller.format.admin.AddMailFormat; import com.eagleoj.web.controller.format.admin.UpdateGlobalSettingFormat; import com.eagleoj.web.controller.format.admin.UpdateStorageSettingFormat; +import com.eagleoj.web.file.FileService; +import com.eagleoj.web.mail.MailService; import com.eagleoj.web.setting.SettingEnum; import com.eagleoj.web.setting.SettingService; import com.eagleoj.web.util.FileUtil; @@ -37,7 +40,10 @@ public class SettingController { private SettingService settingService; @Autowired - private FileUtil fileUtil; + private FileService fileService; + + @Autowired + private MailService mailService; @GetMapping public ResponseEntity getSetting() { @@ -120,16 +126,10 @@ public ResponseEntity updateStorageSetting(@RequestBody @Valid UpdateStorageSett } // 进行客户端测试 - if (! fileUtil.test(format.getAccessKey(), format.getSecretKey(), format.getEndPoint(), format.getBucket())) { + if (! fileService.test(format.getAccessKey(), format.getSecretKey(), format.getEndPoint(), format.getBucket())) { throw new WebErrorException("你填写的参数无效"); } - boolean isAlready = true; - try { - settingService.getSetting(SettingEnum.OSS_BUCKET); - } catch (Exception e) { - isAlready = false; - } List keys = new ArrayList<>(6); List values = new ArrayList<>(6); keys.add(SettingEnum.IS_OPEN_STORAGE); @@ -144,53 +144,88 @@ public ResponseEntity updateStorageSetting(@RequestBody @Valid UpdateStorageSett values.add(format.getBucket()); keys.add(SettingEnum.OSS_URL); values.add(format.getUrl()); - if (isAlready) { - // update - settingService.updateSettings(keys, values); - } else { - // save - settingService.saveSettings(keys, values); + settingService.updateSettings(keys, values); + fileService.refresh(); + } else { + settingService.updateSetting(SettingEnum.IS_OPEN_STORAGE, "false"); + } + return new ResponseEntity("更新设置成功"); + } + + @ApiOperation("获取邮件设置") + @RequiresRoles("9") + @GetMapping("/mail") + public ResponseEntity getMailSetting() { + Map map = new HashMap<>(); + if (settingService.isOpenMail()) { + map.put(SettingEnum.IS_OPEN_MAIL.getName(), "true"); + List keys = new ArrayList<>(4);; + keys.add(SettingEnum.MAIL_HOST); + keys.add(SettingEnum.MAIL_PORT); + keys.add(SettingEnum.MAIL_USERNAME); + keys.add(SettingEnum.MAIL_PASSWORD); + List values = settingService.listSettings(keys); + for (int i=0; i keys = new ArrayList<>(5); + List values = new ArrayList<>(5); + keys.add(SettingEnum.IS_OPEN_MAIL); + values.add("true"); + keys.add(SettingEnum.MAIL_HOST); + values.add(host); + keys.add(SettingEnum.MAIL_PORT); + values.add(String.valueOf(port)); + keys.add(SettingEnum.MAIL_USERNAME); + values.add(username); + keys.add(SettingEnum.MAIL_PASSWORD); + values.add(password); + settingService.updateSettings(keys, values); + } else { + settingService.updateSetting(SettingEnum.IS_OPEN_MAIL, "false"); } return new ResponseEntity("更新设置成功"); } + @ApiOperation("测试邮件发送") @RequiresRoles("9") - @PutMapping - public ResponseEntity updateSetting(@RequestBody @Valid UpdateSettingFormat format) { - /*String key = format.getKey(); - String value = format.getValue(); - switch (key) { - case "title": - settingService.updateSetting(SettingKeyMapper.WEB_TITLE, value); - break; - case "accessKey": - settingService.updateSetting(SettingKeyMapper.OSS_ACCESS_KEY, value); - break; - case "secretKey": - settingService.updateSetting(SettingKeyMapper.OSS_SECRET_KEY, value); - break; - case "bucket": - settingService.updateSetting(SettingKeyMapper.OSS_BUCKET, value); - break; - case "endPoint": - settingService.updateSetting(SettingKeyMapper.OSS_END_POINT, value); - break; - case "url": - settingService.updateSetting(SettingKeyMapper.OSS_URL, value); - break; - default: - throw new WebErrorException("不存在此key"); + @PostMapping("/mail/test") + public ResponseEntity testMailSetting(@RequestBody @Valid AddMailFormat format) { + String host = format.getHost(); + Integer port = format.getPort(); + String username = format.getUsername(); + String password = format.getPassword(); + String receiver = format.getTestMailAddress(); + if (host == null || port == null || username == null || + password == null || receiver == null) { + throw new WebErrorException("邮件信息不完整"); + } + + if (! mailService.testMail(host, port, username, password, receiver)) { + throw new WebErrorException("邮件发送失败"); } - settingService.refresh();*/ - return new ResponseEntity("网站配置更新成功"); + return new ResponseEntity("邮件发送成功,请查看收件箱"); } @PostMapping("/install") diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/controller/format/admin/AddMailFormat.java b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/format/admin/AddMailFormat.java new file mode 100644 index 0000000..f659565 --- /dev/null +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/controller/format/admin/AddMailFormat.java @@ -0,0 +1,80 @@ +package com.eagleoj.web.controller.format.admin; + +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.Range; +import org.hibernate.validator.constraints.URL; + +import javax.validation.constraints.NotNull; + +/** + * @author Smith + **/ +public class AddMailFormat { + + @NotNull + private Boolean isOpenMail; + + @Length(min = 1, max = 255) + private String host; + + @Range(min = 1) + private Integer port; + + @Email(message = "发件人地址非法") + private String username; + + @Length(min = 1, max = 255) + private String password; + + @Email(message = "测试收件人地址非法") + private String testMailAddress; + + public Boolean getOpenMail() { + return isOpenMail; + } + + public void setOpenMail(Boolean openMail) { + isOpenMail = openMail; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getTestMailAddress() { + return testMailAddress; + } + + public void setTestMailAddress(String testMailAddress) { + this.testMailAddress = testMailAddress; + } +} diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/dao/SettingMapper.java b/eagle-oj-web/src/main/java/com/eagleoj/web/dao/SettingMapper.java index dcc8bc1..e935d35 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/dao/SettingMapper.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/dao/SettingMapper.java @@ -19,8 +19,4 @@ public interface SettingMapper { SettingEntity getByKey(String key); List listByKeys(List keys); - - int updateByKey(@Param("key") String key, @Param("value") String value); - - int updateByKeys(List entities); } diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/file/FileService.java b/eagle-oj-web/src/main/java/com/eagleoj/web/file/FileService.java new file mode 100644 index 0000000..a7e7db3 --- /dev/null +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/file/FileService.java @@ -0,0 +1,54 @@ +package com.eagleoj.web.file; + +import com.eagleoj.judge.LanguageEnum; +import com.eagleoj.web.controller.exception.WebErrorException; +import com.eagleoj.web.setting.SettingEnum; +import com.eagleoj.web.setting.SettingService; +import com.eagleoj.web.util.FileUtil; +import com.eagleoj.web.util.WebUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.InputStream; +import java.util.UUID; + +/** + * @author Smith + **/ +@Service +public class FileService { + + @Autowired + private SettingService settingService; + + @Autowired + private FileUtil fileUtil; + + public boolean test(String accessKey, String secretKey, String endPoint, String bucket) { + return fileUtil.test(accessKey, secretKey, endPoint, bucket); + } + + public void refresh() { + fileUtil.refresh(); + } + + public String uploadCode(LanguageEnum lang, String code) { + checkIsOpen(); + String url = fileUtil.uploadCode(lang, code); + WebUtil.assertNotNull(url, "上传代码出错"); + return url; + } + + public String uploadAvatar(InputStream is, String postfix) { + checkIsOpen(); + String url = fileUtil.uploadAvatar(is, postfix); + WebUtil.assertNotNull(url, "上传头像出错"); + return url; + } + + private void checkIsOpen() { + if (! settingService.isOpenStorage()) { + throw new WebErrorException("未开启存储功能"); + } + } +} diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailEntity.java b/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailEntity.java new file mode 100644 index 0000000..0433aaf --- /dev/null +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailEntity.java @@ -0,0 +1,37 @@ +package com.eagleoj.web.mail; + +/** + * @author Smith + **/ +public class MailEntity { + + private String receiver; + + private String title; + + private String text; + + public String getReceiver() { + return receiver; + } + + public void setReceiver(String receiver) { + this.receiver = receiver; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailService.java b/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailService.java new file mode 100644 index 0000000..9b25a48 --- /dev/null +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/mail/MailService.java @@ -0,0 +1,19 @@ +package com.eagleoj.web.mail; + +import com.eagleoj.web.util.MailUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author Smith + **/ +@Service +public class MailService { + + @Autowired + private MailUtil mailUtil; + + public boolean testMail(String host, int port, String username, String password, String receiver) { + return mailUtil.test(host, port, username, password, receiver); + } +} diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/JudgeServiceImpl.java b/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/JudgeServiceImpl.java index 80fd0c6..52f69bd 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/JudgeServiceImpl.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/JudgeServiceImpl.java @@ -8,6 +8,7 @@ import com.eagleoj.web.data.status.ContestTypeStatus; import com.eagleoj.web.data.status.ProblemStatus; import com.eagleoj.web.entity.*; +import com.eagleoj.web.file.FileService; import com.eagleoj.web.judger.JudgeResult; import com.eagleoj.web.judger.task.ContestJudgeTask; import com.eagleoj.web.judger.task.GroupJudgeTask; @@ -34,7 +35,7 @@ public class JudgeServiceImpl implements JudgeService { private Logger LOGGER = LoggerFactory.getLogger(this.getClass()); @Autowired - private FileUtil fileUtil; + private FileService fileService; @Autowired private SubmissionService submissionService; @@ -174,7 +175,7 @@ private void saveSubmission(String sourceCode, LanguageEnum lang, double time, i if (settingService.isOpenStorage()) { String uploadURL = null; try { - uploadURL = fileUtil.uploadCode(lang, sourceCode); + uploadURL = fileService.uploadCode(lang, sourceCode); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new WebErrorException("保存代码记录失败"); diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/UserServiceImpl.java b/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/UserServiceImpl.java index 51072ee..2c8882a 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/UserServiceImpl.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/service/impl/UserServiceImpl.java @@ -4,18 +4,23 @@ import com.eagleoj.web.controller.exception.WebErrorException; import com.eagleoj.web.dao.UserMapper; import com.eagleoj.web.entity.UserEntity; +import com.eagleoj.web.file.FileService; import com.eagleoj.web.service.AttachmentService; import com.eagleoj.web.service.UserService; import com.eagleoj.web.setting.SettingService; import com.eagleoj.web.util.FileUtil; import com.eagleoj.web.util.WebUtil; import org.apache.shiro.crypto.hash.Md5Hash; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,11 +31,13 @@ @Service public class UserServiceImpl implements UserService { + private Logger LOGGER = LoggerFactory.getLogger(this.getClass()); + @Autowired private UserMapper userMapper; @Autowired - private FileUtil fileUtil; + private FileService fileService; @Autowired private AttachmentService attachmentService; @@ -104,20 +111,18 @@ public void updateUser(int uid, UserEntity userEntity) { @Transactional @Override public void uploadUserAvatar(Integer uid, MultipartFile file) { - if (! settingService.isOpenStorage()) { - throw new WebErrorException("未开启存储功能,无法上传"); - } + InputStream is = null; try { - String filePath = fileUtil.uploadAvatar(file.getInputStream(), "jpg"); - int aid = attachmentService.save(uid, filePath); - UserEntity userEntity = new UserEntity(); - userEntity.setAvatar(aid); - boolean flag = userMapper.updateByUid(uid, userEntity) == 1; - WebUtil.assertIsSuccess(flag, "头像更新失败"); - } catch (Exception e) { - throw new WebErrorException("头像更新失败"); + is = file.getInputStream(); + } catch (IOException e) { + throw new WebErrorException(e.getMessage()); } - + String filePath = fileService.uploadAvatar(is, "jpg"); + int aid = attachmentService.save(uid, filePath); + UserEntity userEntity = new UserEntity(); + userEntity.setAvatar(aid); + boolean flag = userMapper.updateByUid(uid, userEntity) == 1; + WebUtil.assertIsSuccess(flag, "头像更新失败"); } @Override diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingEnum.java b/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingEnum.java index 6b3931c..efbed79 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingEnum.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingEnum.java @@ -13,7 +13,8 @@ public enum SettingEnum { IS_OPEN_STORAGE("is_open_storage"), OSS_END_POINT("oss_end_point"), OSS_URL("oss_url"), OSS_ACCESS_KEY("oss_access_key"), OSS_SECRET_KEY("oss_secret_key"), OSS_BUCKET("oss_bucket"), - IS_OPEN_MAIL("is_open_mail"); + IS_OPEN_MAIL("is_open_mail"), + MAIL_HOST("mail_host"), MAIL_PORT("mail_port"), MAIL_USERNAME("mail_username"), MAIL_PASSWORD("mail_password"); private String name; diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingService.java b/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingService.java index af234c3..d4a7e93 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingService.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/setting/SettingService.java @@ -90,7 +90,8 @@ public List listSettings(List settingEnums) { } public void updateSetting(SettingEnum settingEnum, String value) { - boolean flag = settingMapper.updateByKey(settingEnum.getName(), value) == 1; + SettingEntity settingEntity = new SettingEntity(settingEnum.getName(), value); + boolean flag = settingMapper.save(settingEntity) > 0; WebUtil.assertIsSuccess(flag, "更新设置失败"); refresh(settingEnum); } @@ -101,7 +102,7 @@ public void updateSettings(List settingEnums, List values) for (int i=0; i 0; + boolean flag = settingMapper.saveList(list) > 0; WebUtil.assertIsSuccess(flag, "批量更新设置失败"); refreshList(settingEnums); } @@ -124,6 +125,15 @@ public boolean isOpenStorage() { } } + public boolean isOpenMail() { + try { + String s= getSetting(SettingEnum.IS_OPEN_MAIL); + return Boolean.valueOf(s); + } catch (Exception e) { + return false; + } + } + private void refresh(SettingEnum settingEnum) { if (settingMap.contains(settingEnum.getName())) { settingMap.remove(settingEnum.getName()); @@ -140,45 +150,6 @@ private void refreshList(List settingEnums) { listSettings(settingEnums); } - /* public synchronized String getSetting(String key) { - if (settingMap == null) { - refresh(); - } - return settingMap.get(key); - } - - public synchronized void refresh() { - settingMap = new HashMap<>(); - List list = settingMapper.listAll(); - for (SettingEntity entity: list) { - settingMap.put(entity.getKey(), entity.getValue()); - } - } - - public void updateSetting(String key, String value) { - boolean flag = settingMapper.updateByKey(key, value) == 1; - WebUtil.assertIsSuccess(flag, "网站设置更新失败"); - } - - public boolean isInstalled() { - if (settingMap == null) { - refresh(); - } - - if (settingMap == null) { - return false; - } - return settingMap.size() > 0; - } - - public Map getLangMap() { - Map lang = new HashMap<>(); - for (LanguageEnum languageEnum: LanguageEnum.values()) { - lang.put(languageEnum, languageEnum.getName()); - } - return lang; - } -*/ @Transactional public void installWeb(String email, String nickname, String password, String title) { if (isInstalled()) { diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/util/FileUtil.java b/eagle-oj-web/src/main/java/com/eagleoj/web/util/FileUtil.java index cda1476..e5a310b 100644 --- a/eagle-oj-web/src/main/java/com/eagleoj/web/util/FileUtil.java +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/util/FileUtil.java @@ -48,36 +48,46 @@ public boolean test(String accessKey, String secretKey, String endPoint, String } } - public String uploadCode(LanguageEnum lang, String code) throws Exception { - InputStream is = new ByteArrayInputStream(code.getBytes()); - StringBuilder fileName = new StringBuilder(UUID.randomUUID().toString()); - switch (lang) { - case JAVA8: - fileName.append(".java"); - break; - case C: - fileName.append(".c"); - break; - case CPP: - fileName.append(".cpp"); - break; - default: - fileName.append(".py"); - break; - } - ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentType("text/plain"); - String filePath = generateFilePath(fileName.toString()); + public String uploadCode(LanguageEnum lang, String code) { + try { + InputStream is = new ByteArrayInputStream(code.getBytes()); + StringBuilder fileName = new StringBuilder(UUID.randomUUID().toString()); + switch (lang) { + case JAVA8: + fileName.append(".java"); + break; + case C: + fileName.append(".c"); + break; + case CPP: + fileName.append(".cpp"); + break; + default: + fileName.append(".py"); + break; + } + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType("text/plain"); + String filePath = generateFilePath(fileName.toString()); - getOSSClient().putObject(settingService.getSetting(SettingEnum.OSS_BUCKET), filePath, is, metadata); - return "/"+filePath; + getOSSClient().putObject(settingService.getSetting(SettingEnum.OSS_BUCKET), filePath, is, metadata); + return "/"+filePath; + } catch (Exception e) { + LOGGER.error(e.getMessage()); + return null; + } } - public String uploadAvatar(InputStream is, String postfix) throws Exception { - String file = UUID.randomUUID().toString()+"."+postfix; - String filePath = generateFilePath(file); - getOSSClient().putObject(settingService.getSetting(SettingEnum.OSS_BUCKET), filePath, is); - return "/"+filePath; + public String uploadAvatar(InputStream is, String postfix) { + try { + String file = UUID.randomUUID().toString()+"."+postfix; + String filePath = generateFilePath(file); + getOSSClient().putObject(settingService.getSetting(SettingEnum.OSS_BUCKET), filePath, is); + return "/"+filePath; + } catch (Exception e) { + LOGGER.error(e.getMessage()); + return null; + } } private OSSClient getOSSClient() { diff --git a/eagle-oj-web/src/main/java/com/eagleoj/web/util/MailUtil.java b/eagle-oj-web/src/main/java/com/eagleoj/web/util/MailUtil.java new file mode 100644 index 0000000..39ee34c --- /dev/null +++ b/eagle-oj-web/src/main/java/com/eagleoj/web/util/MailUtil.java @@ -0,0 +1,87 @@ +package com.eagleoj.web.util; + +import com.eagleoj.web.controller.exception.WebErrorException; +import com.eagleoj.web.mail.MailEntity; +import com.eagleoj.web.setting.SettingEnum; +import com.eagleoj.web.setting.SettingService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Smith + **/ +@Component +public class MailUtil { + + private Logger LOGGER = LoggerFactory.getLogger(this.getClass()); + + private SettingService settingService; + + public MailUtil(SettingService settingService) { + this.settingService = settingService; + } + + public boolean send(MailEntity entity) { + try { + MimeMessageHelper helper = getHelper(); + helper.setTo(entity.getReceiver()); + helper.setSubject(entity.getTitle()); + helper.setText(entity.getText()); + return true; + } catch (MessagingException e) { + LOGGER.error(e.getMessage()); + return false; + } + } + + public boolean test(String host, int port, String username, String password, String receiver) { + try { + JavaMailSenderImpl sender = new JavaMailSenderImpl(); + sender.setHost(host); + sender.setPort(port); + sender.setUsername(username); + sender.setPassword(password); + sender.testConnection(); + MimeMessage message = sender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message); + helper.setFrom(username); + helper.setSubject("Eagle-oj-mail-test"); + helper.setText("This is a test mail for Eagle-oj system!"); + helper.setTo(receiver); + sender.send(message); + return true; + } catch (MessagingException e) { + return false; + } + } + + private MimeMessageHelper getHelper() throws MessagingException { + List keys = new ArrayList<>(4); + keys.add(SettingEnum.MAIL_HOST); + keys.add(SettingEnum.MAIL_PORT); + keys.add(SettingEnum.MAIL_USERNAME); + keys.add(SettingEnum.MAIL_PASSWORD); + List values = settingService.listSettings(keys); + String host = values.get(0); + int port = Integer.valueOf(values.get(1)); + String username = values.get(2); + String password = values.get(3); + JavaMailSenderImpl sender = new JavaMailSenderImpl(); + sender.setHost(host); + sender.setPort(port); + sender.setUsername(username); + sender.setPassword(password); + MimeMessage message = sender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message); + helper.setFrom(username); + return helper; + } +} diff --git a/eagle-oj-web/src/main/resources/application.properties b/eagle-oj-web/src/main/resources/application.properties index e6caacb..3c7c00d 100644 --- a/eagle-oj-web/src/main/resources/application.properties +++ b/eagle-oj-web/src/main/resources/application.properties @@ -1,5 +1,7 @@ spring.profiles.active=dev spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +logging.file=eagle-oj.log + mybatis.config-location=classpath:mybatis-config.xml mybatis.mapper-locations=classpath:mapping/*.xml \ No newline at end of file diff --git a/eagle-oj-web/src/main/resources/mapping/SettingMapper.xml b/eagle-oj-web/src/main/resources/mapping/SettingMapper.xml index f885d5c..5d48751 100644 --- a/eagle-oj-web/src/main/resources/mapping/SettingMapper.xml +++ b/eagle-oj-web/src/main/resources/mapping/SettingMapper.xml @@ -5,11 +5,11 @@ - INSERT IGNORE INTO setting VALUE (#{key}, #{value}) + REPLACE INTO setting VALUE (#{key}, #{value}) - INSERT IGNORE setting (`key`, value) VALUES + REPLACE INTO setting (`key`, value) VALUES (#{item.key}, #{item.value}) @@ -32,17 +32,6 @@ ) - - UPDATE setting SET value=#{value} WHERE `key`=#{key} - - - - REPLACE INTO setting VALUES - - (#{item.key}, #{item.value}) - - - diff --git a/pom.xml b/pom.xml index 06c8f2a..362cda9 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,6 @@ eagle-oj-web eagle-oj-judge - eagle-oj-mail