From b63f3bd0e8df637b74cf954911d840b04aac31ef Mon Sep 17 00:00:00 2001 From: lroyia Date: Fri, 31 Oct 2025 17:13:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BF=9D=E5=AD=98=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E4=B8=BA=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../course/controller/PageController.java | 68 +++++++++---------- src/main/resources/application.yml | 3 + 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/chinaweal/youfool/course/controller/PageController.java b/src/main/java/com/chinaweal/youfool/course/controller/PageController.java index 9212a21..812ffc7 100644 --- a/src/main/java/com/chinaweal/youfool/course/controller/PageController.java +++ b/src/main/java/com/chinaweal/youfool/course/controller/PageController.java @@ -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(); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 460d2f1..7900991 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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名称)