feat: 新增统一文件上传接口

pull/14/head
Parker 3 years ago
parent c4306522de
commit 0327bede85

@ -128,11 +128,11 @@ public interface UserApi {
/**
*
* @param request request
* @param userAvatarModel
* @return ResultVo
*/
@PostMapping("/updateAvatar")
ResultVo<?> updateAvatar(MultipartHttpServletRequest request);
ResultVo<?> updateAvatar(@RequestBody UserAvatarModel userAvatarModel);
/**

@ -0,0 +1,38 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.api.wrapper.system.user;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opsli.common.annotation.validator.Validator;
import org.opsli.common.enums.ValidatorType;
/**
*
*
* @author Parker
* @date 2020-09-16 17:33
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class UserAvatarModel {
@ApiModelProperty(value = "图片地址")
@Validator({ValidatorType.IS_NOT_NULL, ValidatorType.IS_URL})
private String imgUrl;
}

@ -63,6 +63,7 @@ import org.opsli.plugins.oss.service.BaseOssStorageService;
import org.opsli.plugins.oss.service.OssStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@ -201,44 +202,20 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
/**
*
* @param request request
* @param userAvatarModel
* @return ResultVo
*/
@ApiOperation(value = "上传头像", notes = "上传头像")
@Override
public ResultVo<?> updateAvatar(MultipartHttpServletRequest request) {
Iterator<String> itr = request.getFileNames();
String uploadedFile = itr.next();
List<MultipartFile> files = request.getFiles(uploadedFile);
if (CollectionUtils.isEmpty(files)) {
// 请选择文件
return ResultVo.error(SystemMsg.EXCEPTION_USER_FILE_NULL.getCode(),
SystemMsg.EXCEPTION_USER_FILE_NULL.getMessage());
}
try {
MultipartFile multipartFile = files.get(0);
Resource resource = multipartFile.getResource();
String filename = resource.getFilename();
// 调用OSS 服务保存头像
OssStorageService ossStorageService = OssStorageFactory.INSTANCE.getHandle();
BaseOssStorageService.FileAttr fileAttr = ossStorageService.upload(
multipartFile.getInputStream(), FileUtil.extName(filename));
public ResultVo<?> updateAvatar(UserAvatarModel userAvatarModel) {
UserModel user = UserUtil.getUserBySource();
// 更新头像至数据库
UserModel userModel = new UserModel();
userModel.setId(user.getId());
userModel.setAvatar(fileAttr.getFileStoragePath());
userModel.setAvatar(userAvatarModel.getImgUrl());
IService.updateAvatar(userModel);
// 刷新用户信息
UserUtil.refreshUser(user);
}catch (IOException e){
log.error(e.getMessage(), e);
return ResultVo.error("更新头像失败,请稍后再试");
}
return ResultVo.success();
}

@ -1,8 +1,27 @@
package org.opsli.modulars.tools.oss.web;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.plugins.oss.OssStorageFactory;
import org.opsli.plugins.oss.service.BaseOssStorageService;
import org.opsli.plugins.oss.service.OssStorageService;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
@ -16,6 +35,39 @@ import org.opsli.common.annotation.ApiRestController;
@ApiRestController("/{ver}/tools/oss")
public class OssRestController {
/**
*
* @param request request
* @return ResultVo
*/
@ApiOperation(value = "文件上传", notes = "文件上传")
@PostMapping("/upload")
public ResultVo<?> upload(MultipartHttpServletRequest request) {
Iterator<String> itr = request.getFileNames();
String uploadedFile = itr.next();
List<MultipartFile> files = request.getFiles(uploadedFile);
if (CollectionUtils.isEmpty(files)) {
// 请选择文件
return ResultVo.error(SystemMsg.EXCEPTION_USER_FILE_NULL.getCode(),
SystemMsg.EXCEPTION_USER_FILE_NULL.getMessage());
}
try {
MultipartFile multipartFile = files.get(0);
Resource resource = multipartFile.getResource();
String filename = resource.getFilename();
// 调用OSS 服务保存文件
OssStorageService ossStorageService = OssStorageFactory.INSTANCE.getHandle();
BaseOssStorageService.FileAttr fileAttr = ossStorageService.upload(
multipartFile.getInputStream(), FileUtil.extName(filename));
return ResultVo.success(fileAttr);
}catch (IOException e){
log.error(e.getMessage(), e);
}
return ResultVo.error();
}
}

Loading…
Cancel
Save