1.新增 log 日志上传功能
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -125,6 +125,13 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.x-file-storage</groupId>
|
||||
<artifactId>x-file-storage-spring</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.yupi.springbootinit.controller;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 13:36
|
||||
*/
|
||||
|
||||
import com.yupi.springbootinit.common.BaseResponse;
|
||||
import com.yupi.springbootinit.common.ResultUtils;
|
||||
import com.yupi.springbootinit.service.ServerAiLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/log")
|
||||
@Slf4j
|
||||
@CrossOrigin
|
||||
public class FileController {
|
||||
|
||||
@Resource
|
||||
private ServerAiLogService serverAiLogService;
|
||||
|
||||
@PostMapping("upload")
|
||||
public BaseResponse<Boolean> upload(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam Long tenantId,
|
||||
@RequestParam Long userId) {
|
||||
return ResultUtils.success(serverAiLogService.saveLog(file,tenantId,userId));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.yupi.springbootinit.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.yupi.springbootinit.model.entity.ServerAiLog;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 13:37
|
||||
*/
|
||||
|
||||
public interface ServerAiLogMapper extends BaseMapper<ServerAiLog> {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.yupi.springbootinit.model.dto.file;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 14:32
|
||||
*/
|
||||
@Data
|
||||
public class FileDTO {
|
||||
/**
|
||||
* 日志租户
|
||||
*/
|
||||
@TableField(value = "log_tenant_id")
|
||||
@ApiModelProperty(value="日志租户")
|
||||
private Long logTenantId;
|
||||
|
||||
/**
|
||||
* 用户 Id
|
||||
*/
|
||||
@TableField(value = "log_user_id")
|
||||
@ApiModelProperty(value="用户 Id")
|
||||
private Long logUserId;
|
||||
|
||||
@ApiModelProperty(value="日志文件")
|
||||
private MultipartFile file;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.yupi.springbootinit.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 13:37
|
||||
*/
|
||||
|
||||
@ApiModel(description="server_ai_log")
|
||||
@Data
|
||||
@TableName(value = "server_ai_log")
|
||||
public class ServerAiLog {
|
||||
/**
|
||||
* 日志主键
|
||||
*/
|
||||
@TableId(value = "log_id", type = IdType.AUTO)
|
||||
@ApiModelProperty(value="日志主键")
|
||||
private Long logId;
|
||||
|
||||
/**
|
||||
* 日志主键
|
||||
*/
|
||||
@TableField(value = "log_name")
|
||||
@ApiModelProperty(value="日志主键")
|
||||
private String logName;
|
||||
|
||||
/**
|
||||
* 日志路径
|
||||
*/
|
||||
@TableField(value = "log_file_path")
|
||||
@ApiModelProperty(value="日志路径")
|
||||
private String logFilePath;
|
||||
|
||||
/**
|
||||
* 日志租户
|
||||
*/
|
||||
@TableField(value = "log_tenant_id")
|
||||
@ApiModelProperty(value="日志租户")
|
||||
private Long logTenantId;
|
||||
|
||||
/**
|
||||
* 用户 Id
|
||||
*/
|
||||
@TableField(value = "log_user_id")
|
||||
@ApiModelProperty(value="用户 Id")
|
||||
private Long logUserId;
|
||||
|
||||
@TableField(value = "creator")
|
||||
@ApiModelProperty(value="")
|
||||
private String creator;
|
||||
|
||||
@TableField(value = "create_time")
|
||||
@ApiModelProperty(value="")
|
||||
private Date createTime;
|
||||
|
||||
@TableField(value = "updater")
|
||||
@ApiModelProperty(value="")
|
||||
private String updater;
|
||||
|
||||
@TableField(value = "update_time")
|
||||
@ApiModelProperty(value="")
|
||||
private Date updateTime;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.yupi.springbootinit.service;
|
||||
|
||||
import com.yupi.springbootinit.model.entity.ServerAiLog;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 13:37
|
||||
*/
|
||||
|
||||
public interface ServerAiLogService extends IService<ServerAiLog>{
|
||||
|
||||
|
||||
Boolean saveLog(MultipartFile file, Long tenantId, Long userId);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.yupi.springbootinit.service.impl;
|
||||
|
||||
import com.yupi.springbootinit.common.ErrorCode;
|
||||
import com.yupi.springbootinit.exception.BusinessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yupi.springbootinit.mapper.ServerAiLogMapper;
|
||||
import com.yupi.springbootinit.model.entity.ServerAiLog;
|
||||
import com.yupi.springbootinit.service.ServerAiLogService;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
/*
|
||||
* @author: ziin
|
||||
* @date: 2025/8/25 13:37
|
||||
*/
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ServerAiLogServiceImpl extends ServiceImpl<ServerAiLogMapper, ServerAiLog> implements ServerAiLogService{
|
||||
|
||||
@Resource
|
||||
private ServerAiLogMapper serverAiLogMapper;
|
||||
|
||||
@Value("${ai_log_path}")
|
||||
private String basePath;
|
||||
|
||||
@Override
|
||||
public Boolean saveLog(MultipartFile file, Long tenantId, Long userId) {
|
||||
|
||||
if (file.isEmpty()) {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"上传文件为空");
|
||||
}
|
||||
Path dir = Paths.get(basePath, tenantId.toString(), userId.toString());
|
||||
if (!Files.exists(dir)) {
|
||||
try {
|
||||
Files.createDirectories(dir);
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"创建目录失败");
|
||||
}
|
||||
}
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
Path target = dir.resolve(originalFilename);
|
||||
try {
|
||||
Files.copy(file.getInputStream(), target, StandardCopyOption.REPLACE_EXISTING);
|
||||
log.info("文件名{},文件上传路径{},",originalFilename,target);
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(ErrorCode.SYSTEM_ERROR,"保存文件失败");
|
||||
}
|
||||
ServerAiLog log = new ServerAiLog();
|
||||
log.setLogName(originalFilename);
|
||||
log.setLogFilePath(target.toString());
|
||||
log.setLogTenantId(tenantId);
|
||||
log.setLogUserId(userId);
|
||||
log.setCreator(String.valueOf(userId)); // 可替换为当前登录用户
|
||||
return serverAiLogMapper.insert(log) == 1;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -109,4 +109,4 @@ sa-token:
|
||||
md5:
|
||||
salt: (-FhqvXO,wMz
|
||||
|
||||
ai_log_path: /test/ai_log/
|
||||
ai_log_path: /test/ai_log
|
||||
22
src/main/resources/mapper/ServerAiLogMapper.xml
Normal file
22
src/main/resources/mapper/ServerAiLogMapper.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.yupi.springbootinit.mapper.ServerAiLogMapper">
|
||||
<resultMap id="BaseResultMap" type="com.yupi.springbootinit.model.entity.ServerAiLog">
|
||||
<!--@mbg.generated-->
|
||||
<!--@Table server_ai_log-->
|
||||
<id column="log_id" jdbcType="BIGINT" property="logId" />
|
||||
<result column="log_name" jdbcType="VARCHAR" property="logName" />
|
||||
<result column="log_file_path" jdbcType="VARCHAR" property="logFilePath" />
|
||||
<result column="log_tenant_id" jdbcType="BIGINT" property="logTenantId" />
|
||||
<result column="log_user_id" jdbcType="BIGINT" property="logUserId" />
|
||||
<result column="creator" jdbcType="VARCHAR" property="creator" />
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
|
||||
<result column="updater" jdbcType="VARCHAR" property="updater" />
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List">
|
||||
<!--@mbg.generated-->
|
||||
log_id, log_name, log_file_path, log_tenant_id, log_user_id, creator, create_time,
|
||||
updater, update_time
|
||||
</sql>
|
||||
</mapper>
|
||||
@@ -10,7 +10,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
* @from <a href="https://yupi.icu">编程导航知识星球</a>
|
||||
*/
|
||||
@SpringBootTest
|
||||
class MainApplicationTests {
|
||||
class
|
||||
MainApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
|
||||
Reference in New Issue
Block a user