commit
05b2739f0c
@ -15,7 +15,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@Slf4j
|
||||
@MapperScan(basePackages = {"cn.axzo.oss.dal.mapper"})
|
||||
@SpringBootApplication(scanBasePackages = {"cn.axzo.oss"})
|
||||
@MapperScan(basePackages = {"cn.axzo.oss.dal.mapper"})
|
||||
public class Bootstrap {
|
||||
|
||||
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.oss.common.utils.BeanConvertUtil;
|
||||
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
||||
import cn.axzo.oss.http.model.*;
|
||||
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.ServerFileUploadDto;
|
||||
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 javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 服务端文件 Controller
|
||||
@ -33,11 +34,19 @@ public class ServerFileController implements ServerFileServiceApi {
|
||||
public CommonResponse<ServerFileUploadResponse> upload(@Valid @RequestBody ServerFileUploadRequest request) {
|
||||
ServerFileUploadDto dto = BeanConvertUtil.copyBean(request, ServerFileUploadDto.class);
|
||||
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删除文件
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@ -47,4 +56,28 @@ public class ServerFileController implements ServerFileServiceApi {
|
||||
fileService.delete(dto);
|
||||
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;
|
||||
|
||||
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.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.response.FileInformationResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.FindFileUrlResponse;
|
||||
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||
import cn.axzo.oss.service.api.FileService;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
@ -12,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 前端文件controller
|
||||
@ -42,4 +49,33 @@ public class WebFileController {
|
||||
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 com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 文件上传记录 服务类
|
||||
@ -20,4 +22,8 @@ public interface FileDao extends IService<File> {
|
||||
* @return
|
||||
*/
|
||||
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 org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 文件上传记录 Dao实现类
|
||||
@ -19,10 +21,28 @@ import org.springframework.stereotype.Repository;
|
||||
@Repository("fileDao")
|
||||
public class FileDaoImpl extends ServiceImpl<FileMapper, File> implements FileDao {
|
||||
|
||||
@Override
|
||||
public File getByAppCodeAndUrlMd5(String appCode, String urlMd5) {
|
||||
return lambdaQuery().eq(File::getAppCode, appCode).eq(File::getUrlMd5, urlMd5)
|
||||
.eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||
.last("limit 1").one();
|
||||
}
|
||||
@Override
|
||||
public File getByAppCodeAndUrlMd5(String appCode, String urlMd5) {
|
||||
return lambdaQuery().eq(File::getAppCode, appCode).eq(File::getUrlMd5, urlMd5)
|
||||
.eq(File::getStatus, FileStatus.SUCCESS).eq(File::getIsDelete, TableDelete.UN_DELETED)
|
||||
.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;
|
||||
|
||||
import cn.axzo.oss.http.model.ServerFileDeleteRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadRequest;
|
||||
import cn.axzo.oss.http.model.ServerFileUploadResponse;
|
||||
import cn.axzo.oss.http.model.*;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 服务端 OSS 服务
|
||||
*
|
||||
@ -29,6 +29,14 @@ public interface ServerFileServiceApi {
|
||||
@RequestMapping(value = "/api/v1/server/upload", method = RequestMethod.POST)
|
||||
CommonResponse<ServerFileUploadResponse> upload(ServerFileUploadRequest request);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping(value = "/api/v2/server/upload", method = RequestMethod.POST)
|
||||
CommonResponse<FileInformationResponse> uploadV2(ServerFileUploadRequest request);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param request
|
||||
@ -37,4 +45,16 @@ public interface ServerFileServiceApi {
|
||||
@RequestMapping(value = "/api/v1/server/delete", method = RequestMethod.POST)
|
||||
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;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @Author admin
|
||||
@ -9,6 +12,9 @@ import lombok.Data;
|
||||
* @Version 0.0.1
|
||||
**/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
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;
|
||||
|
||||
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.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 java.util.List;
|
||||
|
||||
/**
|
||||
* @Author admin
|
||||
* @Description
|
||||
@ -12,17 +19,23 @@ import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||
**/
|
||||
public interface FileService {
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param dto
|
||||
*/
|
||||
void delete(ServerFileDeleteDto dto);
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param dto
|
||||
*/
|
||||
void delete(ServerFileDeleteDto dto);
|
||||
|
||||
/**
|
||||
* 上传
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
ServerFileUploadResponse upload(ServerFileUploadDto request);
|
||||
/**
|
||||
* 上传
|
||||
*
|
||||
* @param 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.utils.JsonUtil;
|
||||
import cn.axzo.oss.common.utils.Utility;
|
||||
import cn.axzo.oss.dal.entity.AppChannelBucket;
|
||||
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.entity.*;
|
||||
import cn.axzo.oss.dal.repository.FileAppDao;
|
||||
import cn.axzo.oss.dal.repository.FileDao;
|
||||
import cn.axzo.oss.manager.api.AppChannelBucketManager;
|
||||
import cn.axzo.oss.manager.api.FileBusinessSceneManager;
|
||||
import cn.axzo.oss.manager.api.FileManager;
|
||||
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.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.service.api.FileService;
|
||||
import java.util.Arrays;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author admin
|
||||
* @Description
|
||||
@ -39,167 +45,238 @@ import org.springframework.stereotype.Service;
|
||||
@Slf4j
|
||||
public class FileServiceImpl implements FileService {
|
||||
|
||||
@Autowired
|
||||
private FileManager fileManager;
|
||||
@Autowired
|
||||
private FileManager fileManager;
|
||||
|
||||
@Autowired
|
||||
private FileDao fileDao;
|
||||
@Autowired
|
||||
private FileDao fileDao;
|
||||
|
||||
@Autowired
|
||||
private FileAppDao fileAppDao;
|
||||
@Autowired
|
||||
private AppChannelBucketManager appChannelBucketManager;
|
||||
@Autowired
|
||||
private FileBusinessSceneManager fileBusinessSceneManager;
|
||||
@Autowired
|
||||
private FileUploadConfigManager fileUploadConfigManager;
|
||||
@Autowired
|
||||
private FileAppDao fileAppDao;
|
||||
@Autowired
|
||||
private AppChannelBucketManager appChannelBucketManager;
|
||||
@Autowired
|
||||
private FileBusinessSceneManager fileBusinessSceneManager;
|
||||
@Autowired
|
||||
private FileUploadConfigManager fileUploadConfigManager;
|
||||
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param dto
|
||||
*/
|
||||
@Override
|
||||
public void delete(ServerFileDeleteDto dto) {
|
||||
log.info("delete dto = {}", JsonUtil.obj2Str(dto));
|
||||
// 检查app code
|
||||
checkAppCode(dto.getAppCode());
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param dto
|
||||
*/
|
||||
@Override
|
||||
public void delete(ServerFileDeleteDto dto) {
|
||||
log.info("delete dto = {}", JsonUtil.obj2Str(dto));
|
||||
// 检查app code
|
||||
checkAppCode(dto.getAppCode());
|
||||
|
||||
String urlMd5 = Utility.getMd5(dto.getUrl());
|
||||
log.info("delete urlMd5 = {}", urlMd5);
|
||||
String urlMd5 = Utility.getMd5(dto.getUrl());
|
||||
log.info("delete urlMd5 = {}", urlMd5);
|
||||
|
||||
File file = fileDao.getByAppCodeAndUrlMd5(dto.getAppCode(), urlMd5);
|
||||
if (Utility.objIsNull(file)) {
|
||||
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
||||
return;
|
||||
}
|
||||
String tgtFileKey = Utility
|
||||
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
||||
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
||||
boolean deleteFlag = fileManager.delete(file.getBucketName(), tgtFileKey);
|
||||
log.info("delete deleteFlag = {}", deleteFlag);
|
||||
if (deleteFlag) {
|
||||
File updateFile = new File();
|
||||
updateFile.setIsDelete(TableDelete.DELETED);
|
||||
updateFile.setStatus(FileStatus.DELETED);
|
||||
updateFile.setUpdateBy(dto.getOperator());
|
||||
updateFile.setId(file.getId());
|
||||
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);
|
||||
File file = fileDao.getByAppCodeAndUrlMd5(dto.getAppCode(), urlMd5);
|
||||
if (Utility.objIsNull(file)) {
|
||||
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
||||
return;
|
||||
}
|
||||
String tgtFileKey = Utility
|
||||
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
||||
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
||||
boolean deleteFlag = fileManager.delete(file.getBucketName(), tgtFileKey);
|
||||
log.info("delete deleteFlag = {}", deleteFlag);
|
||||
if (deleteFlag) {
|
||||
File updateFile = new File();
|
||||
updateFile.setIsDelete(TableDelete.DELETED);
|
||||
updateFile.setStatus(FileStatus.DELETED);
|
||||
updateFile.setUpdateBy(dto.getOperator());
|
||||
updateFile.setId(file.getId());
|
||||
fileDao.updateById(updateFile);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
/**
|
||||
* 检查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) {
|
||||
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