feat: add upload img logic
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -87,6 +87,14 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>application.yml</exclude>
|
||||||
|
</excludes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package xin.merlin.myblog_server.controller;
|
|||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import xin.merlin.myblog_server.entity.Article;
|
import xin.merlin.myblog_server.entity.Article;
|
||||||
import xin.merlin.myblog_server.entity.Comment;
|
import xin.merlin.myblog_server.entity.Comment;
|
||||||
import xin.merlin.myblog_server.entity.News;
|
import xin.merlin.myblog_server.entity.News;
|
||||||
@@ -15,6 +17,14 @@ import xin.merlin.myblog_server.utils.JwtUtil;
|
|||||||
import xin.merlin.myblog_server.utils.RequestBack;
|
import xin.merlin.myblog_server.utils.RequestBack;
|
||||||
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/admin")
|
@RequestMapping("/admin")
|
||||||
public class AdminController {
|
public class AdminController {
|
||||||
@@ -32,6 +42,9 @@ public class AdminController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CommentServiceImpl commentService;
|
private CommentServiceImpl commentService;
|
||||||
|
|
||||||
|
@Value("${upload.dir}")
|
||||||
|
private String uploadDir;
|
||||||
|
|
||||||
//编辑,新增,删除新闻
|
//编辑,新增,删除新闻
|
||||||
@PostMapping("/update/news")
|
@PostMapping("/update/news")
|
||||||
RequestBack editNews(@RequestBody News news, @RequestHeader("Authorization")String token) {
|
RequestBack editNews(@RequestBody News news, @RequestHeader("Authorization")String token) {
|
||||||
@@ -100,4 +113,30 @@ public class AdminController {
|
|||||||
commentService.removeById(user.getId());
|
commentService.removeById(user.getId());
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
return RequestBack.success(ResultCode.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//上传照片
|
||||||
|
@PostMapping("/upload/img")
|
||||||
|
RequestBack uploadImg(@RequestHeader("Authorization")String token, @RequestParam("image") MultipartFile file) throws IOException {
|
||||||
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
||||||
|
if (file == null || file.isEmpty()) {
|
||||||
|
return RequestBack.fail(ResultCode.NOT_FOUND);
|
||||||
|
}
|
||||||
|
String original = file.getOriginalFilename();
|
||||||
|
String ext = "";
|
||||||
|
if (original != null && original.contains(".")) {
|
||||||
|
ext = original.substring(original.lastIndexOf('.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
String filename = UUID.randomUUID().toString() + ext;
|
||||||
|
|
||||||
|
Path dirPath = Paths.get(uploadDir);
|
||||||
|
Files.createDirectories(dirPath);
|
||||||
|
|
||||||
|
Path target = dirPath.resolve(filename);
|
||||||
|
try (InputStream in = file.getInputStream()) {
|
||||||
|
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RequestBack.success(ResultCode.SUCCESS, "https://blog.merlin.xin/app/uploads/"+filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ public class BasicController {
|
|||||||
@GetMapping("/get/articles")
|
@GetMapping("/get/articles")
|
||||||
public RequestBack getArticles(@RequestParam Integer current,@RequestParam Integer size) {
|
public RequestBack getArticles(@RequestParam Integer current,@RequestParam Integer size) {
|
||||||
Page<Article> page = new Page<>(current,size);
|
Page<Article> page = new Page<>(current,size);
|
||||||
|
page.setOrders(List.of(OrderItem.desc("published")));
|
||||||
return RequestBack.success(ResultCode.SUCCESS,articleService.page(page));
|
return RequestBack.success(ResultCode.SUCCESS,articleService.page(page));
|
||||||
}
|
}
|
||||||
// 发表评论
|
// 发表评论
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8081
|
||||||
|
|
||||||
jwt:
|
jwt:
|
||||||
secret: CkmEXxVBNBsMUo4VNhDcH0YBhA1O4zSkQgSM243YzDY=
|
secret: CkmEXxVBNBsMUo4VNhDcH0YBhA1O4zSkQgSM243YzDY=
|
||||||
@@ -7,6 +7,8 @@ jwt:
|
|||||||
subject: Interesting
|
subject: Interesting
|
||||||
expire: 604800
|
expire: 604800
|
||||||
|
|
||||||
|
upload:
|
||||||
|
dir: c:/uploads
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
servlet:
|
servlet:
|
||||||
@@ -21,8 +23,6 @@ spring:
|
|||||||
jackson:
|
jackson:
|
||||||
time-zone: Asia/Shanghai
|
time-zone: Asia/Shanghai
|
||||||
date-format: yyyy-MM-dd HH:mm:ss
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
|
||||||
|
|
||||||
mail:
|
mail:
|
||||||
protocol: smtps
|
protocol: smtps
|
||||||
port: 465
|
port: 465
|
||||||
|
|||||||
Reference in New Issue
Block a user