package cn.besteam.modules.system.service.impl; import cn.besteam.common.constant.ErrorCode; import cn.besteam.common.constant.OssTypeEM; import cn.besteam.common.exception.BusinessException; import cn.besteam.common.util.StringUtil; import cn.besteam.config.OssConfig; import cn.besteam.modules.system.service.IOssService; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSException; import com.aliyun.oss.common.utils.BinaryUtil; import com.aliyun.oss.model.ObjectMetadata; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.DateFormatUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.Date; /** * @author jacob */ @Service("ossService") @Slf4j public class OssServiceImpl implements IOssService { /** * 系统文件分割符 */ private final String separator = File.separator; /** * oss配置 */ @Resource private OssConfig ossConfig; /** * 上传客户端 */ @Resource private OSSClient uploadOSSClient; /** * @param filenameExtension 文件后缀 * @return 文件的contentType * @Describe 判断OSS服务文件上传时文件的contentType */ private static String getContentType(String filenameExtension) { return OssTypeEM.getByFileType(filenameExtension.toLowerCase()).getContentType(); } /** * @param file 文件 * @param file 文件 * @return 文件访问路径 * @Describe 上传文件,返回文件访问路径(默认上传到根目录) */ @Override public String upload(MultipartFile file) throws BusinessException { return upload(ossConfig.getFolder(), file); } // /** // * @param file 文件 // * @param dir 子目录 // * @return 文件访问路径 // * @Describe 上传文件,返回文件访问路径(默认上传到根目录+子目录) // */ // public Result upload(MultipartFile file, String dir) throws BusinessException { // return upload(ossConfig.getFolder()+separator+dir,file); // } /** * @param storagePath 存储的路径 * @param file 文件 * @return 文件访问路径 * @Describe 上传文件,返回文件访问路径 */ @Override public String upload(String storagePath, MultipartFile file) throws BusinessException { String originFileName = file.getOriginalFilename(); String fileExt = originFileName.substring(originFileName.lastIndexOf(".") + 1); String fileName = originFileName.substring(0,originFileName.lastIndexOf(".") ); // 设置文件名 String filePathName = generateRelativeStoragePath(storagePath, fileExt,fileName); byte[] fileContent = null; try { fileContent = file.getBytes(); } catch (Exception e) { log.error("Cannot get file content from {}.", originFileName); throw new BusinessException(ErrorCode.ERR_500, "不能读取" + originFileName + "内容"); } ObjectMetadata meta = new ObjectMetadata(); // 设置上传文件长度 meta.setContentLength(file.getSize()); // 设置上传MD5校验 String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(fileContent)); meta.setContentMD5(md5); meta.setContentType(getContentType(fileExt)); // 存储 try { uploadOSSClient.putObject(ossConfig.getBucketName(), filePathName, file.getInputStream(), meta); } catch (OSSException | ClientException | IOException e) { log.error("OSS storage error", e); throw new BusinessException(ErrorCode.ERR_600); } return ossConfig.getDownloadEndpoint() + filePathName; } /** * 上传 * * @param imageBytes * @return */ @Override public String uploadImage(byte[] imageBytes) throws BusinessException { return uploadImage(ossConfig.getFolder(), imageBytes); } @Override public String uploadImage(String storagePath, byte[] imageBytes) throws BusinessException { String fileType = "image/jpeg"; // 设置文件名 String filePathName = generateRelativeStoragePath(storagePath, "jpeg",null); ObjectMetadata meta = new ObjectMetadata(); // 设置上传文件长度 meta.setContentLength(imageBytes.length); // 设置上传MD5校验 String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(imageBytes)); meta.setContentMD5(md5); meta.setContentType(fileType); // 存储 try { uploadOSSClient.putObject(ossConfig.getBucketName(), filePathName, new ByteArrayInputStream(imageBytes), meta); } catch (Exception e) { log.error("OSS storage error", e); throw new BusinessException(ErrorCode.ERR_600); } return ossConfig.getDownloadEndpoint() + filePathName; } /** *
* 获取存储的相对路径
* 规则path + / + yyyyMMddHH + uuid
*
*
* @param storagePath
* @return
*/
private String generateRelativeStoragePath(String storagePath, String fileType,String fileName) {
String time = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
String uuid = StringUtil.uuid();
StringBuilder sb = new StringBuilder();
if (StringUtil.isNotBlank(storagePath)) {
sb.append(storagePath).append("/");
}
if(fileName == null){
sb.append(time).append(uuid);
}else {
sb.append(fileName).append(time);
}
if (StringUtil.isNotBlank(fileType)) {
sb.append(".").append(fileType);
}
return sb.toString();
}
}