commit
05b2739f0c
@ -15,7 +15,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@MapperScan(basePackages = {"cn.axzo.oss.dal.mapper"})
|
@MapperScan(basePackages = {"cn.axzo.oss.dal.mapper"})
|
||||||
@SpringBootApplication(scanBasePackages = {"cn.axzo.oss"})
|
@SpringBootApplication(scanBasePackages = {"cn.axzo.oss"})
|
||||||
@MapperScan(basePackages = {"cn.axzo.oss.dal.mapper"})
|
|
||||||
public class Bootstrap {
|
public class Bootstrap {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@ -3,9 +3,9 @@ package cn.axzo.oss.client.controller;
|
|||||||
import cn.axzo.core.utils.converter.BeanConverter;
|
import cn.axzo.core.utils.converter.BeanConverter;
|
||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
||||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
import cn.axzo.oss.http.model.*;
|
||||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
import cn.axzo.oss.manager.api.dto.request.FindFileKeyDto;
|
||||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
import cn.axzo.oss.manager.api.dto.request.FindFileUrlDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||||
import cn.axzo.oss.service.api.FileService;
|
import cn.axzo.oss.service.api.FileService;
|
||||||
@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端文件 Controller
|
* 服务端文件 Controller
|
||||||
@ -33,11 +34,19 @@ public class ServerFileController implements ServerFileServiceApi {
|
|||||||
public CommonResponse<ServerFileUploadResponse> upload(@Valid @RequestBody ServerFileUploadRequest request) {
|
public CommonResponse<ServerFileUploadResponse> upload(@Valid @RequestBody ServerFileUploadRequest request) {
|
||||||
ServerFileUploadDto dto = BeanConvertUtil.copyBean(request, ServerFileUploadDto.class);
|
ServerFileUploadDto dto = BeanConvertUtil.copyBean(request, ServerFileUploadDto.class);
|
||||||
request.setFileContent(null);
|
request.setFileContent(null);
|
||||||
return CommonResponse.success(BeanConverter.convert(fileService.upload(dto),ServerFileUploadResponse.class));
|
return CommonResponse.success(BeanConverter.convert(fileService.upload(dto), ServerFileUploadResponse.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommonResponse<FileInformationResponse> uploadV2(ServerFileUploadRequest request) {
|
||||||
|
ServerFileUploadDto dto = BeanConvertUtil.copyBean(request, ServerFileUploadDto.class);
|
||||||
|
request.setFileContent(null);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(fileService.uploadV2(dto), FileInformationResponse.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据文件url删除文件
|
* 根据文件url删除文件
|
||||||
|
*
|
||||||
* @param request
|
* @param request
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@ -47,4 +56,28 @@ public class ServerFileController implements ServerFileServiceApi {
|
|||||||
fileService.delete(dto);
|
fileService.delete(dto);
|
||||||
return CommonResponse.success();
|
return CommonResponse.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件url
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public CommonResponse<List<FindFileUrlResponse>> getFileUrl(FindFileUrlRequest request) {
|
||||||
|
FindFileUrlDto dto = BeanConvertUtil.copyBean(request, FindFileUrlDto.class);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(fileService.findFileUrl(dto), FindFileUrlResponse.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件key,用于历史数据清理
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public CommonResponse<List<FindFileKeyResponse>> getFileKey(FindFileKeyRequest request) {
|
||||||
|
FindFileKeyDto dto = BeanConvertUtil.copyBean(request, FindFileKeyDto.class);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(fileService.findFileKey(dto), FindFileKeyResponse.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,14 @@
|
|||||||
package cn.axzo.oss.client.controller;
|
package cn.axzo.oss.client.controller;
|
||||||
|
|
||||||
|
import cn.axzo.core.utils.converter.BeanConverter;
|
||||||
|
import cn.axzo.oss.client.vo.FileInformationVo;
|
||||||
|
import cn.axzo.oss.client.vo.FindFileUrlVo;
|
||||||
import cn.axzo.oss.client.vo.WebFileUploadVo;
|
import cn.axzo.oss.client.vo.WebFileUploadVo;
|
||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
|
import cn.axzo.oss.manager.api.dto.request.FindFileUrlDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FileInformationResponse;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||||
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||||
import cn.axzo.oss.service.api.FileService;
|
import cn.axzo.oss.service.api.FileService;
|
||||||
import cn.azxo.framework.common.model.CommonResponse;
|
import cn.azxo.framework.common.model.CommonResponse;
|
||||||
@ -12,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 前端文件controller
|
* 前端文件controller
|
||||||
@ -42,4 +49,33 @@ public class WebFileController {
|
|||||||
return CommonResponse.success(result);
|
return CommonResponse.success(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
@PostMapping("/v1/file/getUrl")
|
||||||
|
@CrossOrigin
|
||||||
|
public CommonResponse<List<FindFileUrlVo>> getUrl(@Valid @RequestParam String appCode,
|
||||||
|
@Valid @RequestParam List<String> fileKey) {
|
||||||
|
FindFileUrlDto dto = FindFileUrlDto.builder()
|
||||||
|
.appCode(appCode)
|
||||||
|
.fileKey(fileKey)
|
||||||
|
.build();
|
||||||
|
List<FindFileUrlResponse> response = fileService.findFileUrl(dto);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(response, FindFileUrlVo.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/v2/file")
|
||||||
|
@CrossOrigin
|
||||||
|
@SneakyThrows
|
||||||
|
public CommonResponse<FileInformationVo> uploadV2(@Valid @RequestParam String appCode,
|
||||||
|
@Valid @RequestParam String bizScene,
|
||||||
|
@Valid @RequestParam MultipartFile file) {
|
||||||
|
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
||||||
|
.appCode(appCode)
|
||||||
|
.bizScene(bizScene)
|
||||||
|
.fileName(file.getOriginalFilename())
|
||||||
|
.fileContent(file.getBytes())
|
||||||
|
.build();
|
||||||
|
FileInformationResponse response = fileService.uploadV2(fileUploadDto);
|
||||||
|
FileInformationVo result = BeanConvertUtil.copyBean(response, FileInformationVo.class);
|
||||||
|
return CommonResponse.success(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.client.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class FileInformationVo {
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件名称
|
||||||
|
*/
|
||||||
|
private String fileName;
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
private Long fileSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL MD5
|
||||||
|
*/
|
||||||
|
private String urlMd5;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package cn.axzo.oss.client.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 19:41
|
||||||
|
* @Description: 前端获取文件url返回实体
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileUrlVo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -3,6 +3,8 @@ package cn.axzo.oss.dal.repository;
|
|||||||
import cn.axzo.oss.dal.entity.File;
|
import cn.axzo.oss.dal.entity.File;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* 文件上传记录 服务类
|
* 文件上传记录 服务类
|
||||||
@ -20,4 +22,8 @@ public interface FileDao extends IService<File> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
File getByAppCodeAndUrlMd5(String appCode, String urlMd5);
|
File getByAppCodeAndUrlMd5(String appCode, String urlMd5);
|
||||||
|
|
||||||
|
List<File> getByAppCodeAndFileUuids(String appCode, List<String> fileKey);
|
||||||
|
|
||||||
|
List<File> getByAppCodeAndUrlMd5s(String appCode, List<String> urlMd5List);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,8 @@ import cn.axzo.oss.dal.repository.FileDao;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* 文件上传记录 Dao实现类
|
* 文件上传记录 Dao实现类
|
||||||
@ -19,10 +21,28 @@ import org.springframework.stereotype.Repository;
|
|||||||
@Repository("fileDao")
|
@Repository("fileDao")
|
||||||
public class FileDaoImpl extends ServiceImpl<FileMapper, File> implements FileDao {
|
public class FileDaoImpl extends ServiceImpl<FileMapper, File> implements FileDao {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public File getByAppCodeAndUrlMd5(String appCode, String urlMd5) {
|
public File getByAppCodeAndUrlMd5(String appCode, String urlMd5) {
|
||||||
return lambdaQuery().eq(File::getAppCode, appCode).eq(File::getUrlMd5, urlMd5)
|
return lambdaQuery().eq(File::getAppCode, appCode).eq(File::getUrlMd5, urlMd5)
|
||||||
.eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETED)
|
.eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||||
.last("limit 1").one();
|
.last("limit 1").one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<File> getByAppCodeAndFileUuids(String appCode, List<String> fileKey) {
|
||||||
|
return lambdaQuery().eq(File::getAppCode, appCode)
|
||||||
|
.in(File::getFileUuid, fileKey)
|
||||||
|
.eq(File::getStatus, FileStatus.SUCCESS)
|
||||||
|
.eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||||
|
.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<File> getByAppCodeAndUrlMd5s(String appCode, List<String> urlMd5List) {
|
||||||
|
return lambdaQuery().eq(File::getAppCode, appCode)
|
||||||
|
.in(File::getUrlMd5, urlMd5List)
|
||||||
|
.eq(File::getStatus, FileStatus.SUCCESS)
|
||||||
|
.eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||||
|
.list();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
package cn.axzo.oss.http.api;
|
package cn.axzo.oss.http.api;
|
||||||
|
|
||||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
import cn.axzo.oss.http.model.*;
|
||||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
|
||||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
|
||||||
import cn.azxo.framework.common.model.CommonResponse;
|
import cn.azxo.framework.common.model.CommonResponse;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务端 OSS 服务
|
* 服务端 OSS 服务
|
||||||
*
|
*
|
||||||
@ -29,6 +29,14 @@ public interface ServerFileServiceApi {
|
|||||||
@RequestMapping(value = "/api/v1/server/upload", method = RequestMethod.POST)
|
@RequestMapping(value = "/api/v1/server/upload", method = RequestMethod.POST)
|
||||||
CommonResponse<ServerFileUploadResponse> upload(ServerFileUploadRequest request);
|
CommonResponse<ServerFileUploadResponse> upload(ServerFileUploadRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "/api/v2/server/upload", method = RequestMethod.POST)
|
||||||
|
CommonResponse<FileInformationResponse> uploadV2(ServerFileUploadRequest request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
* @param request
|
* @param request
|
||||||
@ -37,4 +45,16 @@ public interface ServerFileServiceApi {
|
|||||||
@RequestMapping(value = "/api/v1/server/delete", method = RequestMethod.POST)
|
@RequestMapping(value = "/api/v1/server/delete", method = RequestMethod.POST)
|
||||||
CommonResponse delete(ServerFileDeleteRequest request);
|
CommonResponse delete(ServerFileDeleteRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件URL
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "api/v1/server/getUrl", method = RequestMethod.POST)
|
||||||
|
CommonResponse<List<FindFileUrlResponse>> getFileUrl(FindFileUrlRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件key
|
||||||
|
* 用于历史数据清理
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "api/v1/server/getFileKey", method = RequestMethod.POST)
|
||||||
|
CommonResponse<List<FindFileKeyResponse>> getFileKey(FindFileKeyRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class FileInformationResponse {
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件名称
|
||||||
|
*/
|
||||||
|
private String fileName;
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
private Long fileSize;
|
||||||
|
/**
|
||||||
|
* URL MD5
|
||||||
|
*/
|
||||||
|
private String urlMd5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 17:39
|
||||||
|
* @Description: 服务端获取文件key请求类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileKeyRequest {
|
||||||
|
|
||||||
|
@NotBlank(message = "appCode must not be null")
|
||||||
|
private String appCode;
|
||||||
|
|
||||||
|
@NotNull(message = "url must not be null")
|
||||||
|
private List<String> url;
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 18:21
|
||||||
|
* @Description: 服务端获取文件key响应类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileKeyResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 17:33
|
||||||
|
* @Description: 服务端获取文件url请求类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileUrlRequest {
|
||||||
|
|
||||||
|
@NotBlank(message = "appCode must not be null")
|
||||||
|
private String appCode;
|
||||||
|
|
||||||
|
@NotNull(message = "fileKey must not be null")
|
||||||
|
private List<String> fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 18:21
|
||||||
|
* @Description: 服务端获取文件url响应类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileUrlResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package cn.axzo.oss.manager.api.dto.request;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 17:39
|
||||||
|
* @Description: 服务端获取文件key请求类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FindFileKeyDto {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用码
|
||||||
|
*/
|
||||||
|
private String appCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件url
|
||||||
|
*/
|
||||||
|
private List<String> url;
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package cn.axzo.oss.manager.api.dto.request;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 17:33
|
||||||
|
* @Description: 服务端获取文件url请求类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FindFileUrlDto {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用码
|
||||||
|
*/
|
||||||
|
private String appCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件uuid
|
||||||
|
*/
|
||||||
|
private List<String> fileKey;
|
||||||
|
}
|
||||||
@ -1,6 +1,9 @@
|
|||||||
package cn.axzo.oss.manager.api.dto.request;
|
package cn.axzo.oss.manager.api.dto.request;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author admin
|
* @Author admin
|
||||||
@ -9,6 +12,9 @@ import lombok.Data;
|
|||||||
* @Version 0.0.1
|
* @Version 0.0.1
|
||||||
**/
|
**/
|
||||||
@Data
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class ServerFileDeleteDto {
|
public class ServerFileDeleteDto {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.manager.api.dto.response;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class FileInformationResponse {
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件名称
|
||||||
|
*/
|
||||||
|
private String fileName;
|
||||||
|
/**
|
||||||
|
* 文件大小
|
||||||
|
*/
|
||||||
|
private Long fileSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URL MD5
|
||||||
|
*/
|
||||||
|
private String urlMd5;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.axzo.oss.manager.api.dto.response;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 18:21
|
||||||
|
* @Description: 服务端获取文件key响应类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileKeyResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
package cn.axzo.oss.manager.api.dto.response;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/11/16 18:21
|
||||||
|
* @Description: 服务端获取文件url响应类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FindFileUrlResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 URL
|
||||||
|
*/
|
||||||
|
private String url;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
}
|
||||||
@ -1,9 +1,16 @@
|
|||||||
package cn.axzo.oss.service.api;
|
package cn.axzo.oss.service.api;
|
||||||
|
|
||||||
|
import cn.axzo.oss.manager.api.dto.request.FindFileKeyDto;
|
||||||
|
import cn.axzo.oss.manager.api.dto.request.FindFileUrlDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FileInformationResponse;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FindFileKeyResponse;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||||
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author admin
|
* @Author admin
|
||||||
* @Description
|
* @Description
|
||||||
@ -12,17 +19,23 @@ import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
|||||||
**/
|
**/
|
||||||
public interface FileService {
|
public interface FileService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
*
|
*
|
||||||
* @param dto
|
* @param dto
|
||||||
*/
|
*/
|
||||||
void delete(ServerFileDeleteDto dto);
|
void delete(ServerFileDeleteDto dto);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传
|
* 上传
|
||||||
*
|
*
|
||||||
* @param request
|
* @param request
|
||||||
*/
|
*/
|
||||||
ServerFileUploadResponse upload(ServerFileUploadDto request);
|
ServerFileUploadResponse upload(ServerFileUploadDto request);
|
||||||
|
|
||||||
|
List<FindFileUrlResponse> findFileUrl(FindFileUrlDto dto);
|
||||||
|
|
||||||
|
List<FindFileKeyResponse> findFileKey(FindFileKeyDto dto);
|
||||||
|
|
||||||
|
FileInformationResponse uploadV2(ServerFileUploadDto request);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,26 +9,32 @@ import cn.axzo.oss.common.enums.FileStatusEnum;
|
|||||||
import cn.axzo.oss.common.exception.BizException;
|
import cn.axzo.oss.common.exception.BizException;
|
||||||
import cn.axzo.oss.common.utils.JsonUtil;
|
import cn.axzo.oss.common.utils.JsonUtil;
|
||||||
import cn.axzo.oss.common.utils.Utility;
|
import cn.axzo.oss.common.utils.Utility;
|
||||||
import cn.axzo.oss.dal.entity.AppChannelBucket;
|
import cn.axzo.oss.dal.entity.*;
|
||||||
import cn.axzo.oss.dal.entity.File;
|
|
||||||
import cn.axzo.oss.dal.entity.FileApp;
|
|
||||||
import cn.axzo.oss.dal.entity.FileBusinessScene;
|
|
||||||
import cn.axzo.oss.dal.entity.FileUploadConfig;
|
|
||||||
import cn.axzo.oss.dal.repository.FileAppDao;
|
import cn.axzo.oss.dal.repository.FileAppDao;
|
||||||
import cn.axzo.oss.dal.repository.FileDao;
|
import cn.axzo.oss.dal.repository.FileDao;
|
||||||
import cn.axzo.oss.manager.api.AppChannelBucketManager;
|
import cn.axzo.oss.manager.api.AppChannelBucketManager;
|
||||||
import cn.axzo.oss.manager.api.FileBusinessSceneManager;
|
import cn.axzo.oss.manager.api.FileBusinessSceneManager;
|
||||||
import cn.axzo.oss.manager.api.FileManager;
|
import cn.axzo.oss.manager.api.FileManager;
|
||||||
import cn.axzo.oss.manager.api.FileUploadConfigManager;
|
import cn.axzo.oss.manager.api.FileUploadConfigManager;
|
||||||
|
import cn.axzo.oss.manager.api.dto.request.FindFileKeyDto;
|
||||||
|
import cn.axzo.oss.manager.api.dto.request.FindFileUrlDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FileInformationResponse;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FindFileKeyResponse;
|
||||||
|
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||||
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||||
import cn.axzo.oss.service.api.FileService;
|
import cn.axzo.oss.service.api.FileService;
|
||||||
import java.util.Arrays;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author admin
|
* @Author admin
|
||||||
* @Description
|
* @Description
|
||||||
@ -39,167 +45,238 @@ import org.springframework.stereotype.Service;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class FileServiceImpl implements FileService {
|
public class FileServiceImpl implements FileService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileManager fileManager;
|
private FileManager fileManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileDao fileDao;
|
private FileDao fileDao;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileAppDao fileAppDao;
|
private FileAppDao fileAppDao;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AppChannelBucketManager appChannelBucketManager;
|
private AppChannelBucketManager appChannelBucketManager;
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileBusinessSceneManager fileBusinessSceneManager;
|
private FileBusinessSceneManager fileBusinessSceneManager;
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileUploadConfigManager fileUploadConfigManager;
|
private FileUploadConfigManager fileUploadConfigManager;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
*
|
*
|
||||||
* @param dto
|
* @param dto
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void delete(ServerFileDeleteDto dto) {
|
public void delete(ServerFileDeleteDto dto) {
|
||||||
log.info("delete dto = {}", JsonUtil.obj2Str(dto));
|
log.info("delete dto = {}", JsonUtil.obj2Str(dto));
|
||||||
// 检查app code
|
// 检查app code
|
||||||
checkAppCode(dto.getAppCode());
|
checkAppCode(dto.getAppCode());
|
||||||
|
|
||||||
String urlMd5 = Utility.getMd5(dto.getUrl());
|
String urlMd5 = Utility.getMd5(dto.getUrl());
|
||||||
log.info("delete urlMd5 = {}", urlMd5);
|
log.info("delete urlMd5 = {}", urlMd5);
|
||||||
|
|
||||||
File file = fileDao.getByAppCodeAndUrlMd5(dto.getAppCode(), urlMd5);
|
File file = fileDao.getByAppCodeAndUrlMd5(dto.getAppCode(), urlMd5);
|
||||||
if (Utility.objIsNull(file)) {
|
if (Utility.objIsNull(file)) {
|
||||||
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String tgtFileKey = Utility
|
String tgtFileKey = Utility
|
||||||
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
||||||
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
||||||
boolean deleteFlag = fileManager.delete(file.getBucketName(), tgtFileKey);
|
boolean deleteFlag = fileManager.delete(file.getBucketName(), tgtFileKey);
|
||||||
log.info("delete deleteFlag = {}", deleteFlag);
|
log.info("delete deleteFlag = {}", deleteFlag);
|
||||||
if (deleteFlag) {
|
if (deleteFlag) {
|
||||||
File updateFile = new File();
|
File updateFile = new File();
|
||||||
updateFile.setIsDelete(TableDelete.DELETED);
|
updateFile.setIsDelete(TableDelete.DELETED);
|
||||||
updateFile.setStatus(FileStatus.DELETED);
|
updateFile.setStatus(FileStatus.DELETED);
|
||||||
updateFile.setUpdateBy(dto.getOperator());
|
updateFile.setUpdateBy(dto.getOperator());
|
||||||
updateFile.setId(file.getId());
|
updateFile.setId(file.getId());
|
||||||
fileDao.updateById(updateFile);
|
fileDao.updateById(updateFile);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查appCode是否有效
|
|
||||||
*
|
|
||||||
* @param appCode
|
|
||||||
*/
|
|
||||||
private void checkAppCode(final String appCode) {
|
|
||||||
log.info("checkAppCode appCode = {}", appCode);
|
|
||||||
FileApp fileApp = fileAppDao.getByAppCode(appCode);
|
|
||||||
BizException.error(Utility.objIsNotNull(fileApp), CodeEnum.APP_CODE_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传
|
|
||||||
*
|
|
||||||
* @param dto
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public ServerFileUploadResponse upload(ServerFileUploadDto dto) {
|
|
||||||
log.info("update fileName:{},appCode:{},bizScene:{}",
|
|
||||||
dto.getFileName(), dto.getAppCode(), dto.getBizScene());
|
|
||||||
// 检查appCode
|
|
||||||
checkAppCode(dto.getAppCode());
|
|
||||||
|
|
||||||
// 通过appcode获取文件渠道桶信息
|
|
||||||
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(dto.getAppCode());
|
|
||||||
|
|
||||||
// 通过渠道桶编码获取到具体文件业务场景
|
|
||||||
FileBusinessScene scene = fileBusinessSceneManager
|
|
||||||
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), dto.getBizScene());
|
|
||||||
|
|
||||||
// 通过渠道码和桶名称获取获取指定上传配置
|
|
||||||
FileUploadConfig fileUploadConfig = fileUploadConfigManager
|
|
||||||
.getByUploadConfig(scene.getAppChannelBucketNo(), scene.getDirectory());
|
|
||||||
|
|
||||||
// 上传文件并生成file对象
|
|
||||||
File ossFile = generateFile(fileUploadConfig, dto);
|
|
||||||
|
|
||||||
return setResponse(ossFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断文件是否符合要求
|
|
||||||
*/
|
|
||||||
private String isFileConform(FileUploadConfig fileUploadConfig, int fileLength, String fileName) {
|
|
||||||
// 文件大小超出上限
|
|
||||||
int size = Utility
|
|
||||||
.capacityConversion(fileUploadConfig.getStorageSize(), fileUploadConfig.getStorageUnit());
|
|
||||||
BizException.error(size > fileLength, CodeEnum.FILE_SIZE_EXCEEDS_LIMIT);
|
|
||||||
// 文件格式判断
|
|
||||||
String[] formats = fileUploadConfig.getFileFormat().split(FileClassEnum.COMMA.type);
|
|
||||||
|
|
||||||
int lastIndexOf = fileName.lastIndexOf(FileClassEnum.DOT.type);
|
|
||||||
BizException
|
|
||||||
.error(lastIndexOf != CommonConstants.NOT_FOUND_INDEX_OF, CodeEnum.NOT_FILE_FORMAT);
|
|
||||||
|
|
||||||
// 是否包含指定格式
|
|
||||||
String fileFormat = fileName.substring(lastIndexOf + CommonConstants.ONE).toLowerCase();
|
|
||||||
boolean contains = Arrays.asList(formats).contains(fileFormat);
|
|
||||||
BizException.error(contains, CodeEnum.FILE_FORMAT_NOT_SUPPORTED);
|
|
||||||
|
|
||||||
return fileFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private File generateFile(FileUploadConfig fileUploadConfig, ServerFileUploadDto dto) {
|
|
||||||
// 判断容量
|
|
||||||
String fileConform = isFileConform(fileUploadConfig, dto.getFileContent().length,
|
|
||||||
dto.getFileName());
|
|
||||||
|
|
||||||
String uuid = Utility.getUUID();
|
|
||||||
|
|
||||||
// 生成上传文件的唯一key
|
|
||||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
|
||||||
|
|
||||||
File ossFile = new File();
|
|
||||||
ossFile.setAppChannelBucketNo(fileUploadConfig.getAppChannelBucketNo());
|
|
||||||
ossFile.setAppCode(fileUploadConfig.getAppCode());
|
|
||||||
ossFile.setChannelCode(fileUploadConfig.getChannelCode());
|
|
||||||
ossFile.setBucketName(fileUploadConfig.getBucketName());
|
|
||||||
ossFile.setDirectory(fileUploadConfig.getDirectory());
|
|
||||||
ossFile.setStatus(FileStatusEnum.STATUS_UPLOAD_FAIL.getCode());
|
|
||||||
ossFile.setStorageUnit(fileUploadConfig.getStorageUnit());
|
|
||||||
ossFile.setStorageSize(fileUploadConfig.getStorageSize());
|
|
||||||
ossFile.setFileFormat(fileConform);
|
|
||||||
// 上传文件
|
|
||||||
String fileUrl = fileManager.uploadByStream(
|
|
||||||
fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileContent());
|
|
||||||
|
|
||||||
// 保存失败
|
|
||||||
if (Utility.isBlank(fileUrl)) {
|
|
||||||
log.error("fileUrl is empty");
|
|
||||||
//fileDao.save(ossFile);
|
|
||||||
throw new BizException(CodeEnum.FILE_UPLOAD_FAILED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ossFile.setFileUuid(uuid);
|
|
||||||
ossFile.setFileUrl(fileUrl);
|
|
||||||
ossFile.setUrlMd5(Utility.getMd5(fileUrl));
|
|
||||||
ossFile.setStatus(FileStatusEnum.STATUS_UPLOAD_SUCCESS.getCode());
|
|
||||||
ossFile.setFileName(dto.getFileName());
|
|
||||||
ossFile.setFileMd5(Utility.getMd5(dto.getFileContent()));
|
|
||||||
fileDao.save(ossFile);
|
|
||||||
return ossFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServerFileUploadResponse setResponse(File ossFile){
|
/**
|
||||||
ServerFileUploadResponse response = new ServerFileUploadResponse();
|
* 检查appCode是否有效
|
||||||
response.setUrl(ossFile.getFileUrl());
|
*
|
||||||
response.setUrlMd5(ossFile.getUrlMd5());
|
* @param appCode
|
||||||
response.setFileKey(ossFile.getFileUuid());
|
*/
|
||||||
return response;
|
private void checkAppCode(final String appCode) {
|
||||||
|
log.info("checkAppCode appCode = {}", appCode);
|
||||||
|
FileApp fileApp = fileAppDao.getByAppCode(appCode);
|
||||||
|
BizException.error(Utility.objIsNotNull(fileApp), CodeEnum.APP_CODE_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传
|
||||||
|
*
|
||||||
|
* @param dto
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ServerFileUploadResponse upload(ServerFileUploadDto dto) {
|
||||||
|
File ossFile = uploadFileAndGetFile(dto);
|
||||||
|
return setResponse(ossFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileInformationResponse uploadV2(ServerFileUploadDto request) {
|
||||||
|
File ossFile = uploadFileAndGetFile(request);
|
||||||
|
return setFileInfoResp(ossFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File uploadFileAndGetFile(ServerFileUploadDto dto) {
|
||||||
|
log.info("update fileName:{},appCode:{},bizScene:{}",
|
||||||
|
dto.getFileName(), dto.getAppCode(), dto.getBizScene());
|
||||||
|
// 检查appCode
|
||||||
|
checkAppCode(dto.getAppCode());
|
||||||
|
|
||||||
|
// 通过appcode获取文件渠道桶信息
|
||||||
|
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(dto.getAppCode());
|
||||||
|
|
||||||
|
// 通过渠道桶编码获取到具体文件业务场景
|
||||||
|
FileBusinessScene scene = fileBusinessSceneManager
|
||||||
|
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), dto.getBizScene());
|
||||||
|
|
||||||
|
// 通过渠道码和桶名称获取获取指定上传配置
|
||||||
|
FileUploadConfig fileUploadConfig = fileUploadConfigManager
|
||||||
|
.getByUploadConfig(scene.getAppChannelBucketNo(), scene.getDirectory());
|
||||||
|
// 上传文件并生成file对象
|
||||||
|
return generateFile(fileUploadConfig, dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FindFileUrlResponse> findFileUrl(FindFileUrlDto dto) {
|
||||||
|
log.info("find file url dto = {}", JsonUtil.obj2Str(dto));
|
||||||
|
// 检查app code
|
||||||
|
checkAppCode(dto.getAppCode());
|
||||||
|
|
||||||
|
List<File> fileList = fileDao.getByAppCodeAndFileUuids(dto.getAppCode(), dto.getFileKey());
|
||||||
|
if (CollectionUtil.isEmpty(fileList)) {
|
||||||
|
log.warn("find file url is null,key = {}", Arrays.toString(dto.getFileKey().toArray()));
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
return setFileUrlRes(fileList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<FindFileKeyResponse> findFileKey(FindFileKeyDto dto) {
|
||||||
|
log.info("find file key dto = {}", JsonUtil.obj2Str(dto));
|
||||||
|
// 检查app code
|
||||||
|
checkAppCode(dto.getAppCode());
|
||||||
|
|
||||||
|
List<String> urlMd5List = dto.getUrl().stream()
|
||||||
|
.map(url -> Utility.getMd5(url))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
List<File> fileList = fileDao.getByAppCodeAndUrlMd5s(dto.getAppCode(), urlMd5List);
|
||||||
|
if (CollectionUtil.isEmpty(fileList)) {
|
||||||
|
log.warn("find file key is null,url = {}", Arrays.toString(dto.getUrl().toArray()));
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
return setFileKeyRes(fileList);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<FindFileUrlResponse> setFileUrlRes(List<File> fileList) {
|
||||||
|
List<FindFileUrlResponse> resList = new ArrayList<>();
|
||||||
|
fileList.forEach(file -> {
|
||||||
|
FindFileUrlResponse response = new FindFileUrlResponse();
|
||||||
|
response.setUrl(file.getFileUrl());
|
||||||
|
response.setFileKey(file.getFileUuid());
|
||||||
|
resList.add(response);
|
||||||
|
});
|
||||||
|
return resList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<FindFileKeyResponse> setFileKeyRes(List<File> fileList) {
|
||||||
|
List<FindFileKeyResponse> resList = new ArrayList<>();
|
||||||
|
fileList.forEach(file -> {
|
||||||
|
FindFileKeyResponse response = new FindFileKeyResponse();
|
||||||
|
response.setUrl(file.getFileUrl());
|
||||||
|
response.setFileKey(file.getFileUuid());
|
||||||
|
resList.add(response);
|
||||||
|
});
|
||||||
|
return resList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断文件是否符合要求
|
||||||
|
*/
|
||||||
|
private String isFileConform(FileUploadConfig fileUploadConfig, int fileLength, String fileName) {
|
||||||
|
// 文件大小超出上限
|
||||||
|
int size = Utility
|
||||||
|
.capacityConversion(fileUploadConfig.getStorageSize(), fileUploadConfig.getStorageUnit());
|
||||||
|
BizException.error(size > fileLength, CodeEnum.FILE_SIZE_EXCEEDS_LIMIT);
|
||||||
|
// 文件格式判断
|
||||||
|
String[] formats = fileUploadConfig.getFileFormat().split(FileClassEnum.COMMA.type);
|
||||||
|
|
||||||
|
int lastIndexOf = fileName.lastIndexOf(FileClassEnum.DOT.type);
|
||||||
|
BizException
|
||||||
|
.error(lastIndexOf != CommonConstants.NOT_FOUND_INDEX_OF, CodeEnum.NOT_FILE_FORMAT);
|
||||||
|
|
||||||
|
// 是否包含指定格式
|
||||||
|
String fileFormat = fileName.substring(lastIndexOf + CommonConstants.ONE).toLowerCase();
|
||||||
|
boolean contains = Arrays.asList(formats).contains(fileFormat);
|
||||||
|
BizException.error(contains, CodeEnum.FILE_FORMAT_NOT_SUPPORTED);
|
||||||
|
|
||||||
|
return fileFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
private File generateFile(FileUploadConfig fileUploadConfig, ServerFileUploadDto dto) {
|
||||||
|
// 判断容量
|
||||||
|
String fileConform = isFileConform(fileUploadConfig, dto.getFileContent().length,
|
||||||
|
dto.getFileName());
|
||||||
|
|
||||||
|
String uuid = Utility.getUUID();
|
||||||
|
|
||||||
|
// 生成上传文件的唯一key
|
||||||
|
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
||||||
|
|
||||||
|
File ossFile = new File();
|
||||||
|
ossFile.setAppChannelBucketNo(fileUploadConfig.getAppChannelBucketNo());
|
||||||
|
ossFile.setAppCode(fileUploadConfig.getAppCode());
|
||||||
|
ossFile.setChannelCode(fileUploadConfig.getChannelCode());
|
||||||
|
ossFile.setBucketName(fileUploadConfig.getBucketName());
|
||||||
|
ossFile.setDirectory(fileUploadConfig.getDirectory());
|
||||||
|
ossFile.setStatus(FileStatusEnum.STATUS_UPLOAD_FAIL.getCode());
|
||||||
|
ossFile.setStorageUnit(fileUploadConfig.getStorageUnit());
|
||||||
|
ossFile.setStorageSize(fileUploadConfig.getStorageSize());
|
||||||
|
ossFile.setFileFormat(fileConform);
|
||||||
|
// 上传文件
|
||||||
|
String fileUrl = fileManager.uploadByStream(
|
||||||
|
fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileContent());
|
||||||
|
|
||||||
|
// 保存失败
|
||||||
|
if (Utility.isBlank(fileUrl)) {
|
||||||
|
log.error("fileUrl is empty");
|
||||||
|
//fileDao.save(ossFile);
|
||||||
|
throw new BizException(CodeEnum.FILE_UPLOAD_FAILED);
|
||||||
|
}
|
||||||
|
|
||||||
|
ossFile.setFileUuid(uuid);
|
||||||
|
ossFile.setFileUrl(fileUrl);
|
||||||
|
ossFile.setUrlMd5(Utility.getMd5(fileUrl));
|
||||||
|
ossFile.setStatus(FileStatusEnum.STATUS_UPLOAD_SUCCESS.getCode());
|
||||||
|
ossFile.setFileName(dto.getFileName());
|
||||||
|
ossFile.setFileMd5(Utility.getMd5(dto.getFileContent()));
|
||||||
|
fileDao.save(ossFile);
|
||||||
|
return ossFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ServerFileUploadResponse setResponse(File ossFile) {
|
||||||
|
ServerFileUploadResponse response = new ServerFileUploadResponse();
|
||||||
|
response.setUrl(ossFile.getFileUrl());
|
||||||
|
response.setUrlMd5(ossFile.getUrlMd5());
|
||||||
|
response.setFileKey(ossFile.getFileUuid());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FileInformationResponse setFileInfoResp(File ossFile){
|
||||||
|
FileInformationResponse resp=new FileInformationResponse();
|
||||||
|
resp.setUrl(ossFile.getFileUrl());
|
||||||
|
resp.setFileName(ossFile.getFileName());
|
||||||
|
resp.setUrlMd5(ossFile.getUrlMd5());
|
||||||
|
resp.setFileKey(ossFile.getFileUuid());
|
||||||
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user