Search in sources :

Example 1 with OssClient

use of com.ruoyi.common.oss.core.OssClient in project RuoYi-Cloud-Plus by JavaLionLi.

the class SysOssServiceImpl method deleteWithValidByIds.

@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
    // 做一些业务上的校验,判断是否需要校验
    }
    List<SysOss> list = baseMapper.selectBatchIds(ids);
    for (SysOss sysOss : list) {
        OssClient storage = OssFactory.instance(sysOss.getService());
        storage.delete(sysOss.getUrl());
    }
    return baseMapper.deleteBatchIds(ids) > 0;
}
Also used : SysOss(com.ruoyi.resource.domain.SysOss) OssClient(com.ruoyi.common.oss.core.OssClient)

Example 2 with OssClient

use of com.ruoyi.common.oss.core.OssClient in project RuoYi-Cloud-Plus by JavaLionLi.

the class RemoteFileServiceImpl method upload.

/**
 * 文件上传请求
 */
@Transactional(rollbackFor = Exception.class)
@Override
public SysFile upload(String name, String originalFilename, String contentType, byte[] file) throws ServiceException {
    try {
        String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
        OssClient storage = OssFactory.instance();
        UploadResult uploadResult = storage.uploadSuffix(file, suffix, contentType);
        // 保存文件信息
        SysOss oss = new SysOss();
        oss.setUrl(uploadResult.getUrl());
        oss.setFileSuffix(suffix);
        oss.setFileName(uploadResult.getFilename());
        oss.setOriginalName(originalFilename);
        oss.setService(storage.getConfigKey());
        sysOssMapper.insert(oss);
        SysFile sysFile = new SysFile();
        sysFile.setName(uploadResult.getFilename());
        sysFile.setUrl(uploadResult.getUrl());
        return sysFile;
    } catch (Exception e) {
        log.error("上传文件失败", e);
        throw new ServiceException("上传文件失败");
    }
}
Also used : SysOss(com.ruoyi.resource.domain.SysOss) OssClient(com.ruoyi.common.oss.core.OssClient) ServiceException(com.ruoyi.common.core.exception.ServiceException) UploadResult(com.ruoyi.common.oss.entity.UploadResult) SysFile(com.ruoyi.resource.api.domain.SysFile) ServiceException(com.ruoyi.common.core.exception.ServiceException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with OssClient

use of com.ruoyi.common.oss.core.OssClient in project RuoYi-Cloud-Plus by JavaLionLi.

the class SysOssServiceImpl method upload.

@Override
public SysOss upload(MultipartFile file) {
    String originalfileName = file.getOriginalFilename();
    String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
    OssClient storage = OssFactory.instance();
    UploadResult uploadResult;
    try {
        uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
    } catch (IOException e) {
        throw new ServiceException(e.getMessage());
    }
    // 保存文件信息
    SysOss oss = new SysOss();
    oss.setUrl(uploadResult.getUrl());
    oss.setFileSuffix(suffix);
    oss.setFileName(uploadResult.getFilename());
    oss.setOriginalName(originalfileName);
    oss.setService(storage.getConfigKey());
    baseMapper.insert(oss);
    return oss;
}
Also used : SysOss(com.ruoyi.resource.domain.SysOss) OssClient(com.ruoyi.common.oss.core.OssClient) ServiceException(com.ruoyi.common.core.exception.ServiceException) UploadResult(com.ruoyi.common.oss.entity.UploadResult) IOException(java.io.IOException)

Example 4 with OssClient

use of com.ruoyi.common.oss.core.OssClient in project RuoYi-Cloud-Plus by JavaLionLi.

the class OssFactory method refresh.

private static void refresh(String configKey) {
    Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + configKey);
    OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class);
    if (properties == null) {
        throw new OssException("系统异常, '" + configKey + "'配置信息不存在!");
    }
    CLIENT_CACHE.put(configKey, new OssClient(configKey, properties));
}
Also used : OssClient(com.ruoyi.common.oss.core.OssClient) OssProperties(com.ruoyi.common.oss.properties.OssProperties) OssException(com.ruoyi.common.oss.exception.OssException)

Example 5 with OssClient

use of com.ruoyi.common.oss.core.OssClient in project RuoYi-Cloud-Plus by JavaLionLi.

the class OssFactory method init.

/**
 * 初始化工厂
 */
public static void init() {
    log.info("初始化OSS工厂");
    RedisUtils.subscribe(OssConstant.CACHE_CONFIG_KEY, String.class, configKey -> {
        OssClient client = getClient(configKey);
        // 未初始化不处理
        if (client != null) {
            refresh(configKey);
            log.info("订阅刷新OSS配置 => " + configKey);
        }
    });
}
Also used : OssClient(com.ruoyi.common.oss.core.OssClient)

Aggregations

OssClient (com.ruoyi.common.oss.core.OssClient)5 SysOss (com.ruoyi.resource.domain.SysOss)3 ServiceException (com.ruoyi.common.core.exception.ServiceException)2 UploadResult (com.ruoyi.common.oss.entity.UploadResult)2 OssException (com.ruoyi.common.oss.exception.OssException)1 OssProperties (com.ruoyi.common.oss.properties.OssProperties)1 SysFile (com.ruoyi.resource.api.domain.SysFile)1 IOException (java.io.IOException)1 Transactional (org.springframework.transaction.annotation.Transactional)1