parent
046c27d71c
commit
fb028d7091
@ -1,31 +1,31 @@
|
|||||||
package com.ruoyi.file;
|
package com.ruoyi.file;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件服务
|
* 文件服务
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@EnableCustomSwagger2
|
@EnableCustomSwagger2
|
||||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
||||||
public class RuoYFileApplication
|
public class RuoYiFileApplication
|
||||||
{
|
{
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
SpringApplication.run(RuoYFileApplication.class, args);
|
SpringApplication.run(RuoYiFileApplication.class, args);
|
||||||
System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ \n" +
|
System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ \n" +
|
||||||
" .-------. ____ __ \n" +
|
" .-------. ____ __ \n" +
|
||||||
" | _ _ \\ \\ \\ / / \n" +
|
" | _ _ \\ \\ \\ / / \n" +
|
||||||
" | ( ' ) | \\ _. / ' \n" +
|
" | ( ' ) | \\ _. / ' \n" +
|
||||||
" |(_ o _) / _( )_ .' \n" +
|
" |(_ o _) / _( )_ .' \n" +
|
||||||
" | (_,_).' __ ___(_ o _)' \n" +
|
" | (_,_).' __ ___(_ o _)' \n" +
|
||||||
" | |\\ \\ | || |(_,_)' \n" +
|
" | |\\ \\ | || |(_,_)' \n" +
|
||||||
" | | \\ `' /| `-' / \n" +
|
" | | \\ `' /| `-' / \n" +
|
||||||
" | | \\ / \\ / \n" +
|
" | | \\ / \\ / \n" +
|
||||||
" ''-' `'-' `-..-' ");
|
" ''-' `'-' `-..-' ");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,40 +1,73 @@
|
|||||||
package com.ruoyi.file.config;
|
package com.ruoyi.file.config;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
/**
|
|
||||||
* 通用映射配置
|
/**
|
||||||
*
|
* 本地文件配置
|
||||||
* @author ruoyi
|
* 通用映射配置
|
||||||
*/
|
*
|
||||||
@RefreshScope
|
* @author ruoyi
|
||||||
@Configuration
|
*/
|
||||||
public class ResourcesConfig implements WebMvcConfigurer
|
@RefreshScope
|
||||||
{
|
@Configuration
|
||||||
/**
|
public class LocalConfig implements WebMvcConfigurer
|
||||||
* 上传文件存储在本地的根路径
|
{
|
||||||
* eg: D:/ruoyi/uploadPath
|
/**
|
||||||
*/
|
* 资源映射路径 前缀
|
||||||
@Value("${file.path}")
|
* eg: eg: /statics
|
||||||
private String localFilePath;
|
*/
|
||||||
|
@Value("${file.prefix}")
|
||||||
/**
|
private String localFilePrefix;
|
||||||
* 资源映射路径 前缀
|
|
||||||
* eg: /statics
|
/**
|
||||||
*/
|
* 域名或本机访问地址
|
||||||
@Value("${file.prefix}")
|
* eg: http://127.0.0.1:9300
|
||||||
public String localFilePrefix;
|
*/
|
||||||
|
@Value("${file.domain}")
|
||||||
@Override
|
private String domain;
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry)
|
|
||||||
{
|
/**
|
||||||
/** 本地文件上传路径 */
|
* 上传文件存储在本地的根路径
|
||||||
registry.addResourceHandler(localFilePrefix + "/**")
|
* eg: D:/ruoyi/uploadPath
|
||||||
.addResourceLocations("file:" + localFilePath + File.separator);
|
*/
|
||||||
}
|
@Value("${file.path}")
|
||||||
|
private String localFilePath;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry)
|
||||||
|
{
|
||||||
|
/** 本地文件上传路径 */
|
||||||
|
registry.addResourceHandler(localFilePrefix + "/**")
|
||||||
|
.addResourceLocations("file:" + localFilePath + File.separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocalFilePrefix() {
|
||||||
|
return localFilePrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalFilePrefix(String localFilePrefix) {
|
||||||
|
this.localFilePrefix = localFilePrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocalFilePath() {
|
||||||
|
return localFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLocalFilePath(String localFilePath) {
|
||||||
|
this.localFilePath = localFilePath;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,69 +1,58 @@
|
|||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import com.ruoyi.common.core.exception.CustomException;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import com.ruoyi.file.config.LocalConfig;
|
||||||
import org.springframework.context.annotation.Primary;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.context.annotation.Primary;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.stereotype.Service;
|
||||||
import com.ruoyi.file.utils.FileUploadUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import com.ruoyi.file.utils.FileUploadUtils;
|
||||||
/**
|
|
||||||
* 本地文件存储
|
/**
|
||||||
*
|
* 本地文件存储
|
||||||
* @author ruoyi
|
*
|
||||||
*/
|
* @author ruoyi
|
||||||
@Primary
|
*/
|
||||||
@Service
|
@Primary
|
||||||
public class LocalSysFileServiceImpl implements ISysFileService
|
@Service
|
||||||
{
|
public class LocalDfsServiceImpl implements IDfsService
|
||||||
/**
|
{
|
||||||
* 资源映射路径 前缀
|
private final LocalConfig localConfig;
|
||||||
* eg: eg: /statics
|
|
||||||
*/
|
public LocalDfsServiceImpl(LocalConfig localConfig) {
|
||||||
@Value("${file.prefix}")
|
this.localConfig = localConfig;
|
||||||
public String localFilePrefix;
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 域名或本机访问地址
|
* 本地文件上传接口
|
||||||
* eg: http://127.0.0.1:9300
|
*
|
||||||
*/
|
* @param file 上传的文件
|
||||||
@Value("${file.domain}")
|
* @return 访问地址
|
||||||
public String domain;
|
* @throws Exception
|
||||||
|
*/
|
||||||
/**
|
@Override
|
||||||
* 上传文件存储在本地的根路径
|
public String uploadFile(MultipartFile file) throws Exception
|
||||||
* eg: D:/ruoyi/uploadPath
|
{
|
||||||
*/
|
return this.uploadFile(file, null);
|
||||||
@Value("${file.path}")
|
}
|
||||||
private String localFilePath;
|
|
||||||
|
@Override
|
||||||
/**
|
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
||||||
* 本地文件上传接口
|
String localFilePath = localConfig.getLocalFilePath();
|
||||||
*
|
String domain = localConfig.getDomain();
|
||||||
* @param file 上传的文件
|
String localFilePrefix = localConfig.getLocalFilePrefix();
|
||||||
* @return 访问地址
|
|
||||||
* @throws Exception
|
String name = FileUploadUtils.upload(localFilePath + "/" + StringUtils.defaultString(modules, ""), file);
|
||||||
*/
|
return domain + localFilePrefix + name;
|
||||||
@Override
|
}
|
||||||
public String uploadFile(MultipartFile file) throws Exception
|
|
||||||
{
|
@Override
|
||||||
return this.uploadFile(file, null);
|
public boolean deleteFile(String fileUrl) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
@Override
|
||||||
String name = FileUploadUtils.upload(localFilePath + "/" + StringUtils.defaultString(modules, ""), file);
|
public String objectsCapacityStr() {
|
||||||
String url = domain + localFilePrefix + name;
|
throw new CustomException("本地存储-获取文件占用空间功能,敬请期待");
|
||||||
return url;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean deleteFile(String fileUrl) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String listObject() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,90 +1,90 @@
|
|||||||
package com.ruoyi.file.service;
|
package com.ruoyi.file.service;
|
||||||
|
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import io.minio.RemoveObjectArgs;
|
import com.ruoyi.common.core.exception.CustomException;
|
||||||
import io.minio.errors.*;
|
import io.minio.RemoveObjectArgs;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import io.minio.errors.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.stereotype.Service;
|
||||||
import com.ruoyi.file.config.MinioConfig;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import com.ruoyi.file.utils.FileUploadUtils;
|
import com.ruoyi.file.config.MinioConfig;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
import io.minio.PutObjectArgs;
|
import io.minio.PutObjectArgs;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minio 文件存储
|
* Minio 文件存储
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class MinioSysFileServiceImpl implements ISysFileService
|
public class MinioDfsServiceImpl implements IDfsService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioConfig minioConfig;
|
private MinioConfig minioConfig;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioClient client;
|
private MinioClient client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地文件上传接口
|
* 本地文件上传接口
|
||||||
*
|
*
|
||||||
* @param file 上传的文件
|
* @param file 上传的文件
|
||||||
* @return 访问地址
|
* @return 访问地址
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile file) throws Exception
|
public String uploadFile(MultipartFile file) throws Exception
|
||||||
{
|
{
|
||||||
return this.uploadFile(file, null);
|
return this.uploadFile(file, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
public String uploadFile(MultipartFile file, String modules) throws Exception {
|
||||||
String fileName = validateModule(file ,modules);
|
String fileName = validateModule(file ,modules);
|
||||||
boolean isProd = "prod".equalsIgnoreCase(SpringUtil.getActiveProfile());
|
boolean isProd = "prod".equalsIgnoreCase(SpringUtil.getActiveProfile());
|
||||||
if (!isProd) {
|
if (!isProd) {
|
||||||
fileName = SpringUtil.getActiveProfile() + "/" + fileName;
|
fileName = SpringUtil.getActiveProfile() + "/" + fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
PutObjectArgs args = PutObjectArgs.builder()
|
PutObjectArgs args = PutObjectArgs.builder()
|
||||||
.bucket(minioConfig.getBucketName())
|
.bucket(minioConfig.getBucketName())
|
||||||
.object(fileName)
|
.object(fileName)
|
||||||
.stream(file.getInputStream(), file.getSize(), -1)
|
.stream(file.getInputStream(), file.getSize(), -1)
|
||||||
.contentType(file.getContentType())
|
.contentType(file.getContentType())
|
||||||
.build();
|
.build();
|
||||||
client.putObject(args);
|
client.putObject(args);
|
||||||
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteFile(String fileUrl) {
|
public boolean deleteFile(String fileUrl) {
|
||||||
RemoveObjectArgs args = RemoveObjectArgs.builder().
|
RemoveObjectArgs args = RemoveObjectArgs.builder().
|
||||||
bucket(minioConfig.getBucketName()).
|
bucket(minioConfig.getBucketName()).
|
||||||
object(fileUrl).
|
object(fileUrl).
|
||||||
build();
|
build();
|
||||||
try {
|
try {
|
||||||
client.removeObject(args);
|
client.removeObject(args);
|
||||||
return true;
|
return true;
|
||||||
} catch (ErrorResponseException |
|
} catch (ErrorResponseException |
|
||||||
InsufficientDataException |
|
InsufficientDataException |
|
||||||
InternalException |
|
InternalException |
|
||||||
InvalidKeyException |
|
InvalidKeyException |
|
||||||
InvalidResponseException |
|
InvalidResponseException |
|
||||||
IOException |
|
IOException |
|
||||||
NoSuchAlgorithmException |
|
NoSuchAlgorithmException |
|
||||||
ServerException |
|
ServerException |
|
||||||
XmlParserException e) {
|
XmlParserException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String listObject() {
|
public String objectsCapacityStr() {
|
||||||
return null;
|
throw new CustomException("minio存储-获取文件占用空间功能,敬请期待");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in new issue