package com.example.server.sysOss.service; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.ReflectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.example.client.entity.ErrorCode; import com.example.client.entity.RenException; import com.example.client.service.BaseService; import com.example.server.sysOss.dao.SysOssDao; import com.example.server.sysOss.dto.OssDto; import com.example.server.sysOss.model.SysOss; import lombok.SneakyThrows; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Decoder; import javax.annotation.PostConstruct; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import java.io.*; import java.lang.reflect.Field; import java.security.Key; import java.security.SecureRandom; import java.util.*; import java.util.stream.Collectors; /** * 文件上传 * * @author hehz */ @Service public class SysOssService extends BaseService { @Autowired OssEncryptService ossEncryptService; @Value("${zt.oss.local-path}") private String localPath; private static final String CIPHER_ALGORITHM = "AES"; private static final String key = "zhpt-key#%W"; /** * 通过业务id BusiId和业务类型 busiType获取附件数据 * * @return */ public List getList(Long busiId, String busiType) { return baseDao.getList(busiId, busiType); } public void upload(SysOss oss) { oss.setId(IdWorker.getId()); File oldFile = new File(oss.getPath()); oss.setSize(NumberUtil.round(oldFile.length() / 1024, 2).doubleValue()); String uuid = UUID.randomUUID().toString().replaceAll("-", ""); // 文件路径 String path = DateUtil.format(new Date(), "yyyyMMdd") + "/" + uuid; String newPath = "life-protection/" + path + FilenameUtils.getExtension(oss.getPath()); oss.setPath(newPath); oss.setType(FilenameUtils.getExtension(oss.getPath())); String newFullPath = localPath + newPath; File newFile = new File(newFullPath); try { FileUtils.copyToFile(ossEncryptService.encryptStream(FileUtils.openInputStream(oldFile), ossEncryptService.getKey(key)), newFile); } catch (Exception e) { throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR.getCode(), e, ""); } oss.setUrl("http://127.0.0.1:8050/life-protection/sys/oss/content?_zt_oss_id=" + oss.getId()); this.insert(oss); } public Integer getBusiFieldNameCount(Long busiId, String busiType) { return baseDao.getBusiFieldNameCount(busiId, busiType); } public List getSysOssByBusiType(Map params) { return baseDao.getSysOssByBusiType(params); } }