1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| package com.jhj.media.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.j256.simplemagic.ContentInfo; import com.j256.simplemagic.ContentInfoUtil; import com.jhj.base.exception.UltimateException; import com.jhj.base.model.PageParams; import com.jhj.base.model.PageResult; import com.jhj.media.mapper.MediaFilesMapper; import com.jhj.media.model.dto.QueryMediaParamsDto; import com.jhj.media.model.dto.UploadFileParamsDto; import com.jhj.media.model.dto.UploadFileResultDto; import com.jhj.media.model.po.MediaFiles; import com.jhj.media.service.MediaFileService; import io.minio.MinioClient; import io.minio.UploadObjectArgs; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.Date; import java.util.List;
@Service @Slf4j public class MediaFileServiceImpl implements MediaFileService { @Autowired MediaFilesMapper mediaFilesMapper;
@Autowired MinioClient minioClient;
@Autowired @Lazy MediaFileService currentProxy;
@Value("${minio.bucket.files}") private String bucket_mediafiles;
@Value("${minio.bucket.videofiles}") private String bucket_video;
@Override public PageResult<MediaFiles> queryMediaFiels(Long companyId, PageParams pageParams, QueryMediaParamsDto queryMediaParamsDto) {
LambdaQueryWrapper<MediaFiles> queryWrapper = new LambdaQueryWrapper<>();
Page<MediaFiles> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize()); Page<MediaFiles> pageResult = mediaFilesMapper.selectPage(page, queryWrapper); List<MediaFiles> list = pageResult.getRecords(); long total = pageResult.getTotal(); PageResult<MediaFiles> mediaListResult = new PageResult<>(list, total, pageParams.getPageNo(), pageParams.getPageSize()); return mediaListResult;
}
@Override public UploadFileResultDto uploadFile(Long companyId, UploadFileParamsDto uploadFileParamsDto, String localFilePath) { String filename = uploadFileParamsDto.getFilename(); String extension = filename.substring(filename.lastIndexOf(".")); String mimeType = getMimeType(extension); String fileMd5 = getFileMd5(new File(localFilePath)); String defaultFolderPath = getDefaultFolderPath(); String objectName = defaultFolderPath + fileMd5 + extension; boolean result = addMediaFilesToMinio(localFilePath, mimeType, bucket_mediafiles, objectName); if(!result){ UltimateException.cast("上传文件失败"); }
MediaFiles mediaFiles = currentProxy.addMediaFilesToDb(companyId, fileMd5, uploadFileParamsDto, bucket_mediafiles, objectName); if(mediaFiles==null){ UltimateException.cast("文件上传后,保存信息失败"); }
UploadFileResultDto uploadFileResultDto = new UploadFileResultDto(); BeanUtils.copyProperties(mediaFiles,uploadFileResultDto); return uploadFileResultDto; }
private String getMimeType(String extension) { if (extension == null) { extension = ""; } ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension); String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE; if (extensionMatch != null) { mimeType = extensionMatch.getMimeType(); } return mimeType; }
private boolean addMediaFilesToMinio(String localFilePath, String mimeType, String bucket, String objectName) { try { minioClient.uploadObject( UploadObjectArgs.builder() .bucket(bucket) .filename(localFilePath) .object(objectName) .contentType(mimeType) .bucket(bucket).build() ); log.debug("上传文件到minio成功,bucket:{},objectName:{}",bucket,objectName); return true; } catch (Exception e) { e.printStackTrace(); log.error("上传文件出错,bucket:{},objectName:{},错误信息:{}",bucket,objectName,e.getMessage()); } return false; } private String getDefaultFolderPath(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String folder = sdf.format(new Date()).replace("-", "/") + "/"; return folder; }
private String getFileMd5(File file){ try(FileInputStream fileInputStream=new FileInputStream(file)){ String fileMd5 = DigestUtils.md5Hex(fileInputStream); return fileMd5; }catch (Exception e){ e.printStackTrace(); return null; } }
@Transactional public MediaFiles addMediaFilesToDb(Long companyId, String fileMd5, UploadFileParamsDto uploadFileParamsDto,String bucket,String objectName){ MediaFiles mediaFiles = mediaFilesMapper.selectById(fileMd5); if (mediaFiles == null){ mediaFiles=new MediaFiles(); BeanUtils.copyProperties(uploadFileParamsDto,mediaFiles); mediaFiles.setId(fileMd5); mediaFiles.setFileId(fileMd5); mediaFiles.setCompanyId(companyId); mediaFiles.setBucket(bucket); mediaFiles.setFilePath(objectName); mediaFiles.setUrl("/"+bucket+"/"+objectName); mediaFiles.setCreateDate(LocalDateTime.now()); mediaFiles.setStatus("1"); mediaFiles.setAuditStatus("002003"); int insert = mediaFilesMapper.insert(mediaFiles); int i=1/0; if(insert<0){ log.error("向数据库保存文件失败,bucket:{},objectName:{}",bucket,objectName); return null; } return mediaFiles; } return mediaFiles; } }
|