diff --git a/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteFileService.java b/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteFileService.java index 429d3dbf..9a39759f 100644 --- a/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteFileService.java +++ b/ruoyi-api/ruoyi-api-resource/src/main/java/org/dromara/resource/api/RemoteFileService.java @@ -27,7 +27,7 @@ public interface RemoteFileService { * @return 结果 * @throws ServiceException */ - RemoteFile upload(byte[] file) throws ServiceException; + RemoteFile uploadImg(byte[] file) throws ServiceException; /** * 通过ossId查询对应的url diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/ContentTypeEnum.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/ContentTypeEnum.java index 78530307..1c67a916 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/ContentTypeEnum.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/enums/ContentTypeEnum.java @@ -30,7 +30,13 @@ public enum ContentTypeEnum { /** * GIF图片类型 */ - GIF("gif", "image/gif"); + GIF("gif", "image/gif"), + + WEBP("webp", "image/webp"), + + SVG("svg", "image/svg+xml"), + + ; private final String extension; private final String contentType; @@ -41,13 +47,13 @@ public enum ContentTypeEnum { * @param extension 文件扩展名 * @return 对应的内容类型,如果未找到则返回null */ - public static String getContentType(String extension) { + public static ContentTypeEnum ContentTypeEnum(String extension) { for (ContentTypeEnum type : values()) { if (type.extension.equalsIgnoreCase(extension)) { - return type.contentType; + return type; } } - return null; + return JPG; } /** diff --git a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/service/impl/SisAlarmEventsServiceImpl.java b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/service/impl/SisAlarmEventsServiceImpl.java index 28680f85..15a31d27 100644 --- a/ruoyi-modules/Sis/src/main/java/org/dromara/sis/service/impl/SisAlarmEventsServiceImpl.java +++ b/ruoyi-modules/Sis/src/main/java/org/dromara/sis/service/impl/SisAlarmEventsServiceImpl.java @@ -11,6 +11,7 @@ import org.dromara.common.core.utils.StringUtils; import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.resource.api.RemoteFileService; +import org.dromara.resource.api.domain.RemoteFile; import org.dromara.sis.domain.SisAlarmEvents; import org.dromara.sis.domain.bo.SisAlarmEventsBo; import org.dromara.sis.domain.vo.SisAlarmEventsVo; @@ -41,6 +42,7 @@ public class SisAlarmEventsServiceImpl implements ISisAlarmEventsService { @DubboReference private RemoteFileService remoteFileService; + /** * 查询告警 * @@ -48,7 +50,7 @@ public class SisAlarmEventsServiceImpl implements ISisAlarmEventsService { * @return 告警 */ @Override - public SisAlarmEventsVo queryById(Long id){ + public SisAlarmEventsVo queryById(Long id) { return baseMapper.selectVoById(id); } @@ -125,7 +127,7 @@ public class SisAlarmEventsServiceImpl implements ISisAlarmEventsService { /** * 保存前的数据校验 */ - private void validEntityBeforeSave(SisAlarmEvents entity){ + private void validEntityBeforeSave(SisAlarmEvents entity) { //TODO 做一些数据校验,如唯一约束 } @@ -138,7 +140,7 @@ public class SisAlarmEventsServiceImpl implements ISisAlarmEventsService { */ @Override public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { - if(isValid){ + if (isValid) { //TODO 做一些业务上的校验,判断是否需要校验 } return baseMapper.deleteByIds(ids) > 0; @@ -149,15 +151,13 @@ public class SisAlarmEventsServiceImpl implements ISisAlarmEventsService { public void createAlarmRecord(String deviceIp, Integer level, Integer type, byte[] smallImg, byte[] bigImg) { // 校验设备信息 SisDeviceManageVo sisDeviceManageVo = deviceManageService.queryVoByDeviceIp(deviceIp); - if(sisDeviceManageVo == null){ + if (sisDeviceManageVo == null) { log.error("设备信息不存在,放弃此条告警记录。"); return; } // 上传图片 - remoteFileService.upload(smallImg); - - - + RemoteFile small = remoteFileService.uploadImg(smallImg); + RemoteFile big = remoteFileService.uploadImg(bigImg); } diff --git a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteFileServiceImpl.java b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteFileServiceImpl.java index 306e57b9..ff8551cd 100644 --- a/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteFileServiceImpl.java +++ b/ruoyi-modules/ruoyi-resource/src/main/java/org/dromara/resource/dubbo/RemoteFileServiceImpl.java @@ -5,6 +5,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboService; +import org.dromara.common.core.enums.ContentTypeEnum; import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.utils.Base64Utils; import org.dromara.common.core.utils.MapstructUtils; @@ -68,16 +69,33 @@ public class RemoteFileServiceImpl implements RemoteFileService { } @Override - public RemoteFile upload(byte[] file) throws ServiceException { - // 计算文件类型 - String type = Base64Utils.getType(file); - // 获contentType - - - - - - return null; + public RemoteFile uploadImg(byte[] file) throws ServiceException { + try { + // 计算文件类型 + String type = Base64Utils.getType(file); + // 获contentType + ContentTypeEnum contentTypeEnum = ContentTypeEnum.ContentTypeEnum(type); + OssClient storage = OssFactory.instance(); + UploadResult uploadResult = storage.uploadSuffix(file, type, contentTypeEnum.getContentType()); + // 保存文件信息 + SysOssBo oss = new SysOssBo(); + oss.setUrl(uploadResult.getUrl()); + oss.setFileSuffix(type); + oss.setFileName(uploadResult.getFilename()); + oss.setOriginalName(uploadResult.getFilename()); + oss.setService(storage.getConfigKey()); + sysOssService.insertByBo(oss); + RemoteFile sysFile = new RemoteFile(); + sysFile.setOssId(oss.getOssId()); + sysFile.setName(uploadResult.getFilename()); + sysFile.setUrl(uploadResult.getUrl()); + sysFile.setOriginalName(uploadResult.getFilename()); + sysFile.setFileSuffix(type); + return sysFile; + } catch (Exception e) { + log.error("上传文件失败", e); + throw new ServiceException("上传文件失败"); + } } /**