107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
package com.gunshi.project.hsz.controller;
|
||
|
||
import com.gunshi.core.GunShiCoreProperties;
|
||
import com.gunshi.core.result.R;
|
||
import com.gunshi.core.session.entity.SessionUser;
|
||
import com.gunshi.core.session.service.BaseSessionService;
|
||
import com.gunshi.file.model.FileDescriptor;
|
||
import com.gunshi.file.service.IFileService;
|
||
import com.gunshi.file.service.LoginFileService;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.Parameter;
|
||
import jakarta.servlet.http.HttpServletRequest;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestParam;
|
||
import org.springframework.web.bind.annotation.RequestPart;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
/**
|
||
* 类描述
|
||
*
|
||
* @author lyf
|
||
* @version 1.0.0
|
||
* @since 2024-03-12
|
||
*/
|
||
public abstract class AbstractCommonFileController implements ICommonFileController {
|
||
|
||
@Override
|
||
public R<FileDescriptor> uploadSingle(MultipartFile file, String groupId, String businessType, Long userId, HttpServletRequest request) throws Exception {
|
||
return ICommonFileController.super.uploadSingle(file, getGroupId(), getBusinessType(), userId, request);
|
||
}
|
||
|
||
/**
|
||
* 单个文件上传
|
||
*
|
||
* @param file 文件
|
||
* @param userId 上传者userId
|
||
*/
|
||
@Operation(summary = "单个文件上传,不需要groupId和businessType")
|
||
@PostMapping(path = "/file/upload/singleSimple", consumes = "multipart/form-data")
|
||
@Override
|
||
public R<FileDescriptor> uploadSingle(
|
||
@Parameter(description = "文件") @RequestPart("file") MultipartFile file,
|
||
@Parameter(description = "上传者用户id", hidden = true) @RequestParam(value = "userId", required = false) Long userId,
|
||
@Parameter(hidden = true) HttpServletRequest request) throws Exception {
|
||
|
||
if (userId == null) {
|
||
SessionUser sessionUser = checkLogin(request);
|
||
if (sessionUser == null) {
|
||
return R.error(400, "未登录", null);
|
||
}
|
||
userId = sessionUser.getUserId();
|
||
}
|
||
|
||
FileDescriptor fd = new FileDescriptor();
|
||
fd.setBusinessType(getBusinessType());
|
||
fd.setGroupId(getGroupId());
|
||
fd.setUserId(userId);
|
||
fd.setAccessGroup(getService().getAccessGroup());
|
||
fd.setFilePath(generateFilePath(getProperty().getAppCode(), getBusinessType(), userId, getGroupId(), file.getOriginalFilename()));
|
||
fd.setFileName(file.getOriginalFilename());
|
||
fd.setFileLength(file.getSize());
|
||
|
||
getService().upload(fd, file.getInputStream());
|
||
|
||
return R.ok(fd);
|
||
}
|
||
|
||
@Override
|
||
public String getBusinessType() {
|
||
return "common";
|
||
}
|
||
|
||
@Autowired
|
||
private LoginFileService service;
|
||
|
||
@Autowired
|
||
private GunShiCoreProperties properties;
|
||
|
||
@Autowired
|
||
private BaseSessionService sessionService;
|
||
|
||
@Override
|
||
public IFileService getService() {
|
||
return service;
|
||
}
|
||
|
||
@Override
|
||
public SessionUser checkLogin(HttpServletRequest request) {
|
||
String token = sessionService.getToken(request);
|
||
SessionUser sessionUser = null;
|
||
if(token != null){
|
||
sessionUser = sessionService.getSessionUser(token);
|
||
}
|
||
if(sessionUser == null){
|
||
sessionUser = new SessionUser();
|
||
sessionUser.setUserId(1L);
|
||
}
|
||
return sessionUser;
|
||
}
|
||
|
||
@Override
|
||
public GunShiCoreProperties getProperty() {
|
||
return properties;
|
||
}
|
||
}
|