完善minio上传

This commit is contained in:
none
2023-03-08 15:56:44 +08:00
parent 1486e518cc
commit e8455a3a58
16 changed files with 321 additions and 67 deletions

View File

@@ -12,6 +12,8 @@ public interface MinioService {
String saveFile(MultipartFile file, String savePath, String contentType);
String saveBytes(byte[] file, String savePath, String contentType);
String uploadId(String path);
String chunkPreSignUrl(String filename, String partNumber, String uploadId);

View File

@@ -19,6 +19,8 @@ public interface ResourceCategoryService extends IService<ResourceCategory> {
ResourceCategory findOrFail(Integer id) throws NotFoundException;
ResourceCategory find(Integer id, String type);
void update(ResourceCategory category, Integer sort, String name);
}

View File

@@ -19,4 +19,8 @@ public interface ResourceService extends IService<Resource> {
Resource findOrFail(Integer id) throws NotFoundException;
void changeParentId(Integer id, Integer parentId);
void storeResourceVideo(Integer rid, Integer duration, String poster);
}

View File

@@ -0,0 +1,15 @@
package xyz.playedu.api.service;
import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.ServiceException;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/8 14:02
*/
public interface UploadService {
Resource storeMinio(MultipartFile file, Integer categoryId) throws ServiceException;
Resource storeBase64Image(String content,Integer categoryId) throws ServiceException;
}

View File

@@ -13,6 +13,8 @@ import xyz.playedu.api.config.MinioConfig;
import xyz.playedu.api.service.MinioService;
import xyz.playedu.api.vendor.PlayEduMinioClient;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@@ -34,7 +36,7 @@ public class MinioServiceImpl implements MinioService {
@Override
public String url(String path) {
return c.getDomain() + c.getBucket() + path;
return c.getDomain() + c.getBucket() + "/" + path;
}
@Override
@@ -85,4 +87,21 @@ public class MinioServiceImpl implements MinioService {
public void removeByPath(String path) {
client.removeObject(RemoveObjectArgs.builder().bucket(c.getBucket()).object(path).build());
}
@Override
@SneakyThrows
public String saveBytes(byte[] file, String savePath, String contentType) {
InputStream inputStream = new ByteArrayInputStream(file);
PutObjectArgs objectArgs = PutObjectArgs.builder()
.bucket(c.getBucket())
.object(savePath)
.stream(inputStream, file.length, -1)
.contentType(contentType)
.build();
client.putObject(objectArgs);
return url(savePath);
}
}

View File

@@ -53,6 +53,11 @@ public class ResourceCategoryServiceImpl extends ServiceImpl<ResourceCategoryMap
newCategory.setName(name);
updateById(newCategory);
}
@Override
public ResourceCategory find(Integer id, String type) {
return getOne(query().getWrapper().eq("id", id).eq("type", type));
}
}

View File

@@ -4,11 +4,13 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.mapper.ResourceMapper;
import org.springframework.stereotype.Service;
import xyz.playedu.api.service.ResourceVideoService;
import xyz.playedu.api.types.paginate.PaginationResult;
import xyz.playedu.api.types.paginate.ResourcePaginateFilter;
@@ -24,9 +26,12 @@ import java.util.Date;
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {
@Autowired
private ResourceVideoService resourceVideoService;
@Override
public PaginationResult<Resource> paginate(int page, int size, ResourcePaginateFilter filter) {
QueryWrapper<Resource> wrapper = query().getWrapper().eq("1", "1");
QueryWrapper<Resource> wrapper = query().getWrapper().eq("is_hidden", 0);
if (filter.getName() != null) {
wrapper.like("name", "%" + filter.getName() + "%");
@@ -93,6 +98,20 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
}
return resource;
}
@Override
public void changeParentId(Integer id, Integer parentId) {
Resource resource = new Resource();
resource.setId(id);
resource.setParentId(parentId);
resource.setIsHidden(1);
updateById(resource);
}
@Override
public void storeResourceVideo(Integer rid, Integer duration, String poster) {
resourceVideoService.create(rid, duration, poster);
}
}

View File

@@ -0,0 +1,91 @@
package xyz.playedu.api.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.service.MinioService;
import xyz.playedu.api.service.ResourceCategoryService;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.service.UploadService;
import xyz.playedu.api.util.Base64Util;
import xyz.playedu.api.util.HelperUtil;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/8 14:02
*/
@Service
public class UploadServiceImpl implements UploadService {
@Autowired
private ResourceCategoryService resourceCategoryService;
@Autowired
private ResourceService resourceService;
@Autowired
private MinioService minioService;
@Override
public Resource storeMinio(MultipartFile file, Integer cid) throws ServiceException {
if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
throw new ServiceException("请上传文件");
}
// 文件后缀名校验
String filename = file.getOriginalFilename();
String ext = HelperUtil.fileExt(filename);
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(ext);
if (type == null) {
throw new ServiceException("格式不支持");
}
// content-type校验
String contentType = file.getContentType();
String safeContentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext);
if (safeContentType == null || !safeContentType.equals(contentType)) {
throw new ServiceException("格式不支持");
}
// 分类校验
if (cid != null && !cid.equals(0) && resourceCategoryService.find(cid, type) == null) {
throw new ServiceException("分类不存在");
}
// 上传原文件的文件名
String oFilename = filename.replaceAll("." + ext, "");
// 自定义新的存储文件名
String newFilename = HelperUtil.randomString(32) + "." + ext;
String savePath = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + newFilename;
// 保存文件
String url = minioService.saveFile(file, savePath, contentType);
// 上传记录
return resourceService.create(cid, type, oFilename, ext, file.getSize(), BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
}
@Override
public Resource storeBase64Image(String content, Integer categoryId) throws ServiceException {
// data:image/jpeg;base64,
String[] base64Rows = content.split(",");
String contentType = base64Rows[0].replaceAll("data:", "").replaceAll(";base64", "").toLowerCase();
String ext = contentType.replaceAll("image/", "");
String type = BackendConstant.RESOURCE_EXT_2_TYPE.get(ext);
String safeContentType = BackendConstant.RESOURCE_EXT_2_CONTENT_TYPE.get(ext);
if (type == null || safeContentType == null || !safeContentType.equals(contentType)) {
throw new ServiceException("格式不支持");
}
byte[] binary = Base64Util.decode(base64Rows[1]);
String filename = HelperUtil.randomString(32) + "." + ext;
String savePath = BackendConstant.RESOURCE_TYPE_2_DIR.get(type) + filename;
// 保存文件
String url = minioService.saveBytes(binary, savePath, contentType);
// 上传记录
return resourceService.create(categoryId, type, filename, ext, (long) binary.length, BackendConstant.STORAGE_DRIVER_MINIO, "", savePath, url);
}
}