Search in sources :

Example 1 with SysFile

use of com.ruoyi.resource.api.domain.SysFile in project RuoYi-Cloud-Plus by JavaLionLi.

the class SysProfileController method avatar.

/**
 * 头像上传
 */
@GlobalTransactional(rollbackFor = Exception.class)
@ApiOperation("头像上传")
@ApiImplicitParams({ @ApiImplicitParam(name = "avatarfile", value = "用户头像", paramType = "query", dataTypeClass = File.class, required = true) })
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping("/avatar")
public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        SysFile sysFile = remoteFileService.upload(file.getName(), file.getOriginalFilename(), file.getContentType(), file.getBytes());
        if (ObjectUtil.isNull(sysFile)) {
            return R.fail("文件服务异常,请联系管理员");
        }
        String url = sysFile.getUrl();
        if (userService.updateUserAvatar(LoginHelper.getUsername(), url)) {
            Map<String, Object> ajax = new HashMap<>();
            ajax.put("imgUrl", url);
            return R.ok(ajax);
        }
    }
    return R.fail("上传图片异常,请联系管理员");
}
Also used : HashMap(java.util.HashMap) SysFile(com.ruoyi.resource.api.domain.SysFile) GlobalTransactional(io.seata.spring.annotation.GlobalTransactional) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) Log(com.ruoyi.common.log.annotation.Log) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with SysFile

use of com.ruoyi.resource.api.domain.SysFile 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)

Aggregations

SysFile (com.ruoyi.resource.api.domain.SysFile)2 ServiceException (com.ruoyi.common.core.exception.ServiceException)1 Log (com.ruoyi.common.log.annotation.Log)1 OssClient (com.ruoyi.common.oss.core.OssClient)1 UploadResult (com.ruoyi.common.oss.entity.UploadResult)1 SysOss (com.ruoyi.resource.domain.SysOss)1 GlobalTransactional (io.seata.spring.annotation.GlobalTransactional)1 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 Transactional (org.springframework.transaction.annotation.Transactional)1