feat: 下载文件
This commit is contained in:
parent
3fa7ed2043
commit
0afaf12a81
|
@ -97,16 +97,14 @@ public class MinioUtil {
|
|||
* @return 文件流对象
|
||||
*/
|
||||
public byte[] getBucketObjectByte(String objectName) {
|
||||
// 如果buckName为空,设置为默认的桶
|
||||
String bucketName = properties.getBucketName();
|
||||
|
||||
try {
|
||||
objectName = objectName.replace("/" + bucketName, "");
|
||||
GetObjectResponse getObjectResponse = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
|
||||
|
||||
return getObjectResponse.readAllBytes();
|
||||
} catch (Exception exception) {
|
||||
exception.getStackTrace();
|
||||
exception.printStackTrace();
|
||||
}
|
||||
throw new BunnyException(ResultCodeEnum.GET_BUCKET_EXCEPTION);
|
||||
}
|
||||
|
|
|
@ -29,12 +29,9 @@ public enum ResultCodeEnum {
|
|||
GET_BUCKET_EXCEPTION(201, "获取文件信息失败"),
|
||||
SEND_MAIL_CODE_ERROR(201, "邮件发送失败"),
|
||||
EMAIL_CODE_EMPTY(201, "邮箱验证码过期或不存在"),
|
||||
DATA_EXIST(201, "数据已存在"),
|
||||
DATA_NOT_EXIST(201, "数据不存在"),
|
||||
EMAIL_EXIST(201, "邮箱已存在"),
|
||||
REQUEST_IS_EMPTY(201, "请求数据为空"),
|
||||
DATA_TOO_LARGE(201, "请求数据为空"),
|
||||
USER_IS_EMPTY(201, "用户不存在"),
|
||||
UPDATE_NEW_PASSWORD_SAME_AS_OLD_PASSWORD(201, "新密码与密码相同"),
|
||||
|
||||
// 数据相关 206
|
||||
|
@ -43,6 +40,10 @@ public enum ResultCodeEnum {
|
|||
DATA_ERROR(206, "数据异常"),
|
||||
EMAIL_USER_TEMPLATE_IS_EMPTY(206, "邮件模板为空"),
|
||||
EMAIL_TEMPLATE_IS_EMPTY(206, "邮件模板为空"),
|
||||
DATA_EXIST(206, "数据已存在"),
|
||||
DATA_NOT_EXIST(206, "数据不存在"),
|
||||
USER_IS_EMPTY(206, "用户不存在"),
|
||||
FILE_NOT_EXIST(206, "文件不存在"),
|
||||
|
||||
// 身份过期 208
|
||||
LOGIN_AUTH(208, "请先登陆"),
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -51,11 +52,10 @@ public class FilesController {
|
|||
return Mono.just(Result.success(pageResult));
|
||||
}
|
||||
|
||||
@Operation(summary = "添加系统文件表", description = "添加系统文件表")
|
||||
@PostMapping("addFiles")
|
||||
public Mono<Result<String>> addFiles(@Valid @RequestBody FilesAddDto dto) {
|
||||
filesService.addFiles(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
|
||||
@Operation(summary = "下载文件", description = "下载文件")
|
||||
@GetMapping("downloadFiles/{fileId}")
|
||||
public void downloadFiles(@PathVariable Long fileId, HttpServletResponse response) {
|
||||
filesService.downloadFiles(response, fileId);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新系统文件表", description = "更新系统文件表")
|
||||
|
@ -65,6 +65,13 @@ public class FilesController {
|
|||
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "添加系统文件表", description = "添加系统文件表")
|
||||
@PostMapping("addFiles")
|
||||
public Mono<Result<String>> addFiles(@Valid @RequestBody FilesAddDto dto) {
|
||||
filesService.addFiles(dto);
|
||||
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS));
|
||||
}
|
||||
|
||||
@Operation(summary = "上传文件", description = "上传文件")
|
||||
@PostMapping("upload")
|
||||
public Result<FileInfoVo> upload(FileUploadDto dto) {
|
||||
|
|
|
@ -10,6 +10,7 @@ import cn.bunny.dao.vo.system.files.FileInfoVo;
|
|||
import cn.bunny.dao.vo.system.files.FilesVo;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -59,4 +60,12 @@ public interface FilesService extends IService<Files> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deleteFiles(List<Long> ids);
|
||||
|
||||
/**
|
||||
* * 下载文件
|
||||
*
|
||||
* @param fileId 文件名
|
||||
* @param response response
|
||||
*/
|
||||
void downloadFiles(HttpServletResponse response, Long fileId);
|
||||
}
|
||||
|
|
|
@ -112,6 +112,9 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
|||
*/
|
||||
@Override
|
||||
public void deleteDept(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
// 删除当前部门
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
|
||||
|
|
|
@ -98,6 +98,9 @@ public class EmailTemplateServiceImpl extends ServiceImpl<EmailTemplateMapper, E
|
|||
*/
|
||||
@Override
|
||||
public void deleteEmailTemplate(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
|
|
|
@ -110,6 +110,9 @@ public class EmailUsersServiceImpl extends ServiceImpl<EmailUsersMapper, EmailUs
|
|||
*/
|
||||
@Override
|
||||
public void deleteEmailUsers(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,32 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.common.service.exception.BunnyException;
|
||||
import cn.bunny.common.service.utils.minio.MinioUtil;
|
||||
import cn.bunny.dao.dto.system.files.FileUploadDto;
|
||||
import cn.bunny.dao.dto.system.files.FilesAddDto;
|
||||
import cn.bunny.dao.dto.system.files.FilesDto;
|
||||
import cn.bunny.dao.dto.system.files.FilesUpdateDto;
|
||||
import cn.bunny.dao.entity.system.Files;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
import cn.bunny.dao.vo.system.files.FileInfoVo;
|
||||
import cn.bunny.dao.vo.system.files.FilesVo;
|
||||
import cn.bunny.services.factory.FileFactory;
|
||||
import cn.bunny.services.mapper.FilesMapper;
|
||||
import cn.bunny.services.service.FilesService;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -34,8 +41,12 @@ import java.util.List;
|
|||
public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements FilesService {
|
||||
|
||||
private final FileFactory fileFactory;
|
||||
private final MinioUtil minioUtil;
|
||||
|
||||
public FilesServiceImpl(FileFactory fileFactory) {this.fileFactory = fileFactory;}
|
||||
public FilesServiceImpl(FileFactory fileFactory, MinioUtil minioUtil) {
|
||||
this.fileFactory = fileFactory;
|
||||
this.minioUtil = minioUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 系统文件表 服务实现类
|
||||
|
@ -111,6 +122,40 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
|
|||
*/
|
||||
@Override
|
||||
public void deleteFiles(List<Long> ids) {
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 下载文件
|
||||
*
|
||||
* @param response response
|
||||
* @param fileId 文件名
|
||||
*/
|
||||
@Override
|
||||
public void downloadFiles(HttpServletResponse response, Long fileId) {
|
||||
// 查询数据库文件信息
|
||||
Files files = getOne(Wrappers.<Files>lambdaQuery().eq(Files::getId, fileId));
|
||||
|
||||
// 判断文件是否存在
|
||||
if (files == null) throw new BunnyException(ResultCodeEnum.FILE_NOT_EXIST);
|
||||
|
||||
// 从Minio获取文件
|
||||
String filepath = files.getFilepath();
|
||||
int end = filepath.indexOf("/", 1);
|
||||
filepath = filepath.substring(end + 1);
|
||||
byte[] buffer = minioUtil.getBucketObjectByte(filepath);
|
||||
|
||||
// 设置响应头
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + files.getFilename() + "\"");
|
||||
|
||||
// 写入字节数组到输出流
|
||||
try (OutputStream os = response.getOutputStream()) {
|
||||
os.write(buffer);
|
||||
os.flush();
|
||||
} catch (IOException exception) {
|
||||
throw new BunnyException(exception.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,9 +134,9 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
|
|||
*/
|
||||
@Override
|
||||
public void deleteI18n(List<Long> ids) {
|
||||
if (ids.isEmpty()) {
|
||||
throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
}
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,6 +103,9 @@ public class I18nTypeServiceImpl extends ServiceImpl<I18nTypeMapper, I18nType> i
|
|||
*/
|
||||
@Override
|
||||
public void deleteI18nType(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.common.service.exception.BunnyException;
|
||||
import cn.bunny.dao.dto.system.menuIcon.MenuIconAddDto;
|
||||
import cn.bunny.dao.dto.system.menuIcon.MenuIconDto;
|
||||
import cn.bunny.dao.dto.system.menuIcon.MenuIconUpdateDto;
|
||||
import cn.bunny.dao.entity.system.MenuIcon;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
import cn.bunny.dao.vo.system.MenuIconVo;
|
||||
import cn.bunny.services.mapper.MenuIconMapper;
|
||||
import cn.bunny.services.service.MenuIconService;
|
||||
|
@ -90,6 +92,9 @@ public class MenuIconServiceImpl extends ServiceImpl<MenuIconMapper, MenuIcon> i
|
|||
*/
|
||||
@Override
|
||||
public void deleteMenuIcon(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
|
|
|
@ -114,6 +114,9 @@ public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements
|
|||
*/
|
||||
@Override
|
||||
public void deletePower(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
// 删除权限
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
|
||||
|
|
|
@ -112,6 +112,9 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
|||
*/
|
||||
@Override
|
||||
public void deleteRole(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
// 删除角色
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
|
||||
|
|
|
@ -212,6 +212,9 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
|
|||
*/
|
||||
@Override
|
||||
public void deletedMenuByIds(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
// 查找子级菜单,一起删除
|
||||
List<Long> longList = list(Wrappers.<Router>lambdaQuery().in(Router::getParentId, ids)).stream().map(Router::getId).toList();
|
||||
ids.addAll(longList);
|
||||
|
|
|
@ -359,6 +359,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
|||
*/
|
||||
@Override
|
||||
public void deleteAdminUser(List<Long> ids) {
|
||||
// 判断数据请求是否为空
|
||||
if (ids.isEmpty()) throw new BunnyException(ResultCodeEnum.REQUEST_IS_EMPTY);
|
||||
|
||||
// 删除用户
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FilesServiceImplTest {
|
||||
@Test
|
||||
void stringTest() {
|
||||
String filepath = "/auth-admin/avatar/2024/10-04/5a56ad8f-4468-4780-8a61-424e7de54e04.png";
|
||||
int end = filepath.indexOf("/", 1);
|
||||
|
||||
filepath = filepath.substring(end + 1);
|
||||
System.out.println(filepath);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue