package com.cloudroam.controller.sys;
|
|
import cn.hutool.core.util.StrUtil;
|
import com.cloudroam.dto.file.FileInfo;
|
import com.cloudroam.manager.SysFileManager;
|
import com.cloudroam.model.SysFileDO;
|
import com.cloudroam.util.MinioUtil;
|
import io.github.talelin.autoconfigure.exception.ForbiddenException;
|
import io.github.talelin.core.annotation.LoginRequired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
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.RestController;
|
|
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.InputStream;
|
import java.net.URLEncoder;
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* 文件操作接口
|
*/
|
@RestController
|
@RequestMapping(value = "/sys/file")
|
public class FilesController {
|
|
@Autowired
|
MinioUtil minioUtil;
|
|
@Autowired
|
private SysFileManager sysFileManager;
|
|
|
/**
|
* 上传单文件
|
* @param uploadfile
|
* @param bucket
|
* @param objectName
|
* @throws Exception
|
*/
|
// @LoginRequired
|
@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
|
public SysFileDO fileupload(@RequestParam(value = "file",required = true) MultipartFile uploadfile, @RequestParam(required = true) String bucket,
|
@RequestParam(required = true) String objectName) throws Exception {
|
|
if(Objects.isNull(uploadfile)){
|
throw new ForbiddenException(10400);
|
}
|
if(StrUtil.isBlank(bucket)){
|
throw new ForbiddenException(10400);
|
}
|
if(StrUtil.isBlank(objectName)){
|
throw new ForbiddenException(10401);
|
}
|
|
SysFileDO sysFileDO = sysFileManager.uploadFile(uploadfile, bucket, objectName);
|
return sysFileDO;
|
}
|
|
|
/**
|
* 列出所有桶
|
* @return
|
* @throws Exception
|
*/
|
@LoginRequired
|
@RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
|
public List<String> listBuckets() throws Exception {
|
List<String> fileList= minioUtil.listBuckets();
|
|
return fileList;
|
// return ResponseUtils.success(minioUtil.listBuckets());
|
}
|
|
/**
|
* 递归列出一个桶中的所有文件和目录
|
* @param bucket
|
* @return
|
* @throws Exception
|
*/
|
@LoginRequired
|
@RequestMapping(value = "/listFiles", method = RequestMethod.GET)
|
public List<FileInfo> listFiles(@RequestParam String bucket) throws Exception {
|
List<FileInfo> fileInfos = minioUtil.listFiles(bucket);
|
|
return fileInfos;
|
// return ResponseUtils.success(fileInfos);
|
}
|
|
/**
|
* 下载一个文件
|
* @param bucket
|
* @param objectName
|
* @param response
|
* @throws Exception
|
*/
|
// @LoginRequired
|
@RequestMapping(value = "/downloadFileWithBucket", method = RequestMethod.GET)
|
public void downloadFileWithBucket(@RequestParam String bucket, @RequestParam String objectName,
|
HttpServletResponse response) throws Exception {
|
InputStream stream = minioUtil.download(bucket, objectName);
|
ServletOutputStream output = response.getOutputStream();
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
|
response.setContentType("application/octet-stream");
|
response.setCharacterEncoding("UTF-8");
|
IOUtils.copy(stream, output);
|
}
|
|
@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
|
public void downloadFile2(@RequestParam String fullpath,
|
HttpServletResponse response) throws Exception {
|
if(Objects.isNull(fullpath)){
|
throw new ForbiddenException(10403);
|
}
|
SysFileDO existSysFileDO = sysFileManager.getFileByFullpath(fullpath);
|
if(Objects.isNull(existSysFileDO)){
|
throw new ForbiddenException(10404);
|
};
|
|
sysFileManager.downloadFileWithFullpath(fullpath,response);
|
|
}
|
|
|
/**
|
* 删除一个文件
|
* @param bucket
|
* @param objectName
|
* @throws Exception
|
*/
|
@LoginRequired
|
@RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
|
public void deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
|
minioUtil.deleteObject(bucket, objectName);
|
// return ResponseUtils.success();
|
}
|
|
|
/**
|
* 删除一个桶
|
* @param bucket
|
* @throws Exception
|
*/
|
@LoginRequired
|
@RequestMapping(value = "/deleteBucket", method = RequestMethod.GET)
|
public void deleteBucket(@RequestParam String bucket) throws Exception {
|
minioUtil.deleteBucket(bucket);
|
// return ResponseUtils.success();
|
}
|
|
|
/**
|
* 复制一个文件
|
* @param sourceBucket
|
* @param sourceObject
|
* @param targetBucket
|
* @param targetObject
|
* @throws Exception
|
*/
|
@LoginRequired
|
@GetMapping("/copyObject")
|
public void copyObject(@RequestParam String sourceBucket, @RequestParam String sourceObject, @RequestParam String targetBucket, @RequestParam String targetObject) throws Exception {
|
minioUtil.copyObject(sourceBucket, sourceObject, targetBucket, targetObject);
|
// return ResponseUtils.success();
|
}
|
|
/**
|
* 获取文件信息
|
* @param bucket
|
* @param objectName
|
* @return
|
* @throws Exception
|
*/
|
@LoginRequired
|
@GetMapping("/getObjectInfo")
|
public String getObjectInfo(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
|
String objectInfo = minioUtil.getObjectInfo(bucket, objectName);
|
return objectInfo;
|
// return ResponseUtils.success(minioUtil.getObjectInfo(bucket, objectName));
|
}
|
|
/**
|
* 获取一个连接以供下载
|
* @param bucket
|
* @param objectName
|
* @param expires
|
* @return
|
* @throws Exception
|
*/
|
@LoginRequired
|
@GetMapping("/getPresignedObjectUrl")
|
public String getPresignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
|
String presignedObjectUrl = minioUtil.getPresignedObjectUrl(bucket, objectName, expires);
|
// return ResponseUtils.success(minioUtil.getPresignedObjectUrl(bucket, objectName, expires));
|
return presignedObjectUrl;
|
}
|
|
/**
|
* 获取minio中所有的文件
|
* @return
|
* @throws Exception
|
*/
|
@LoginRequired
|
@GetMapping("/listAllFile")
|
public List<FileInfo> listAllFile() throws Exception {
|
List<FileInfo> fileInfos = minioUtil.listAllFile();
|
return fileInfos;
|
|
// return ResponseUtils.success(fileInfos);
|
}
|
|
|
}
|