|
|
@ -10,10 +10,9 @@ import cn.hutool.core.util.StrUtil; |
|
|
|
import com.amazonaws.services.s3.model.S3Object; |
|
|
|
import com.amazonaws.services.s3.model.S3Object; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.cloud.kicc.common.core.exception.CheckedException; |
|
|
|
import com.cloud.kicc.common.core.exception.CheckedException; |
|
|
|
import com.cloud.kicc.system.api.entity.File; |
|
|
|
import com.cloud.kicc.system.api.entity.OssFile; |
|
|
|
import com.cloud.kicc.system.mapper.FileMapper; |
|
|
|
import com.cloud.kicc.system.mapper.FileMapper; |
|
|
|
import com.cloud.kicc.system.service.FileService; |
|
|
|
import com.cloud.kicc.system.service.FileService; |
|
|
|
import com.cloud.kicc.common.core.api.R; |
|
|
|
|
|
|
|
import com.pig4cloud.plugin.oss.OssProperties; |
|
|
|
import com.pig4cloud.plugin.oss.OssProperties; |
|
|
|
import com.pig4cloud.plugin.oss.service.OssTemplate; |
|
|
|
import com.pig4cloud.plugin.oss.service.OssTemplate; |
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
import lombok.AllArgsConstructor; |
|
|
@ -24,8 +23,7 @@ import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
*<p> |
|
|
|
*<p> |
|
|
|
* 文件管理 |
|
|
|
* 文件管理 |
|
|
@ -37,30 +35,30 @@ import java.util.Map; |
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
@Service |
|
|
|
@Service |
|
|
|
@AllArgsConstructor |
|
|
|
@AllArgsConstructor |
|
|
|
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService { |
|
|
|
public class FileServiceImpl extends ServiceImpl<FileMapper, OssFile> implements FileService { |
|
|
|
|
|
|
|
|
|
|
|
private final OssProperties ossProperties; |
|
|
|
private final OssProperties ossProperties; |
|
|
|
private final OssTemplate ossTemplate; |
|
|
|
private final OssTemplate ossTemplate; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public File uploadFile(MultipartFile file, File ossFile) { |
|
|
|
public OssFile uploadFile(MultipartFile file, OssFile ossFile) { |
|
|
|
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); |
|
|
|
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename()); |
|
|
|
File defaultOssFile = new File().setFileName(fileName) |
|
|
|
OssFile defaultOssOssFile = new OssFile().setFileName(fileName) |
|
|
|
.setBucketName(ossProperties.getBucketName()) |
|
|
|
.setBucketName(ossProperties.getBucketName()) |
|
|
|
.setOriginal(file.getOriginalFilename()) |
|
|
|
.setOriginal(file.getOriginalFilename()) |
|
|
|
.setType(FileUtil.extName(file.getOriginalFilename())) |
|
|
|
.setType(FileUtil.extName(file.getOriginalFilename())) |
|
|
|
.setFileSize(file.getSize()) |
|
|
|
.setFileSize(file.getSize()) |
|
|
|
.setAvailablePath(String.format("/system_proxy/system/file/getFile/%s/%s", ossProperties.getBucketName(), fileName)) |
|
|
|
.setAvailablePath(String.format("/system_proxy/system/file/getFile/%s/%s", ossProperties.getBucketName(), fileName)) |
|
|
|
.setMimeType(file.getContentType()); |
|
|
|
.setMimeType(file.getContentType()); |
|
|
|
if(ObjectUtil.isNotEmpty(ossFile)) BeanUtil.copyProperties(ossFile, defaultOssFile, CopyOptions.create().ignoreNullValue().ignoreError()); |
|
|
|
if(ObjectUtil.isNotEmpty(ossFile)) BeanUtil.copyProperties(ossFile, defaultOssOssFile, CopyOptions.create().ignoreNullValue().ignoreError()); |
|
|
|
try { |
|
|
|
try { |
|
|
|
ossTemplate.putObject(ossProperties.getBucketName(), fileName, file.getContentType(), file.getInputStream()); |
|
|
|
ossTemplate.putObject(ossProperties.getBucketName(), fileName, file.getContentType(), file.getInputStream()); |
|
|
|
// 文件管理数据记录,收集管理追踪文件
|
|
|
|
// 文件管理数据记录,收集管理追踪文件
|
|
|
|
this.save(defaultOssFile); |
|
|
|
this.save(defaultOssOssFile); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw new CheckedException("上传失败"); |
|
|
|
throw new CheckedException("上传失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
return defaultOssFile; |
|
|
|
return defaultOssOssFile; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -77,9 +75,9 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements Fi |
|
|
|
@SneakyThrows |
|
|
|
@SneakyThrows |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void deleteFile(String id) { |
|
|
|
public void deleteFile(String id) { |
|
|
|
File file = this.getById(id); |
|
|
|
OssFile ossFile = this.getById(id); |
|
|
|
if (ObjectUtil.isNotEmpty(file)) { |
|
|
|
if (ObjectUtil.isNotEmpty(ossFile)) { |
|
|
|
ossTemplate.removeObject(ossProperties.getBucketName(), file.getFileName()); |
|
|
|
ossTemplate.removeObject(ossProperties.getBucketName(), ossFile.getFileName()); |
|
|
|
this.removeById(id); |
|
|
|
this.removeById(id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|