🎨 代码优化

pull/69/head
ruibaby 2018-12-05 16:41:29 +08:00
parent 2998da98e4
commit 0242fcea37
11 changed files with 637 additions and 670 deletions

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Logs;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
@ -20,17 +21,11 @@ public interface LogsService {
/**
*
*
* @param logs logs
* @return Logs
* @param logTitle logTitle
* @param logContent logContent
* @param request request
*/
Logs saveByLogs(Logs logs);
/**
*
*
* @param logsId logsId
*/
void removeByLogsId(Long logsId);
void save(String logTitle, String logContent, HttpServletRequest request);
/**
*

View File

@ -223,7 +223,6 @@ public class AttachmentServiceImpl implements AttachmentService {
fileSmallPath.append("_small.");
fileSmallPath.append(fileSuffix);
String size = HaloUtils.parseSize(new File(fullPath.toString()).length());
String wh = HaloUtils.getImageWh(new File(fullPath.toString()));
@ -258,7 +257,7 @@ public class AttachmentServiceImpl implements AttachmentService {
String domain = HaloConst.OPTIONS.get("qiniu_domain");
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
String smallUrl = HaloConst.OPTIONS.get("qiniu_small_url");
if (accessKey == null || secretKey == null || domain == null || bucket == null) {
if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(domain) || StrUtil.isEmpty(bucket)) {
return resultMap;
}
Auth auth = Auth.create(accessKey, secretKey);
@ -318,7 +317,7 @@ public class AttachmentServiceImpl implements AttachmentService {
String domain = HaloConst.OPTIONS.get("upyun_oss_domain");
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
String smallUrl = HaloConst.OPTIONS.get("upyun_oss_small");
if (ossSrc == null || ossPwd == null || domain == null || bucket == null || operator == null) {
if (StrUtil.isEmpty(ossSrc) || StrUtil.isEmpty(ossPwd) || StrUtil.isEmpty(domain) || StrUtil.isEmpty(bucket) || StrUtil.isEmpty(operator)) {
return resultMap;
}
String fileName = file.getOriginalFilename();
@ -363,7 +362,7 @@ public class AttachmentServiceImpl implements AttachmentService {
String accessKey = HaloConst.OPTIONS.get("qiniu_access_key");
String secretKey = HaloConst.OPTIONS.get("qiniu_secret_key");
String bucket = HaloConst.OPTIONS.get("qiniu_bucket");
if (accessKey == null || secretKey == null || bucket == null) {
if (StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(bucket)) {
return false;
}
Auth auth = Auth.create(accessKey, secretKey);
@ -391,7 +390,7 @@ public class AttachmentServiceImpl implements AttachmentService {
String ossPwd = HaloConst.OPTIONS.get("upyun_oss_pwd");
String bucket = HaloConst.OPTIONS.get("upyun_oss_bucket");
String operator = HaloConst.OPTIONS.get("upyun_oss_operator");
if (ossSrc == null || ossPwd == null || bucket == null || operator == null) {
if (StrUtil.isEmpty(ossSrc) || StrUtil.isEmpty(ossPwd) || StrUtil.isEmpty(bucket) || StrUtil.isEmpty(operator)) {
return false;
}
UpYun upYun = new UpYun(bucket, operator, ossPwd);

View File

@ -3,11 +3,14 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Logs;
import cc.ryanc.halo.repository.LogsRepository;
import cc.ryanc.halo.service.LogsService;
import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@ -28,23 +31,18 @@ public class LogsServiceImpl implements LogsService {
/**
*
*
* @param logs logs
* @return Logs
* @param logTitle logTitle
* @param logContent logContent
* @param request request
*/
@Override
public Logs saveByLogs(Logs logs) {
return logsRepository.save(logs);
}
/**
*
*
* @param logsId logsId
*/
@Override
public void removeByLogsId(Long logsId) {
Optional<Logs> logs = this.findLogsByLogsId(logsId);
logsRepository.delete(logs.get());
public void save(String logTitle, String logContent, HttpServletRequest request) {
Logs logs = new Logs();
logs.setLogTitle(logTitle);
logs.setLogContent(logContent);
logs.setLogCreated(new Date());
logs.setLogIp(ServletUtil.getClientIP(request));
logsRepository.save(logs);
}
/**

View File

@ -20,7 +20,6 @@ import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -160,7 +159,7 @@ public class AdminController extends BaseController {
session.setAttribute(HaloConst.USER_SESSION_KEY, aUser);
//重置用户的登录状态为正常
userService.updateUserNormal();
logsService.saveByLogs(new Logs(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, ServletUtil.getClientIP(request), DateUtil.date()));
logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_SUCCESS, request);
log.info("User {} login succeeded.", aUser.getUserDisplayName());
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.login.success"));
} else {
@ -170,14 +169,7 @@ public class AdminController extends BaseController {
if (errorCount >= CommonParamsEnum.FIVE.getValue()) {
userService.updateUserLoginEnable(TrueFalseEnum.FALSE.getDesc());
}
logsService.saveByLogs(
new Logs(
LogsRecord.LOGIN,
LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]",
ServletUtil.getClientIP(request),
DateUtil.date()
)
);
logsService.save(LogsRecord.LOGIN, LogsRecord.LOGIN_ERROR + "[" + HtmlUtil.escape(loginName) + "," + HtmlUtil.escape(loginPwd) + "]", request);
Object[] args = {(5 - errorCount)};
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.login.failed", args));
}
@ -193,7 +185,7 @@ public class AdminController extends BaseController {
public String logOut(HttpSession session) {
User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY);
session.removeAttribute(HaloConst.USER_SESSION_KEY);
logsService.saveByLogs(new Logs(LogsRecord.LOGOUT, user.getUserName(), ServletUtil.getClientIP(request), DateUtil.date()));
logsService.save(LogsRecord.LOGOUT, user.getUserName(), request);
log.info("User {} has logged out", user.getUserName());
return "redirect:/admin/login";
}

View File

@ -1,7 +1,6 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.domain.Attachment;
import cc.ryanc.halo.model.domain.Logs;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.model.enums.PostTypeEnum;
@ -11,7 +10,6 @@ import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.utils.LocaleMessageUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@ -116,13 +114,12 @@ public class AttachmentController {
public Map<String, Object> upload(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
Map<String, Object> result = new HashMap<>(3);
System.out.println("源地址" + file.getOriginalFilename() + "类型" + file.getContentType() + "文件名" + file.getName() + "文件大小" + file.getSize());
if (!file.isEmpty()) {
try {
Map<String, String> resultMap = attachmentService.upload(file, request);
if (resultMap == null || resultMap.isEmpty()) {
log.error("文件上传失败");
result.put("success", 0);
log.error("File upload failed");
result.put("success", ResultCodeEnum.FAIL.getCode());
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
return result;
}
@ -139,16 +136,14 @@ public class AttachmentController {
attachment.setAttachLocation(resultMap.get("location"));
attachmentService.saveByAttachment(attachment);
log.info("Upload file {} to {} successfully", resultMap.get("fileName"), resultMap.get("filePath"));
logsService.saveByLogs(
new Logs(LogsRecord.UPLOAD_FILE, resultMap.get("fileName"), ServletUtil.getClientIP(request), DateUtil.date())
);
result.put("success", 1);
result.put("success", ResultCodeEnum.SUCCESS.getCode());
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-success"));
result.put("url", attachment.getAttachPath());
result.put("filename", resultMap.get("filePath"));
logsService.save(LogsRecord.UPLOAD_FILE, resultMap.get("fileName"), request);
} catch (Exception e) {
log.error("Upload file failed:{}", e.getMessage());
result.put("success", 0);
result.put("success", ResultCodeEnum.FAIL.getCode());
result.put("message", localeMessageUtil.getMessage("code.admin.attachment.upload-failed"));
}
} else {
@ -216,9 +211,7 @@ public class AttachmentController {
}
if (flag) {
log.info("Delete file {} successfully!", delFileName);
logsService.saveByLogs(
new Logs(LogsRecord.REMOVE_FILE, delFileName, ServletUtil.getClientIP(request), DateUtil.date())
);
logsService.save(LogsRecord.REMOVE_FILE, delFileName, request);
} else {
log.error("Deleting attachment {} failed!", delFileName);
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.common.delete-failed"));

View File

@ -1,6 +1,9 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.domain.Gallery;
import cc.ryanc.halo.model.domain.Link;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord;
@ -17,7 +20,6 @@ import cc.ryanc.halo.utils.MarkdownUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
@ -247,7 +249,7 @@ public class PageController {
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
}
postService.saveByPost(post);
logsService.saveByLogs(new Logs(LogsRecord.PUSH_PAGE, post.getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
logsService.save(LogsRecord.PUSH_PAGE, post.getPostTitle(), request);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
} catch (Exception e) {
log.error("Save page failed: {}", e.getMessage());

View File

@ -1,6 +1,9 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord;
@ -19,7 +22,6 @@ import cc.ryanc.halo.web.controller.core.BaseController;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -213,7 +215,7 @@ public class PostController extends BaseController {
post.setPostThumbnail("/static/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 10) + ".jpg");
}
postService.saveByPost(post);
logsService.saveByLogs(new Logs(LogsRecord.PUSH_POST, post.getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
logsService.save(LogsRecord.PUSH_POST, post.getPostTitle(), request);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), msg);
} catch (Exception e) {
log.error("Save article failed: {}", e.getMessage());
@ -268,7 +270,7 @@ public class PostController extends BaseController {
try {
Optional<Post> post = postService.findByPostId(postId);
postService.removeByPostId(postId);
logsService.saveByLogs(new Logs(LogsRecord.REMOVE_POST, post.get().getPostTitle(), ServletUtil.getClientIP(request), DateUtil.date()));
logsService.save(LogsRecord.REMOVE_POST, post.get().getPostTitle(), request);
} catch (Exception e) {
log.error("Delete article failed: {}", e.getMessage());
}

View File

@ -1,6 +1,5 @@
package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.domain.Logs;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord;
@ -12,14 +11,12 @@ import cc.ryanc.halo.service.OptionsService;
import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.utils.LocaleMessageUtil;
import cc.ryanc.halo.web.controller.core.BaseController;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.servlet.ServletUtil;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -99,9 +96,7 @@ public class ThemeController extends BaseController {
configuration.setSharedVariable("themeName", siteTheme);
configuration.setSharedVariable("options", HaloConst.OPTIONS);
log.info("Changed theme to {}", siteTheme);
logsService.saveByLogs(
new Logs(LogsRecord.CHANGE_THEME, "更换为" + siteTheme, ServletUtil.getClientIP(request), DateUtil.date())
);
logsService.save(LogsRecord.CHANGE_THEME, "更换为" + siteTheme, request);
return new JsonResult(ResultCodeEnum.SUCCESS.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-success", new Object[]{siteTheme}));
} catch (Exception e) {
return new JsonResult(ResultCodeEnum.FAIL.getCode(), localeMessageUtil.getMessage("code.admin.theme.change-failed"));
@ -126,9 +121,7 @@ public class ThemeController extends BaseController {
File themePath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(file.getOriginalFilename()).toString());
file.transferTo(themePath);
log.info("Upload topic success, path is " + themePath.getAbsolutePath());
logsService.saveByLogs(
new Logs(LogsRecord.UPLOAD_THEME, file.getOriginalFilename(), ServletUtil.getClientIP(request), DateUtil.date())
);
logsService.save(LogsRecord.UPLOAD_THEME, file.getOriginalFilename(), request);
ZipUtil.unzip(themePath, new File(basePath.getAbsolutePath(), "templates/themes/"));
FileUtil.del(themePath);
HaloConst.THEMES.clear();

View File

@ -75,6 +75,7 @@ public class ApiPostController {
public JsonResult posts(@PathVariable(value = "postId") Long postId) {
Post post = postService.findByPostId(postId, PostTypeEnum.POST_TYPE_POST.getDesc());
if (null != post) {
postService.cacheViews(post.getPostId());
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), post);
} else {
return new JsonResult(ResponseStatusEnum.NOTFOUND.getCode(), ResponseStatusEnum.NOTFOUND.getMsg());

View File

@ -11,7 +11,6 @@ import cc.ryanc.halo.utils.MarkdownUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.servlet.ServletUtil;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -180,14 +179,7 @@ public class InstallController {
optionsService.saveOption(BlogPropertiesEnum.COMMENT_REPLY_NOTICE.getProp(), TrueFalseEnum.FALSE.getDesc());
//更新日志
logsService.saveByLogs(
new Logs(
LogsRecord.INSTALL,
"安装成功欢迎使用Halo。",
ServletUtil.getClientIP(request),
DateUtil.date()
)
);
logsService.save(LogsRecord.INSTALL, "安装成功欢迎使用Halo。", request);
Menu menuIndex = new Menu();
menuIndex.setMenuName("首页");