修改保存目录为可配置

This commit is contained in:
黎润豪 2025-10-31 17:13:18 +08:00
parent 6503e8a5d9
commit b63f3bd0e8
2 changed files with 37 additions and 34 deletions

View File

@ -1,34 +1,32 @@
package com.chinaweal.youfool.course.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.youfool.course.entity.Course;
import com.chinaweal.youfool.course.entity.CourseVideo;
import com.chinaweal.youfool.course.entity.CourseAttachment;
import com.chinaweal.youfool.course.entity.CourseComment;
import com.chinaweal.youfool.course.service.CourseService;
import com.chinaweal.youfool.course.service.CourseVideoService;
import com.chinaweal.youfool.course.entity.CourseVideo;
import com.chinaweal.youfool.course.service.CourseAttachmentService;
import com.chinaweal.youfool.course.service.CourseCommentService;
import com.chinaweal.youfool.course.service.CourseService;
import com.chinaweal.youfool.course.service.CourseVideoService;
import com.chinaweal.youfool.framework.springboot.rest.BaseResultCode;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.BaseResultCode;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.UUID;
/**
@ -37,6 +35,7 @@ import java.util.UUID;
* @author lroyia
* @since 2025/10/24
**/
@Slf4j
@Controller
public class PageController {
@ -52,6 +51,9 @@ public class PageController {
@Autowired
private CourseCommentService courseCommentService;
@Value("${file.savePath:}")
private String fileSavePath;
/**
* 登录页面
*
@ -103,9 +105,9 @@ public class PageController {
*/
@GetMapping("/index")
public String indexPage(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String courseName,
Model model) {
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) String courseName,
Model model) {
if (!StpUtil.isLogin()) {
return "redirect:/course/login";
}
@ -125,15 +127,15 @@ public class PageController {
/**
* 课程详情页面
*
* @param id 课程ID
* @param id 课程ID
* @param commentPage 评论页码
* @param model 模型对象
* @param model 模型对象
* @return 课程详情页面
*/
@GetMapping("/detail/{id}")
public String courseDetail(@PathVariable String id,
@RequestParam(defaultValue = "1") Integer commentPage,
Model model) {
@RequestParam(defaultValue = "1") Integer commentPage,
Model model) {
if (!StpUtil.isLogin()) {
return "redirect:/course/login";
}
@ -247,11 +249,11 @@ public class PageController {
/**
* 发布课程
*
* @param courseName 课程名称
* @param courseDesc 课程描述
* @param videoFile 课程视频文件可选
* @param attachment 课程附件文件可选
* @param model 模型对象
* @param courseName 课程名称
* @param courseDesc 课程描述
* @param videoFile 课程视频文件可选
* @param attachment 课程附件文件可选
* @param model 模型对象
* @return 发布结果
*/
@PostMapping("/publish")
@ -263,7 +265,7 @@ public class PageController {
@RequestParam(required = false) MultipartFile videoFile,
@RequestParam(required = false) MultipartFile attachment,
Model model) {
if (!StpUtil.isLogin()) {
return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "请先登录");
}
@ -284,7 +286,7 @@ public class PageController {
course.setCourseDate(LocalDate.now());
course.setCreateBy(StpUtil.getLoginIdAsString());
course.setCreateTime(LocalDateTime.now());
boolean courseSaved = courseService.save(course);
if (!courseSaved) {
return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "创建课程失败");
@ -303,7 +305,7 @@ public class PageController {
courseVideo.setAttachmentId(videoAttachmentId);
courseVideo.setCreateBy(StpUtil.getLoginIdAsString());
courseVideo.setCreateTime(LocalDateTime.now());
courseVideoService.save(courseVideo);
}
}
@ -313,10 +315,10 @@ public class PageController {
saveUploadFile(attachment, courseId, "attachment");
}
return RestResult.ok(courseId, "课程发布成功");
return RestResult.ok(courseId);
} catch (Exception e) {
e.printStackTrace();
log.error("课程发布失败", e);
return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程发布失败:" + e.getMessage());
}
}
@ -344,9 +346,7 @@ public class PageController {
String newFileName = UUID.randomUUID().toString() + "." + fileFormat;
// 创建上传目录
String uploadDir = "../uploads/courses/" + LocalDate.now().getYear() + "/" +
String.format("%02d", LocalDate.now().getMonthValue()) + "/" +
String.format("%02d", LocalDate.now().getDayOfMonth());
String uploadDir = fileSavePath + "/" + courseId;
Path uploadPath = Paths.get(uploadDir);
if (!uploadPath.toFile().exists()) {
uploadPath.toFile().mkdirs();

View File

@ -79,6 +79,9 @@ server:
context-path: ${COURSE_CONTEXT_PATH:/course}
port: ${COURSE_PORT:8080}
file:
savePath: ${FILE_SAVE_PATH:../file}
# Sa-Token配置
sa-token:
# token名称 (同时也是cookie名称)