mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat: 增加图片上传接口
This commit is contained in:
parent
c431683540
commit
282755f7ab
src/main
java/io/github/xxyopen/novel
controller/front
core
common/constant
constant
json/deserializer
service
resources
@ -5,9 +5,8 @@ import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
|||||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||||
import io.github.xxyopen.novel.service.ResourceService;
|
import io.github.xxyopen.novel.service.ResourceService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -32,4 +31,12 @@ public class ResourceController {
|
|||||||
return resourceService.getImgVerifyCode();
|
return resourceService.getImgVerifyCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片上传接口
|
||||||
|
* */
|
||||||
|
@PostMapping("/image")
|
||||||
|
RestResp<String> uploadImage(@RequestParam("file") MultipartFile file) {
|
||||||
|
return resourceService.uploadImage(file);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,16 @@ public enum ErrorCodeEnum {
|
|||||||
* */
|
* */
|
||||||
USER_COMMENTED("A2001","用户已发表评论"),
|
USER_COMMENTED("A2001","用户已发表评论"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户上传文件异常
|
||||||
|
* */
|
||||||
|
USER_UPLOAD_FILE_ERROR("A0700","用户上传文件异常"),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户上传文件类型不匹配
|
||||||
|
* */
|
||||||
|
USER_UPLOAD_FILE_TYPE_NOT_MATCH("A0701","用户上传文件类型不匹配"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一级宏观错误码,系统执行出错
|
* 一级宏观错误码,系统执行出错
|
||||||
* */
|
* */
|
||||||
|
@ -32,6 +32,11 @@ public class SystemConfigConsts {
|
|||||||
* */
|
* */
|
||||||
public static final String NOVEL_ADMIN_KEY = "admin";
|
public static final String NOVEL_ADMIN_KEY = "admin";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片上传目录
|
||||||
|
* */
|
||||||
|
public static final String IMAGE_UPLOAD_DIRECTORY = "/image/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 常量类实例化异常信息
|
* 常量类实例化异常信息
|
||||||
* */
|
* */
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package io.github.xxyopen.novel.core.json.deserializer;
|
package io.github.xxyopen.novel.core.json.deserializer;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JacksonException;
|
|
||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
@ -25,7 +24,7 @@ public class GlobalJsonDeserializer {
|
|||||||
public static class StringDeserializer extends JsonDeserializer<String> {
|
public static class StringDeserializer extends JsonDeserializer<String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
|
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||||
return jsonParser.getValueAsString()
|
return jsonParser.getValueAsString()
|
||||||
.replace("<", "<")
|
.replace("<", "<")
|
||||||
.replace(">", ">");
|
.replace(">", ">");
|
||||||
|
@ -2,6 +2,7 @@ package io.github.xxyopen.novel.service;
|
|||||||
|
|
||||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -16,7 +17,15 @@ public interface ResourceService {
|
|||||||
/**
|
/**
|
||||||
* 获取图片验证码
|
* 获取图片验证码
|
||||||
*
|
*
|
||||||
|
* @throws IOException 验证码图片生成失败
|
||||||
* @return Base64编码的图片
|
* @return Base64编码的图片
|
||||||
*/
|
*/
|
||||||
RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException;
|
RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片上传
|
||||||
|
* @param file 需要上传的图片
|
||||||
|
* @return 图片访问路径
|
||||||
|
* */
|
||||||
|
RestResp<String> uploadImage(MultipartFile file);
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,27 @@
|
|||||||
package io.github.xxyopen.novel.service.impl;
|
package io.github.xxyopen.novel.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||||
|
import io.github.xxyopen.novel.core.common.constant.ErrorCodeEnum;
|
||||||
|
import io.github.xxyopen.novel.core.common.exception.BusinessException;
|
||||||
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
import io.github.xxyopen.novel.core.common.resp.RestResp;
|
||||||
|
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||||
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
import io.github.xxyopen.novel.dto.resp.ImgVerifyCodeRespDto;
|
||||||
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
import io.github.xxyopen.novel.manager.VerifyCodeManager;
|
||||||
import io.github.xxyopen.novel.service.ResourceService;
|
import io.github.xxyopen.novel.service.ResourceService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 资源(图片/视频/文档)相关服务实现类
|
* 资源(图片/视频/文档)相关服务实现类
|
||||||
@ -18,10 +31,14 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class ResourceServiceImpl implements ResourceService {
|
public class ResourceServiceImpl implements ResourceService {
|
||||||
|
|
||||||
private final VerifyCodeManager verifyCodeManager;
|
private final VerifyCodeManager verifyCodeManager;
|
||||||
|
|
||||||
|
@Value("${novel.file.upload.path}")
|
||||||
|
private String fileUploadPath;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
public RestResp<ImgVerifyCodeRespDto> getImgVerifyCode() throws IOException {
|
||||||
String sessionId = IdWorker.get32UUID();
|
String sessionId = IdWorker.get32UUID();
|
||||||
@ -31,4 +48,32 @@ public class ResourceServiceImpl implements ResourceService {
|
|||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
@Override
|
||||||
|
public RestResp<String> uploadImage(MultipartFile file) {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
String savePath =
|
||||||
|
SystemConfigConsts.IMAGE_UPLOAD_DIRECTORY
|
||||||
|
+ now.format(DateTimeFormatter.ofPattern("yyyy")) + File.separator
|
||||||
|
+ now.format(DateTimeFormatter.ofPattern("MM")) + File.separator
|
||||||
|
+ now.format(DateTimeFormatter.ofPattern("dd"));
|
||||||
|
String oriName = file.getOriginalFilename();
|
||||||
|
assert oriName != null;
|
||||||
|
String saveFileName = IdWorker.get32UUID() + oriName.substring(oriName.lastIndexOf("."));
|
||||||
|
File saveFile = new File(fileUploadPath + savePath, saveFileName);
|
||||||
|
if (!saveFile.getParentFile().exists()) {
|
||||||
|
boolean isSuccess = saveFile.getParentFile().mkdirs();
|
||||||
|
if (!isSuccess) {
|
||||||
|
throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.transferTo(saveFile);
|
||||||
|
if (Objects.isNull(ImageIO.read(saveFile))) {
|
||||||
|
// 上传的文件不是图片
|
||||||
|
Files.delete(saveFile.toPath());
|
||||||
|
throw new BusinessException(ErrorCodeEnum.USER_UPLOAD_FILE_TYPE_NOT_MATCH);
|
||||||
|
}
|
||||||
|
return RestResp.ok(savePath + File.separator + saveFileName);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,11 @@ novel:
|
|||||||
# 排除链接
|
# 排除链接
|
||||||
excludes:
|
excludes:
|
||||||
- /system/notice/*
|
- /system/notice/*
|
||||||
|
file:
|
||||||
|
# 文件上传配置
|
||||||
|
upload:
|
||||||
|
# 上传路径
|
||||||
|
path: /var/upload
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user