修改保存目录为可配置

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; package com.chinaweal.youfool.course.controller;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.youfool.course.entity.Course; 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.CourseAttachment;
import com.chinaweal.youfool.course.entity.CourseComment; import com.chinaweal.youfool.course.entity.CourseComment;
import com.chinaweal.youfool.course.service.CourseService; import com.chinaweal.youfool.course.entity.CourseVideo;
import com.chinaweal.youfool.course.service.CourseVideoService;
import com.chinaweal.youfool.course.service.CourseAttachmentService; import com.chinaweal.youfool.course.service.CourseAttachmentService;
import com.chinaweal.youfool.course.service.CourseCommentService; 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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
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.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder; 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.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
/** /**
@ -37,6 +35,7 @@ import java.util.UUID;
* @author lroyia * @author lroyia
* @since 2025/10/24 * @since 2025/10/24
**/ **/
@Slf4j
@Controller @Controller
public class PageController { public class PageController {
@ -52,6 +51,9 @@ public class PageController {
@Autowired @Autowired
private CourseCommentService courseCommentService; private CourseCommentService courseCommentService;
@Value("${file.savePath:}")
private String fileSavePath;
/** /**
* 登录页面 * 登录页面
* *
@ -313,10 +315,10 @@ public class PageController {
saveUploadFile(attachment, courseId, "attachment"); saveUploadFile(attachment, courseId, "attachment");
} }
return RestResult.ok(courseId, "课程发布成功"); return RestResult.ok(courseId);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("课程发布失败", e);
return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程发布失败:" + e.getMessage()); return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程发布失败:" + e.getMessage());
} }
} }
@ -344,9 +346,7 @@ public class PageController {
String newFileName = UUID.randomUUID().toString() + "." + fileFormat; String newFileName = UUID.randomUUID().toString() + "." + fileFormat;
// 创建上传目录 // 创建上传目录
String uploadDir = "../uploads/courses/" + LocalDate.now().getYear() + "/" + String uploadDir = fileSavePath + "/" + courseId;
String.format("%02d", LocalDate.now().getMonthValue()) + "/" +
String.format("%02d", LocalDate.now().getDayOfMonth());
Path uploadPath = Paths.get(uploadDir); Path uploadPath = Paths.get(uploadDir);
if (!uploadPath.toFile().exists()) { if (!uploadPath.toFile().exists()) {
uploadPath.toFile().mkdirs(); uploadPath.toFile().mkdirs();

View File

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