From d25d5e6e35cfb125968c401e581a5db0f8da301c Mon Sep 17 00:00:00 2001 From: wu <303054730@qq.com> Date: Thu, 5 Dec 2024 09:52:09 +0800 Subject: [PATCH] fix --- .../com/lk/paopao/service/AttachService.java | 55 +------------------ 1 file changed, 2 insertions(+), 53 deletions(-) diff --git a/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AttachService.java b/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AttachService.java index 9d63dde..7a09410 100644 --- a/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AttachService.java +++ b/examples/chapter07/paopao/src/main/java/com/lk/paopao/service/AttachService.java @@ -1,22 +1,14 @@ -package com.lk.paopao.service; +package com.lk.paopao.services; + -import com.lk.paopao.dto.AttachmentDto; -import com.lk.paopao.entity.Attachment; -import com.lk.paopao.entity.User; -import com.lk.paopao.mapper.AttachmentMapper; -import com.lk.paopao.repository.AttachmentRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; 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.util.HashMap; -import java.util.Map; import java.util.UUID; @Service @@ -30,49 +22,6 @@ public class AttachService { @Value("${app.upload.sub-path-name-length}") int SUB_PATH_LENGTH = 1; - @Autowired - AuthService authService; - @Autowired - AttachmentRepository attachmentRepository; - @Autowired - AttachmentMapper attachmentMapper; - - - /** - * 上传附件 - * @param file 文件 - * @param attachType 附件类型 - * @return AttachmentDto - * @throws IOException 输入输出异常 - */ - public AttachmentDto upload(MultipartFile file) throws IOException { - // 获取原始文件名 - String originFileName = file.getOriginalFilename(); - if(originFileName == null){ - throw new IllegalArgumentException("没有原始文件名"); - } - - InputStream inputStream = file.getInputStream(); - // 获取当前用户 - User currentUser = authService.getCurrentUser(); - // 上传文件并获取相对URL - String relativeUrl = save(originFileName,inputStream); - Attachment attachment = new Attachment(); - // 设置附件类型 - attachment.setType(getType(file.getContentType())); - // 设置附件内容(即存储的URL) - attachment.setContent(relativeUrl); - // 关联用户 - attachment.setUser(currentUser); - // 设置文件大小 - attachment.setFileSize(file.getSize()); - // 保存附件 - Attachment savedAttachment = attachmentRepository.save(attachment); - // 转换并返回附件DTO - return attachmentMapper.toDto(savedAttachment); - } - - /** * 保存文件到上传目录中 * @param fileName 原文件名