模块化拆分
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
package cn.celess.extension.controller;
|
||||
|
||||
import cn.celess.common.enmu.ResponseEnum;
|
||||
import cn.celess.common.entity.Response;
|
||||
import cn.celess.common.entity.vo.QiniuResponse;
|
||||
import cn.celess.common.exception.MyException;
|
||||
import cn.celess.common.service.CountService;
|
||||
import cn.celess.common.service.QiniuService;
|
||||
import cn.celess.common.util.HttpUtil;
|
||||
import cn.celess.common.util.RedisUtil;
|
||||
import cn.celess.common.util.VeriCodeUtil;
|
||||
import cn.celess.user.util.RedisUserUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 22:03
|
||||
*/
|
||||
@RestController
|
||||
public class ExtensionController {
|
||||
public static final Logger logger = LoggerFactory.getLogger(Object.class);
|
||||
|
||||
@Autowired
|
||||
CountService countService;
|
||||
@Autowired
|
||||
QiniuService qiniuService;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
@Autowired
|
||||
RedisUserUtil redisUserUtil;
|
||||
|
||||
|
||||
@GetMapping("/counts")
|
||||
public Response allCount() {
|
||||
Map<String, Long> countMap = new HashMap<>();
|
||||
countMap.put("articleCount", countService.getArticleCount());
|
||||
countMap.put("commentCount", countService.getCommentCount());
|
||||
countMap.put("categoryCount", countService.getCategoriesCount());
|
||||
countMap.put("tagCount", countService.getTagsCount());
|
||||
countMap.put("visitorCount", countService.getVisitorCount());
|
||||
return Response.success(countMap);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取header的全部参数
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @return Response
|
||||
*/
|
||||
@GetMapping("/headerInfo")
|
||||
public Response headerInfo(HttpServletRequest request) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
String str = null;
|
||||
while (headerNames.hasMoreElements()) {
|
||||
str = headerNames.nextElement();
|
||||
map.put(str, request.getHeader(str));
|
||||
}
|
||||
map.put("sessionID", request.getSession().getId());
|
||||
map.put("request.getRemoteAddr()", request.getRemoteAddr());
|
||||
return Response.success(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回验证码
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
@GetMapping(value = "/imgCode", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public void getImg(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
Object[] obj = VeriCodeUtil.createImage();
|
||||
request.getSession().setAttribute("code", obj[0]);
|
||||
//将图片输出给浏览器
|
||||
BufferedImage image = (BufferedImage) obj[1];
|
||||
response.setContentType("image/png");
|
||||
OutputStream os = response.getOutputStream();
|
||||
ImageIO.write(image, "png", os);
|
||||
os.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 验证码的正确性
|
||||
*
|
||||
* @param code 传进来的验证码
|
||||
* @param request HttpServletRequest
|
||||
* @return Session中写入验证状态
|
||||
*/
|
||||
@PostMapping("/verCode")
|
||||
public Response verCode(@RequestParam("code") String code, HttpServletRequest request) {
|
||||
request.getSession().setAttribute("verImgCodeStatus", false);
|
||||
String codeStr = (String) request.getSession().getAttribute("code");
|
||||
if (code == null) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
if (codeStr == null) {
|
||||
throw new MyException(ResponseEnum.IMG_CODE_TIMEOUT);
|
||||
}
|
||||
code = code.toLowerCase();
|
||||
codeStr = codeStr.toLowerCase();
|
||||
if (code.equals(codeStr)) {
|
||||
request.getSession().removeAttribute("code");
|
||||
request.getSession().setAttribute("verImgCodeStatus", true);
|
||||
return Response.success("验证成功");
|
||||
} else {
|
||||
request.getSession().removeAttribute("code");
|
||||
return Response.failure("验证失败,请重新获取验证码");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FIXME :: 单张图片多次上传的问题
|
||||
* editor.md图片上传的接口
|
||||
* FUCK !!!
|
||||
* !! 推荐使用 fileUpload(/fileUpload) 接口
|
||||
* @param file 文件
|
||||
*/
|
||||
@Deprecated
|
||||
@PostMapping("/imgUpload")
|
||||
public void upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("editormd-image-file") MultipartFile file) throws IOException {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String uploadTimesStr = redisUtil.get(request.getRemoteAddr() + "-ImgUploadTimes");
|
||||
int uploadTimes = 0;
|
||||
if (uploadTimesStr != null) {
|
||||
uploadTimes = Integer.parseInt(uploadTimesStr);
|
||||
}
|
||||
if (uploadTimes == 10) {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "上传次数已达10次,请2小时后在上传");
|
||||
}
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
if (file.isEmpty()) {
|
||||
map.put("success", 0);
|
||||
map.put("message", "上传失败,请选择文件");
|
||||
response.getWriter().println(mapper.writeValueAsString(map));
|
||||
return;
|
||||
}
|
||||
String fileName = file.getOriginalFilename();
|
||||
assert fileName != null;
|
||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||
if (".png".equalsIgnoreCase(mime) || ".jpg".equalsIgnoreCase(mime) ||
|
||||
".jpeg".equalsIgnoreCase(mime) || ".bmp".equalsIgnoreCase(mime)) {
|
||||
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
||||
map.put("success", 1);
|
||||
map.put("message", "上传成功");
|
||||
map.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
||||
response.getWriter().println(mapper.writeValueAsString(map));
|
||||
redisUtil.setEx(request.getRemoteAddr() + "-ImgUploadTimes", uploadTimes + 1 + "", 2, TimeUnit.HOURS);
|
||||
return;
|
||||
}
|
||||
map.put("success", 0);
|
||||
map.put("message", "上传失败,请上传图片文件");
|
||||
response.getWriter().println(mapper.writeValueAsString(map));
|
||||
}
|
||||
|
||||
@GetMapping("/bingPic")
|
||||
public Response bingPic() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root;
|
||||
try {
|
||||
root = mapper.readTree(HttpUtil.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN"));
|
||||
} catch (IOException e) {
|
||||
return Response.failure(null);
|
||||
}
|
||||
JsonNode images = root.get("images").elements().next();
|
||||
return Response.success("https://cn.bing.com" + images.get("url").asText());
|
||||
}
|
||||
|
||||
@PostMapping("/fileUpload")
|
||||
public Response<List<Map<String, Object>>> fileUpload(@RequestParam("file[]") MultipartFile[] files) throws IOException {
|
||||
List<Map<String, Object>> result = new ArrayList<>();
|
||||
String uploadTimesStr = redisUtil.get(redisUserUtil.get().getEmail() + "-fileUploadTimes");
|
||||
int uploadTimes = 0;
|
||||
if (uploadTimesStr != null) {
|
||||
uploadTimes = Integer.parseInt(uploadTimesStr);
|
||||
}
|
||||
if (uploadTimes == 10) {
|
||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "上传次数已达10次,请2小时后在上传");
|
||||
}
|
||||
if (files.length == 0) {
|
||||
throw new MyException(ResponseEnum.NO_FILE);
|
||||
}
|
||||
for (MultipartFile file : files) {
|
||||
Map<String, Object> resp = new HashMap<>(4);
|
||||
|
||||
String fileName = file.getOriginalFilename();
|
||||
assert fileName != null;
|
||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||
String name = fileName.replace(mime, "").replaceAll(" ", "");
|
||||
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "file_" + name + '_' + System.currentTimeMillis() + mime);
|
||||
resp.put("originalFilename", fileName);
|
||||
resp.put("success", qiniuResponse != null);
|
||||
if (qiniuResponse != null) {
|
||||
resp.put("host", "http://cdn.celess.cn/");
|
||||
resp.put("path", qiniuResponse.key);
|
||||
}
|
||||
result.add(resp);
|
||||
}
|
||||
return Response.success(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package cn.celess.extension.serviceimpl;
|
||||
|
||||
import cn.celess.common.mapper.*;
|
||||
import cn.celess.common.service.CountService;
|
||||
import cn.celess.common.util.RedisUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/02 22:06
|
||||
*/
|
||||
@Service
|
||||
public class CountServiceImpl implements CountService {
|
||||
@Autowired
|
||||
ArticleMapper articleMapper;
|
||||
@Autowired
|
||||
TagMapper tagMapper;
|
||||
@Autowired
|
||||
CommentMapper commentMapper;
|
||||
@Autowired
|
||||
CategoryMapper categoryMapper;
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
VisitorMapper visitorMapper;
|
||||
@Autowired
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Override
|
||||
public long getCommentCount() {
|
||||
return commentMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getArticleCount() {
|
||||
return articleMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCategoriesCount() {
|
||||
return categoryMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTagsCount() {
|
||||
return tagMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getUserCount() {
|
||||
return userMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVisitorCount() {
|
||||
return visitorMapper.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getDayVisitCount() {
|
||||
String dayVisitCount = redisUtil.get("dayVisitCount");
|
||||
if (dayVisitCount == null) {
|
||||
return 1;
|
||||
}
|
||||
return Integer.parseInt(dayVisitCount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.celess.extension.serviceimpl;
|
||||
|
||||
import cn.celess.common.service.MailService;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/22 14:26
|
||||
*/
|
||||
@Service
|
||||
public class MailServiceImpl implements MailService {
|
||||
public static final String FROM = "<xiaohai2271@163.com>";
|
||||
@Resource
|
||||
JavaMailSender javaMailSender;
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public Boolean AsyncSend(SimpleMailMessage message) {
|
||||
this.send(message);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean send(SimpleMailMessage message) {
|
||||
String nick = null;
|
||||
try {
|
||||
nick = javax.mail.internet.MimeUtility.encodeText("小海博客");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
message.setFrom(nick + FROM);
|
||||
javaMailSender.send(message);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package cn.celess.extension.serviceimpl;
|
||||
|
||||
import cn.celess.common.entity.vo.QiniuResponse;
|
||||
import cn.celess.common.service.QiniuService;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author : xiaohai
|
||||
* @date : 2019/04/25 18:15
|
||||
*/
|
||||
@Service
|
||||
public class QiniuServiceImpl implements QiniuService {
|
||||
private static final Configuration cfg = new Configuration(Zone.zone2());
|
||||
private static UploadManager uploadManager;
|
||||
private static BucketManager bucketManager;
|
||||
private static Auth auth;
|
||||
|
||||
@Value("${qiniu.accessKey}")
|
||||
private String accessKey;
|
||||
@Value("${qiniu.secretKey}")
|
||||
private String secretKey;
|
||||
@Value("${qiniu.bucket}")
|
||||
private String bucket;
|
||||
|
||||
private void init() {
|
||||
if (auth == null || uploadManager == null || bucketManager == null) {
|
||||
auth = Auth.create(accessKey, secretKey);
|
||||
uploadManager = new UploadManager(cfg);
|
||||
bucketManager = new BucketManager(auth, cfg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
init();
|
||||
//文件存在则删除文件
|
||||
if (continueFile(fileName)) {
|
||||
try {
|
||||
System.out.println(bucketManager.delete(bucket, fileName).toString());
|
||||
} catch (QiniuException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//上传
|
||||
try {
|
||||
Response response = uploadManager.put(is, fileName, auth.uploadToken(bucket), null, null);
|
||||
return response.jsonToObject(QiniuResponse.class);
|
||||
} catch (QiniuException e) {
|
||||
Response r = e.response;
|
||||
System.err.println(r.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
init();
|
||||
BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, "", 1000, "");
|
||||
FileInfo[] items = null;
|
||||
while (fileListIterator.hasNext()) {
|
||||
//处理获取的file list结果
|
||||
items = fileListIterator.next();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private boolean continueFile(String key) {
|
||||
FileInfo[] allFile = getFileList();
|
||||
for (FileInfo fileInfo : allFile) {
|
||||
if (key.equals(fileInfo.key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user