| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.file.FileUploadDto; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.admin.minio.BroadcastFileUtils; |
| | | import com.sandu.ximon.admin.minio.entity.FileSuffix; |
| | | import com.sandu.ximon.admin.minio.entity.MinIoConstant; |
| | | import com.sandu.ximon.admin.minio.utils.MinIoUtil; |
| | | import com.sandu.ximon.admin.param.IpVolumeFileParam; |
| | | import com.sandu.ximon.admin.param.ReceiveParam; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.dao.domain.IpVolumeFile; |
| | | import com.sandu.ximon.dao.mapper.IpVolumeFileMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | /** |
| | | * @Author liuhaonan |
| | | * @Date 2021/12/16 10:48 |
| | | * @Version 1.0 |
| | | */ |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class IpVolumeFileService extends BaseServiceImpl<IpVolumeFileMapper, IpVolumeFile> { |
| | | private final IpVolumeFileMapper ipVolumeFileMapper; |
| | | private final ClientService clientService; |
| | | private MinIoUtil minIoUtil; |
| | | |
| | | |
| | | public boolean addFile(IpVolumeFileParam fileParam) { |
| | | IpVolumeFile file = new IpVolumeFile(); |
| | | file.setUserId(SecurityUtils.getClientId()); |
| | | if(SecurityUtils.getClientId()!=null){ |
| | | // if(SecurityUtils.getClientId()!=null){ |
| | | // file.setUserName(SecurityUtils.getUsername()); |
| | | // } |
| | | // |
| | | if (SecurityUtils.getClientId() != null) { |
| | | file.setUserName(SecurityUtils.getUsername()); |
| | | } |
| | | |
| | | if(clientService.getClientId()!=null){ |
| | | file.setUserId(SecurityUtils.getClientId()); |
| | | file.setClientId(clientService.getClientId()); |
| | | } |
| | | |
| | | file.setFileName(fileParam.getFileName()); |
| | | file.setFileSize(fileParam.getFileSizeIp()); |
| | | file.setFileUrl(fileParam.getFileUrl()); |
| | | file.setOriginSize(fileParam.getOriginSize()); |
| | | |
| | | /** |
| | | * 音柱文件新增 日志记录开始 |
| | | */ |
| | | String content = "音柱文件名称:" + fileParam.getFileName() + ",文件大小:" + fileParam.getOriginSize() + ",文件路径:" + fileParam.getFileUrl(); |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件新增", content); |
| | | /** |
| | | * 音柱文件新增 日志记录结束 |
| | | */ |
| | | return save(file); |
| | | } |
| | | |
| | | public boolean deleteFile(Long fileId) { |
| | | IpVolumeFile byId = getById(fileId); |
| | | String bucketName; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName(); |
| | | } else { |
| | | bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId(); |
| | | } |
| | | if (byId == null) { |
| | | throw new BusinessException("找不到对应文件"); |
| | | } |
| | | boolean b = minIoUtil.deleteFile(bucketName, byId.getFileName()); |
| | | // if (!b) { |
| | | // throw new BusinessException("服务器删除失败"); |
| | | // } |
| | | /** |
| | | * 音柱文件删除 日志记录开始 |
| | | */ |
| | | String content = "删除文件id:"+fileId+"音柱文件名称:" + byId.getFileName() + ",文件大小:" + byId.getOriginSize() + ",文件路径:" + byId.getFileUrl(); |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件删除", content); |
| | | /** |
| | | * 音柱文件删除 日志记录结束 |
| | | */ |
| | | |
| | | return removeById(fileId); |
| | | } |
| | | |
| | | public LambdaQueryWrapper<IpVolumeFile> listFile(BaseConditionVO baseConditionVO, String keyword) { |
| | | LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class).eq(IpVolumeFile::getClientId, clientService.getClientId()); |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | if (keyword.isEmpty()) { |
| | | return wrapper; |
| | | } else { |
| | | return wrapper.like(IpVolumeFile::getFileName,keyword); |
| | | LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class); |
| | | |
| | | //不是超管 |
| | | if (SecurityUtils.getClientId() != null) { |
| | | wrapper = wrapper.eq(IpVolumeFile::getClientId, SecurityUtils.getUserId()) |
| | | .or(file -> { |
| | | file.eq(IpVolumeFile::getUserId, SecurityUtils.getUserId()); |
| | | }); |
| | | } |
| | | |
| | | if (keyword != null && !keyword.isEmpty()) { |
| | | wrapper.like(IpVolumeFile::getFileName, keyword); |
| | | } |
| | | return wrapper; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param file |
| | | * @param |
| | | * @return |
| | | */ |
| | | public FileUploadDto upload(MultipartFile file) { |
| | | log.error("音柱文件上传:(filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId()); |
| | | // 校验是否为mp3文件 |
| | | if (!BroadcastFileUtils.isMp3File(file.getOriginalFilename(), FileSuffix.MP3)) { |
| | | log.error("音柱文件上传:(上传失败, 非MP3文件) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId()); |
| | | throw new BusinessException("上传失败, 非MP3文件"); |
| | | } |
| | | // 判断该文件是否存在 |
| | | String bucketName; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName(); |
| | | } else { |
| | | bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId(); |
| | | } |
| | | if (minIoUtil.objectExists(bucketName, file.getOriginalFilename())) { |
| | | log.error("音柱文件上传:(上传失败, 同名文件已存在) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId()); |
| | | throw new BusinessException("上传失败, 同名文件已存在"); |
| | | } |
| | | |
| | | |
| | | String upload = minIoUtil.upload(bucketName, file); |
| | | |
| | | if (null == upload) { |
| | | log.error("音柱文件上传:(上传失败, 文件服务器出错) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId()); |
| | | throw new BusinessException("上传失败, 文件服务器出错"); |
| | | } |
| | | FileUploadDto fileUploadDto = new FileUploadDto(); |
| | | fileUploadDto.setFileUrl(upload); |
| | | fileUploadDto.setFileSize(file.getSize()); |
| | | fileUploadDto.setFileName(file.getOriginalFilename()); |
| | | fileUploadDto.setFileType(file.getContentType()); |
| | | |
| | | /** |
| | | * 音柱文件上传 日志记录开始 |
| | | */ |
| | | String content = "文件内容:"+ JSON.toJSONString(fileUploadDto); |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件上传", content); |
| | | /** |
| | | * 音柱文件上传 日志记录结束 |
| | | */ |
| | | |
| | | return fileUploadDto; |
| | | } |
| | | |
| | | } |