资源增加admin_id

This commit is contained in:
none 2023-03-13 10:36:36 +08:00
parent c254a42cc6
commit debcddc468
12 changed files with 79 additions and 39 deletions

View File

@ -18,7 +18,7 @@ public class PlayEduBContext {
public PlayEduBContext() { public PlayEduBContext() {
} }
public static void put(String key, Object val) { private static void put(String key, Object val) {
LinkedHashMap<String, Object> hashMap = THREAD_LOCAL.get(); LinkedHashMap<String, Object> hashMap = THREAD_LOCAL.get();
if (hashMap == null) { if (hashMap == null) {
hashMap = new LinkedHashMap<>(); hashMap = new LinkedHashMap<>();
@ -27,7 +27,7 @@ public class PlayEduBContext {
THREAD_LOCAL.set(hashMap); THREAD_LOCAL.set(hashMap);
} }
public static Object get(String key) { private static Object get(String key) {
return THREAD_LOCAL.get().getOrDefault(key, null); return THREAD_LOCAL.get().getOrDefault(key, null);
} }

View File

@ -75,4 +75,16 @@ public class BackendBus {
return PrivacyUtil.desValue(value, 1, 0, "*"); return PrivacyUtil.desValue(value, 1, 0, "*");
} }
public boolean isSuperAdmin() {
AdminRole superRole = adminRoleService.getBySlug(BackendConstant.SUPER_ADMIN_ROLE);
if (superRole == null) {
return false;
}
List<Integer> roleIds = adminUserService.getRoleIdsByUserId(PlayEduBContext.getAdminUserID());
if (roleIds.size() == 0) {
return false;
}
return roleIds.contains(superRole.getId());
}
} }

View File

@ -4,6 +4,8 @@ import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBContext;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource; import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.domain.ResourceVideo; import xyz.playedu.api.domain.ResourceVideo;
@ -35,6 +37,9 @@ public class ResourceController {
@Autowired @Autowired
private MinioService minioService; private MinioService minioService;
@Autowired
private BackendBus backendBus;
@GetMapping("/index") @GetMapping("/index")
public JsonResponse index(@RequestParam HashMap<String, Object> params) { public JsonResponse index(@RequestParam HashMap<String, Object> params) {
Integer page = MapUtils.getInteger(params, "page", 1); Integer page = MapUtils.getInteger(params, "page", 1);
@ -54,8 +59,10 @@ public class ResourceController {
filter.setSortField(sortField); filter.setSortField(sortField);
filter.setType(type); filter.setType(type);
filter.setCategoryIds(categoryIds); filter.setCategoryIds(categoryIds);
if (name != null && name.length() > 0) { filter.setName(name);
filter.setName(name); // 非超管只能读取它自己上传的资源
if (!backendBus.isSuperAdmin()) {
filter.setAdminId(PlayEduBContext.getAdminUserID());
} }
PaginationResult<Resource> result = resourceService.paginate(page, size, filter); PaginationResult<Resource> result = resourceService.paginate(page, size, filter);

View File

@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.PlayEduBContext;
import xyz.playedu.api.constant.BackendConstant; import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource; import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.ServiceException; import xyz.playedu.api.exception.ServiceException;
@ -39,7 +40,7 @@ public class UploadController {
@PostMapping("/minio") @PostMapping("/minio")
public JsonResponse uploadMinio(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException { public JsonResponse uploadMinio(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException {
String categoryIds = MapUtils.getString(params, "category_ids"); String categoryIds = MapUtils.getString(params, "category_ids");
Resource res = uploadService.storeMinio(file, categoryIds); Resource res = uploadService.storeMinio(PlayEduBContext.getAdminUserID(), file, categoryIds);
return JsonResponse.data(res); return JsonResponse.data(res);
} }
@ -91,6 +92,7 @@ public class UploadController {
// 视频素材保存 // 视频素材保存
Resource videoResource = resourceService.create( Resource videoResource = resourceService.create(
PlayEduBContext.getAdminUserID(),
req.getCategoryIds(), req.getCategoryIds(),
type, type,
req.getOriginalFilename(), req.getOriginalFilename(),
@ -102,7 +104,7 @@ public class UploadController {
url url
); );
// 视频封面素材保存 // 视频封面素材保存
Resource posterResource = uploadService.storeBase64Image(req.getPoster(), null); Resource posterResource = uploadService.storeBase64Image(PlayEduBContext.getAdminUserID(), req.getPoster(), null);
// 视频的封面素材改为[隐藏 && 属于视频的子素材] // 视频的封面素材改为[隐藏 && 属于视频的子素材]
resourceService.changeParentId(posterResource.getId(), videoResource.getId()); resourceService.changeParentId(posterResource.getId(), videoResource.getId());
// 视频信息 // 视频信息

View File

@ -4,25 +4,32 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
/** /**
*
* @TableName resources * @TableName resources
*/ */
@TableName(value = "resources") @TableName(value ="resources")
@Data @Data
public class Resource implements Serializable { public class Resource implements Serializable {
/** /**
* *
*/ */
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Integer id;
/**
*
*/
@JsonProperty("admin_id")
private Integer adminId;
/** /**
* 类型 * 类型
*/ */
@ -64,16 +71,22 @@ public class Resource implements Serializable {
*/ */
private String url; private String url;
/**
*
*/
@JsonProperty("created_at") @JsonProperty("created_at")
private Date createdAt; private Date createdAt;
/**
* 所属素材
*/
@JsonProperty("parent_id") @JsonProperty("parent_id")
private Integer parentId; private Integer parentId;
/** /**
* 隐藏[0:,1:] * 隐藏[0:,1:]
*/ */
@JsonProperty("is_hidden") @JsonIgnore
private Integer isHidden; private Integer isHidden;
@TableField(exist = false) @TableField(exist = false)
@ -92,17 +105,18 @@ public class Resource implements Serializable {
} }
Resource other = (Resource) that; Resource other = (Resource) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType())) && (this.getAdminId() == null ? other.getAdminId() == null : this.getAdminId().equals(other.getAdminId()))
&& (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName())) && (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType()))
&& (this.getExtension() == null ? other.getExtension() == null : this.getExtension().equals(other.getExtension())) && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
&& (this.getSize() == null ? other.getSize() == null : this.getSize().equals(other.getSize())) && (this.getExtension() == null ? other.getExtension() == null : this.getExtension().equals(other.getExtension()))
&& (this.getDisk() == null ? other.getDisk() == null : this.getDisk().equals(other.getDisk())) && (this.getSize() == null ? other.getSize() == null : this.getSize().equals(other.getSize()))
&& (this.getFileId() == null ? other.getFileId() == null : this.getFileId().equals(other.getFileId())) && (this.getDisk() == null ? other.getDisk() == null : this.getDisk().equals(other.getDisk()))
&& (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath())) && (this.getFileId() == null ? other.getFileId() == null : this.getFileId().equals(other.getFileId()))
&& (this.getUrl() == null ? other.getUrl() == null : this.getUrl().equals(other.getUrl())) && (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath()))
&& (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt())) && (this.getUrl() == null ? other.getUrl() == null : this.getUrl().equals(other.getUrl()))
&& (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId())) && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
&& (this.getIsHidden() == null ? other.getIsHidden() == null : this.getIsHidden().equals(other.getIsHidden())); && (this.getParentId() == null ? other.getParentId() == null : this.getParentId().equals(other.getParentId()))
&& (this.getIsHidden() == null ? other.getIsHidden() == null : this.getIsHidden().equals(other.getIsHidden()));
} }
@Override @Override
@ -110,6 +124,7 @@ public class Resource implements Serializable {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getAdminId() == null) ? 0 : getAdminId().hashCode());
result = prime * result + ((getType() == null) ? 0 : getType().hashCode()); result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
result = prime * result + ((getName() == null) ? 0 : getName().hashCode()); result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
result = prime * result + ((getExtension() == null) ? 0 : getExtension().hashCode()); result = prime * result + ((getExtension() == null) ? 0 : getExtension().hashCode());
@ -131,6 +146,7 @@ public class Resource implements Serializable {
sb.append(" ["); sb.append(" [");
sb.append("Hash = ").append(hashCode()); sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id); sb.append(", id=").append(id);
sb.append(", adminId=").append(adminId);
sb.append(", type=").append(type); sb.append(", type=").append(type);
sb.append(", name=").append(name); sb.append(", name=").append(name);
sb.append(", extension=").append(extension); sb.append(", extension=").append(extension);

View File

@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/** /**
* @author tengteng * @author tengteng
* @description 针对表resources的数据库操作Mapper * @description 针对表resources的数据库操作Mapper
* @createDate 2023-03-08 16:55:59 * @createDate 2023-03-13 10:25:30
* @Entity xyz.playedu.api.domain.Resource * @Entity xyz.playedu.api.domain.Resource
*/ */
@Mapper @Mapper

View File

@ -6,8 +6,6 @@ import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.types.paginate.PaginationResult; import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter; import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
import java.util.List;
/** /**
* @author tengteng * @author tengteng
* @description 针对表resources的数据库操作Service * @description 针对表resources的数据库操作Service
@ -17,7 +15,7 @@ public interface ResourceService extends IService<Resource> {
PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter); PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter);
Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url); Resource create(Integer adminId, String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url);
Resource findOrFail(Integer id) throws NotFoundException; Resource findOrFail(Integer id) throws NotFoundException;

View File

@ -9,7 +9,7 @@ import xyz.playedu.api.exception.ServiceException;
* @create 2023/3/8 14:02 * @create 2023/3/8 14:02
*/ */
public interface UploadService { public interface UploadService {
Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException; Resource storeMinio(Integer adminId, MultipartFile file, String categoryIds) throws ServiceException;
Resource storeBase64Image(String content, String categoryIds) throws ServiceException; Resource storeBase64Image(Integer adminId, String content, String categoryIds) throws ServiceException;
} }

View File

@ -54,6 +54,9 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
if (filter.getType() != null) { if (filter.getType() != null) {
wrapper.eq("type", filter.getType()); wrapper.eq("type", filter.getType());
} }
if (filter.getAdminId() != null && !filter.getAdminId().equals(0)) {
wrapper.eq("admin_id", filter.getAdminId());
}
if (filter.getCategoryIds() != null && filter.getCategoryIds().trim().length() > 0) { if (filter.getCategoryIds() != null && filter.getCategoryIds().trim().length() > 0) {
List<Integer> categoryIds = Arrays.stream(filter.getCategoryIds().split(",")).map(Integer::valueOf).toList(); List<Integer> categoryIds = Arrays.stream(filter.getCategoryIds().split(",")).map(Integer::valueOf).toList();
List<Integer> ids = relationService.getRidsByCids(categoryIds); List<Integer> ids = relationService.getRidsByCids(categoryIds);
@ -89,8 +92,9 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
@Override @Override
@Transactional @Transactional
public Resource create(String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) { public Resource create(Integer adminId, String categoryIds, String type, String filename, String ext, Long size, String disk, String fileId, String path, String url) {
Resource resource = new Resource(); Resource resource = new Resource();
resource.setAdminId(adminId);
resource.setType(type); resource.setType(type);
resource.setName(filename); resource.setName(filename);
resource.setExtension(ext); resource.setExtension(ext);

View File

@ -8,14 +8,11 @@ import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource; import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.ServiceException; import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.service.MinioService; import xyz.playedu.api.service.MinioService;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.service.ResourceService; import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.service.UploadService; import xyz.playedu.api.service.UploadService;
import xyz.playedu.api.util.Base64Util; import xyz.playedu.api.util.Base64Util;
import xyz.playedu.api.util.HelperUtil; import xyz.playedu.api.util.HelperUtil;
import java.util.List;
/** /**
* @Author 杭州白书科技有限公司 * @Author 杭州白书科技有限公司
* @create 2023/3/8 14:02 * @create 2023/3/8 14:02
@ -31,7 +28,7 @@ public class UploadServiceImpl implements UploadService {
private MinioService minioService; private MinioService minioService;
@Override @Override
public Resource storeMinio(MultipartFile file, String categoryIds) throws ServiceException { public Resource storeMinio(Integer adminId, MultipartFile file, String categoryIds) throws ServiceException {
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) { if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
throw new ServiceException("请上传文件"); throw new ServiceException("请上传文件");
} }
@ -53,11 +50,11 @@ public class UploadServiceImpl implements UploadService {
// 保存文件 // 保存文件
String url = minioService.saveFile(file, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); String url = minioService.saveFile(file, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext));
// 上传记录 // 上传记录
return resourceService.create(categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); return resourceService.create(adminId, categoryIds, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
} }
@Override @Override
public Resource storeBase64Image(String content, String categoryIds) throws ServiceException { public Resource storeBase64Image(Integer adminId, String content, String categoryIds) throws ServiceException {
// data:image/jpeg;base64, // data:image/jpeg;base64,
String[] base64Rows = content.split(","); String[] base64Rows = content.split(",");
// 解析出content-type // 解析出content-type
@ -78,6 +75,6 @@ public class UploadServiceImpl implements UploadService {
// 保存文件 // 保存文件
String url = minioService.saveBytes(binary, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext)); String url = minioService.saveBytes(binary, savePath, BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext));
// 上传记录 // 上传记录
return resourceService.create(categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url); return resourceService.create(adminId, categoryIds, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
} }
} }

View File

@ -23,4 +23,6 @@ public class ResourcePaginateFilter {
private String type; private String type;
private Integer adminId;
} }

View File

@ -6,6 +6,7 @@
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.Resource"> <resultMap id="BaseResultMap" type="xyz.playedu.api.domain.Resource">
<id property="id" column="id" jdbcType="INTEGER"/> <id property="id" column="id" jdbcType="INTEGER"/>
<result property="adminId" column="admin_id" jdbcType="INTEGER"/>
<result property="type" column="type" jdbcType="VARCHAR"/> <result property="type" column="type" jdbcType="VARCHAR"/>
<result property="name" column="name" jdbcType="VARCHAR"/> <result property="name" column="name" jdbcType="VARCHAR"/>
<result property="extension" column="extension" jdbcType="VARCHAR"/> <result property="extension" column="extension" jdbcType="VARCHAR"/>
@ -20,9 +21,10 @@
</resultMap> </resultMap>
<sql id="Base_Column_List"> <sql id="Base_Column_List">
id,type,name, id,admin_id,type,
extension,size,disk, name,extension,size,
file_id,path,url, disk,file_id,path,
created_at,parent_id,is_hidden url,created_at,parent_id,
is_hidden
</sql> </sql>
</mapper> </mapper>