Skip to content

Commit

Permalink
完成邮件配置
Browse files Browse the repository at this point in the history
  • Loading branch information
Smith-Cruise committed Feb 20, 2018
1 parent 6df64d6 commit 0c59579
Show file tree
Hide file tree
Showing 19 changed files with 443 additions and 201 deletions.
25 changes: 0 additions & 25 deletions eagle-oj-mail/pom.xml

This file was deleted.

27 changes: 0 additions & 27 deletions eagle-oj-mail/src/main/java/com/eagleoj/mail/Main.java

This file was deleted.

9 changes: 9 additions & 0 deletions eagle-oj-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.eagleoj</groupId>
<artifactId>eagle-oj-judge</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<SettingEnum> keys = new ArrayList<>(6);
List<String> values = new ArrayList<>(6);
keys.add(SettingEnum.IS_OPEN_STORAGE);
Expand All @@ -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<String, String> map = new HashMap<>();
if (settingService.isOpenMail()) {
map.put(SettingEnum.IS_OPEN_MAIL.getName(), "true");
List<SettingEnum> 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<String> values = settingService.listSettings(keys);
for (int i=0; i<keys.size(); i++) {
map.put(keys.get(i).getName(), values.get(i));
}
fileUtil.refresh();
} else {
try {
settingService.updateSetting(SettingEnum.IS_OPEN_STORAGE, "false");
} catch (Exception e) {
settingService.saveSetting(SettingEnum.IS_OPEN_STORAGE, "false");
map.put(SettingEnum.IS_OPEN_MAIL.getName(), "false");
}
return new ResponseEntity(map);
}

@ApiOperation("更新")
@RequiresRoles("9")
@PostMapping("/mail")
public ResponseEntity addMailSetting(@RequestBody @Valid AddMailFormat format) {
Boolean isOpenMail = format.getOpenMail();
String host = format.getHost();
Integer port = format.getPort();
String username = format.getUsername();
String password = format.getPassword();

if (isOpenMail) {
if (host == null || port == null || username == null || password == null) {
throw new WebErrorException("邮件信息不完整");
}

List<SettingEnum> keys = new ArrayList<>(5);
List<String> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ public interface SettingMapper {
SettingEntity getByKey(String key);

List<SettingEntity> listByKeys(List<String> keys);

int updateByKey(@Param("key") String key, @Param("value") String value);

int updateByKeys(List<SettingEntity> entities);
}
54 changes: 54 additions & 0 deletions eagle-oj-web/src/main/java/com/eagleoj/web/file/FileService.java
Original file line number Diff line number Diff line change
@@ -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("未开启存储功能");
}
}
}
Loading

0 comments on commit 0c59579

Please sign in to comment.