mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-11-04 10:57:10 +08:00 
			
		
		
		
	feat(system/file): 支持原生文件上传 (#166)
This commit is contained in:
		@@ -27,6 +27,7 @@ import top.continew.starter.core.constant.StringConstants;
 | 
				
			|||||||
import top.continew.starter.data.mp.service.IService;
 | 
					import top.continew.starter.data.mp.service.IService;
 | 
				
			||||||
import top.continew.starter.extension.crud.service.BaseService;
 | 
					import top.continew.starter.extension.crud.service.BaseService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.time.LocalDate;
 | 
					import java.time.LocalDate;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
@@ -73,6 +74,40 @@ public interface FileService extends BaseService<FileResp, FileResp, FileQuery,
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException;
 | 
					    FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 上传到默认存储
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param file 文件信息
 | 
				
			||||||
 | 
					     * @return 文件信息
 | 
				
			||||||
 | 
					     * @throws IOException /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    default FileInfo upload(File file) throws IOException {
 | 
				
			||||||
 | 
					        return upload(file, getDefaultParentPath(), null);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 上传到默认存储
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param file       文件信息
 | 
				
			||||||
 | 
					     * @param parentPath 上级目录
 | 
				
			||||||
 | 
					     * @return 文件信息
 | 
				
			||||||
 | 
					     * @throws IOException /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    default FileInfo upload(File file, String parentPath) throws IOException {
 | 
				
			||||||
 | 
					        return upload(file, parentPath, null);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 上传到指定存储
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param file        文件信息
 | 
				
			||||||
 | 
					     * @param parentPath  上级目录
 | 
				
			||||||
 | 
					     * @param storageCode 存储编码
 | 
				
			||||||
 | 
					     * @return 文件信息
 | 
				
			||||||
 | 
					     * @throws IOException /
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    FileInfo upload(File file, String parentPath, String storageCode) throws IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 创建目录
 | 
					     * 创建目录
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,6 +47,7 @@ import top.continew.starter.core.validation.CheckUtils;
 | 
				
			|||||||
import top.continew.starter.core.validation.ValidationUtils;
 | 
					import top.continew.starter.core.validation.ValidationUtils;
 | 
				
			||||||
import top.continew.starter.extension.crud.service.BaseServiceImpl;
 | 
					import top.continew.starter.extension.crud.service.BaseServiceImpl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
@@ -93,6 +94,19 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
 | 
				
			|||||||
    public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException {
 | 
					    public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException {
 | 
				
			||||||
        // 校验文件格式
 | 
					        // 校验文件格式
 | 
				
			||||||
        String extName = FileNameUtil.extName(file.getOriginalFilename());
 | 
					        String extName = FileNameUtil.extName(file.getOriginalFilename());
 | 
				
			||||||
 | 
					        return getFileInfo(file, parentPath, storageCode, extName);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 上传文件并返回上传后的文件信息
 | 
				
			||||||
 | 
					     * 
 | 
				
			||||||
 | 
					     * @param file
 | 
				
			||||||
 | 
					     * @param parentPath
 | 
				
			||||||
 | 
					     * @param storageCode
 | 
				
			||||||
 | 
					     * @param extName
 | 
				
			||||||
 | 
					     * @return
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private FileInfo getFileInfo(Object file, String parentPath, String storageCode, String extName) {
 | 
				
			||||||
        List<String> allExtensions = FileTypeEnum.getAllExtensions();
 | 
					        List<String> allExtensions = FileTypeEnum.getAllExtensions();
 | 
				
			||||||
        CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
 | 
					        CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String
 | 
				
			||||||
            .join(StringConstants.COMMA, allExtensions));
 | 
					            .join(StringConstants.COMMA, allExtensions));
 | 
				
			||||||
@@ -131,6 +145,13 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
 | 
				
			|||||||
        return uploadPretreatment.upload();
 | 
					        return uploadPretreatment.upload();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public FileInfo upload(File file, String parentPath, String storageCode) throws IOException {
 | 
				
			||||||
 | 
					        // 校验文件格式
 | 
				
			||||||
 | 
					        String extName = FileNameUtil.extName(file.getName());
 | 
				
			||||||
 | 
					        return getFileInfo(file, parentPath, storageCode, extName);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public Long createDir(FileReq req) {
 | 
					    public Long createDir(FileReq req) {
 | 
				
			||||||
        String parentPath = req.getParentPath();
 | 
					        String parentPath = req.getParentPath();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user