mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
fix
This commit is contained in:
parent
e818447644
commit
d25d5e6e35
@ -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 原文件名
|
||||
|
Loading…
Reference in New Issue
Block a user