-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
2,331 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
392 changes: 392 additions & 0 deletions
392
...ingboot/DataBackup/src/main/java/top/hcode/hoj/controller/file/ContestFileController.java
Large diffs are not rendered by default.
Oops, something went wrong.
167 changes: 167 additions & 0 deletions
167
hoj-springboot/DataBackup/src/main/java/top/hcode/hoj/controller/file/ImageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package top.hcode.hoj.controller.file; | ||
|
||
import cn.hutool.core.io.FileUtil; | ||
import cn.hutool.core.map.MapUtil; | ||
import cn.hutool.core.util.IdUtil; | ||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.shiro.authz.annotation.RequiresAuthentication; | ||
import org.apache.shiro.authz.annotation.RequiresRoles; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import top.hcode.hoj.common.result.CommonResult; | ||
import top.hcode.hoj.pojo.entity.Role; | ||
import top.hcode.hoj.pojo.entity.UserInfo; | ||
import top.hcode.hoj.pojo.vo.UserRolesVo; | ||
import top.hcode.hoj.service.impl.FileServiceImpl; | ||
import top.hcode.hoj.service.impl.UserInfoServiceImpl; | ||
import top.hcode.hoj.utils.Constants; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.File; | ||
|
||
/** | ||
* @Author: Himit_ZH | ||
* @Date: 2021/10/5 19:46 | ||
* @Description: | ||
*/ | ||
@Controller | ||
@RequestMapping("/api/file") | ||
@Slf4j(topic = "hoj") | ||
public class ImageController { | ||
|
||
@Autowired | ||
private FileServiceImpl fileService; | ||
|
||
@Autowired | ||
private UserInfoServiceImpl userInfoService; | ||
|
||
@RequestMapping(value = "/upload-avatar", method = RequestMethod.POST) | ||
@RequiresAuthentication | ||
@ResponseBody | ||
@Transactional | ||
public CommonResult uploadAvatar(@RequestParam("image") MultipartFile image, HttpServletRequest request) { | ||
if (image == null) { | ||
return CommonResult.errorResponse("上传的头像图片文件不能为空!"); | ||
} | ||
if (image.getSize() > 1024 * 1024 * 2) { | ||
return CommonResult.errorResponse("上传的头像图片文件大小不能大于2M!"); | ||
} | ||
//获取文件后缀 | ||
String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1); | ||
if (!"jpg,jpeg,gif,png,webp".toUpperCase().contains(suffix.toUpperCase())) { | ||
return CommonResult.errorResponse("请选择jpg,jpeg,gif,png,webp格式的头像图片!"); | ||
} | ||
//若不存在该目录,则创建目录 | ||
FileUtil.mkdir(Constants.File.USER_AVATAR_FOLDER.getPath()); | ||
//通过UUID生成唯一文件名 | ||
String filename = IdUtil.simpleUUID() + "." + suffix; | ||
try { | ||
//将文件保存指定目录 | ||
image.transferTo(FileUtil.file(Constants.File.USER_AVATAR_FOLDER.getPath() + File.separator + filename)); | ||
} catch (Exception e) { | ||
log.error("头像文件上传异常-------------->", e); | ||
return CommonResult.errorResponse("服务器异常:头像上传失败!", CommonResult.STATUS_ERROR); | ||
} | ||
|
||
// 获取当前登录用户 | ||
HttpSession session = request.getSession(); | ||
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo"); | ||
|
||
|
||
// 将当前用户所属的file表中avatar类型的实体的delete设置为1; | ||
fileService.updateFileToDeleteByUidAndType(userRolesVo.getUid(), "avatar"); | ||
|
||
//更新user_info里面的avatar | ||
UpdateWrapper<UserInfo> userInfoUpdateWrapper = new UpdateWrapper<>(); | ||
userInfoUpdateWrapper.set("avatar", Constants.File.IMG_API.getPath() + filename) | ||
.eq("uuid", userRolesVo.getUid()); | ||
userInfoService.update(userInfoUpdateWrapper); | ||
|
||
// 插入file表记录 | ||
top.hcode.hoj.pojo.entity.File imgFile = new top.hcode.hoj.pojo.entity.File(); | ||
imgFile.setName(filename).setFolderPath(Constants.File.USER_AVATAR_FOLDER.getPath()) | ||
.setFilePath(Constants.File.USER_AVATAR_FOLDER.getPath() + File.separator + filename) | ||
.setSuffix(suffix) | ||
.setType("avatar") | ||
.setUid(userRolesVo.getUid()); | ||
fileService.saveOrUpdate(imgFile); | ||
|
||
// 更新session | ||
userRolesVo.setAvatar(Constants.File.IMG_API.getPath() + filename); | ||
session.setAttribute("userInfo", userRolesVo); | ||
return CommonResult.successResponse(MapUtil.builder() | ||
.put("uid", userRolesVo.getUid()) | ||
.put("username", userRolesVo.getUsername()) | ||
.put("nickname", userRolesVo.getNickname()) | ||
.put("avatar", Constants.File.IMG_API.getPath() + filename) | ||
.put("email", userRolesVo.getEmail()) | ||
.put("number", userRolesVo.getNumber()) | ||
.put("school", userRolesVo.getSchool()) | ||
.put("course", userRolesVo.getCourse()) | ||
.put("signature", userRolesVo.getSignature()) | ||
.put("realname", userRolesVo.getRealname()) | ||
.put("github", userRolesVo.getGithub()) | ||
.put("blog", userRolesVo.getBlog()) | ||
.put("cfUsername", userRolesVo.getCfUsername()) | ||
.put("roleList", userRolesVo.getRoles().stream().map(Role::getRole)) | ||
.map(), "设置新头像成功!"); | ||
} | ||
|
||
|
||
@RequestMapping(value = "/upload-carouse-img", method = RequestMethod.POST) | ||
@RequiresAuthentication | ||
@ResponseBody | ||
@Transactional | ||
@RequiresRoles("root") | ||
public CommonResult uploadCarouselImg(@RequestParam("file") MultipartFile image, HttpServletRequest request) { | ||
|
||
if (image == null) { | ||
return CommonResult.errorResponse("上传的图片文件不能为空!"); | ||
} | ||
|
||
//获取文件后缀 | ||
String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1); | ||
if (!"jpg,jpeg,gif,png,webp,jfif,svg".toUpperCase().contains(suffix.toUpperCase())) { | ||
return CommonResult.errorResponse("请选择jpg,jpeg,gif,png,webp,jfif,svg格式的头像图片!"); | ||
} | ||
//若不存在该目录,则创建目录 | ||
FileUtil.mkdir(Constants.File.HOME_CAROUSEL_FOLDER.getPath()); | ||
//通过UUID生成唯一文件名 | ||
String filename = IdUtil.simpleUUID() + "." + suffix; | ||
try { | ||
//将文件保存指定目录 | ||
image.transferTo(FileUtil.file(Constants.File.HOME_CAROUSEL_FOLDER.getPath() + File.separator + filename)); | ||
} catch (Exception e) { | ||
log.error("图片文件上传异常-------------->{}", e.getMessage()); | ||
return CommonResult.errorResponse("服务器异常:图片上传失败!", CommonResult.STATUS_ERROR); | ||
} | ||
|
||
// 获取当前登录用户 | ||
HttpSession session = request.getSession(); | ||
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo"); | ||
|
||
|
||
// 插入file表记录 | ||
top.hcode.hoj.pojo.entity.File imgFile = new top.hcode.hoj.pojo.entity.File(); | ||
imgFile.setName(filename).setFolderPath(Constants.File.HOME_CAROUSEL_FOLDER.getPath()) | ||
.setFilePath(Constants.File.HOME_CAROUSEL_FOLDER.getPath() + File.separator + filename) | ||
.setSuffix(suffix) | ||
.setType("carousel") | ||
.setUid(userRolesVo.getUid()); | ||
fileService.saveOrUpdate(imgFile); | ||
|
||
return CommonResult.successResponse(MapUtil.builder() | ||
.put("id", imgFile.getId()) | ||
.put("url", Constants.File.IMG_API.getPath() + filename) | ||
.map(), "上传图片成功!"); | ||
} | ||
|
||
} |
Oops, something went wrong.