【8.3.0】【file】更新多文件上传的方法

master
stylefeng 2024-08-20 12:46:32 +08:00
parent 9d4f773c6a
commit fdc1a5b3c8
6 changed files with 237 additions and 26 deletions

View File

@ -0,0 +1,67 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.file.modular.controller;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.file.modular.service.MultipleFileService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2024/8/20 9:00
*/
@RestController
@ApiResource(name = "多文件上传和下载的接口")
public class MultipleFileController {
@Resource
private MultipleFileService multipleFileService;
/**
* jsonbody
*
* @author fengshuonan
* @since 2024/8/20 9:06
*/
@PostResource(name = "多文件上传-带body体", path = "/sysFileInfo/multipleUploadFileList")
public ResponseData<List<SysFileInfoResponse>> upload(@RequestPart("fileList") MultipartFile[] fileList,
@RequestPart("bodyJson") SysFileInfoRequest sysFileInfoRequest) {
List<SysFileInfoResponse> fileResult = this.multipleFileService.batchUploadFile(fileList, sysFileInfoRequest);
return new SuccessResponseData<>(fileResult);
}
}

View File

@ -41,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import static cn.stylefeng.roses.kernel.file.api.constants.FileConstants.FILE_POSTFIX_SEPARATOR;
@ -52,6 +54,20 @@ import static cn.stylefeng.roses.kernel.file.api.constants.FileConstants.FILE_PO
*/
public class FileInfoFactory {
/**
*
*
* @author fengshuonan
* @since 2024/8/20 12:32
*/
public static Map<MultipartFile, SysFileInfo> batchCreateFileInfo(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest) {
Map<MultipartFile, SysFileInfo> result = new HashMap<>();
for (MultipartFile multipartFile : fileList) {
result.put(multipartFile, createSysFileInfo(multipartFile, sysFileInfoRequest));
}
return result;
}
/**
*
*

View File

@ -0,0 +1,49 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.file.modular.service;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2024/8/20 9:02
*/
public interface MultipleFileService {
/**
*
*
* @author fengshuonan
* @since 2024/8/20 12:05
*/
List<SysFileInfoResponse> batchUploadFile(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest);
}

View File

@ -159,4 +159,20 @@ public interface SysFileInfoService extends IService<SysFileInfo>, FileInfoApi {
*/
List<SysFileInfoResponse> getFileInfoListByFileIds(List<Long> fileIdList);
/**
*
*
* @author fengshuonan
* @since 2024/8/20 12:38
*/
void storageFile(MultipartFile file, SysFileInfo sysFileInfo);
/**
*
*
* @author fengshuonan
* @since 2024/8/20 12:43
*/
SysFileInfoResponse getFileInfoResponse(SysFileInfo sysFileInfo);
}

View File

@ -0,0 +1,54 @@
package cn.stylefeng.roses.kernel.file.modular.service.impl;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.file.modular.entity.SysFileInfo;
import cn.stylefeng.roses.kernel.file.modular.factory.FileInfoFactory;
import cn.stylefeng.roses.kernel.file.modular.service.MultipleFileService;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
*
*
* @author fengshuonan
* @since 2024/8/20 9:03
*/
@Service
public class MultipleFileServiceImpl implements MultipleFileService {
@Resource
private SysFileInfoService sysFileInfoService;
@Override
public List<SysFileInfoResponse> batchUploadFile(MultipartFile[] fileList, SysFileInfoRequest sysFileInfoRequest) {
// 1. 创建文件基本信息
Map<MultipartFile, SysFileInfo> multipartFileSysFileInfoMap = FileInfoFactory.batchCreateFileInfo(fileList, sysFileInfoRequest);
Collection<SysFileInfo> sysFileInfos = multipartFileSysFileInfoMap.values();
this.sysFileInfoService.saveBatch(sysFileInfos);
// 2. 进行批量保存文件
for (Map.Entry<MultipartFile, SysFileInfo> entry : multipartFileSysFileInfoMap.entrySet()) {
MultipartFile file = entry.getKey();
SysFileInfo fileInfo = entry.getValue();
this.sysFileInfoService.storageFile(file, fileInfo);
}
// 3. 获取文件信息
List<SysFileInfoResponse> fileInfoResponses = new ArrayList<>();
for (SysFileInfo sysFileInfo : sysFileInfos) {
SysFileInfoResponse fileInfoResponse = sysFileInfoService.getFileInfoResponse(sysFileInfo);
fileInfoResponses.add(fileInfoResponse);
}
return fileInfoResponses;
}
}

View File

@ -146,34 +146,10 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
this.save(sysFileInfo);
// 存储文件到云或者本地
try {
byte[] bytes = file.getBytes();
// 如果是存在数据库库里,单独处理
if (FileLocationEnum.DB.getCode().equals(sysFileInfoRequest.getFileLocation())) {
sysFileStorageService.saveFile(sysFileInfo.getFileId(), bytes);
} else {
fileOperatorApi.storageFile(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(), bytes);
}
} catch (IOException e) {
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
}
this.storageFile(file, sysFileInfo);
// 返回文件信息体
SysFileInfoResponse fileUploadInfoResult = new SysFileInfoResponse();
BeanUtil.copyProperties(sysFileInfo, fileUploadInfoResult);
// 拼接文件可直接访问的url
String fileAuthUrl;
if (YesOrNotEnum.Y.getCode().equals(sysFileInfoRequest.getSecretFlag())) {
fileAuthUrl = fileOperatorApi.getFileAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(),
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
} else {
fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
}
fileUploadInfoResult.setFileUrl(fileAuthUrl);
return fileUploadInfoResult;
return getFileInfoResponse(sysFileInfo);
}
@Override
@ -624,6 +600,39 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
return fileUploadInfoResult;
}
@Override
public void storageFile(MultipartFile file, SysFileInfo sysFileInfo) {
try {
byte[] bytes = file.getBytes();
// 如果是存在数据库库里,单独处理
if (FileLocationEnum.DB.getCode().equals(sysFileInfo.getFileLocation())) {
sysFileStorageService.saveFile(sysFileInfo.getFileId(), bytes);
} else {
fileOperatorApi.storageFile(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(), bytes);
}
} catch (IOException e) {
throw new FileException(FileExceptionEnum.ERROR_FILE, e.getMessage());
}
}
@Override
public SysFileInfoResponse getFileInfoResponse(SysFileInfo sysFileInfo) {
SysFileInfoResponse fileUploadInfoResult = new SysFileInfoResponse();
BeanUtil.copyProperties(sysFileInfo, fileUploadInfoResult);
// 拼接文件可直接访问的url
String fileAuthUrl;
if (YesOrNotEnum.Y.getCode().equals(sysFileInfo.getSecretFlag())) {
fileAuthUrl = fileOperatorApi.getFileAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(),
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
} else {
fileAuthUrl = fileOperatorApi.getFileUnAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
}
fileUploadInfoResult.setFileUrl(fileAuthUrl);
return fileUploadInfoResult;
}
/**
* servletresponse
*