From 3d49f6126fff9511b98dac095ce78b659dcf5831 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=86=AF=E5=87=AF?= <371894675@qq.com>
Date: Sun, 19 Nov 2023 09:26:09 +0800
Subject: [PATCH] init file
---
.gitignore | 46 +++++
dragon-file-common/pom.xml | 26 +++
.../dragon/file/common/domain/SysFile.java | 45 +++++
dragon-file-remote/pom.xml | 26 +++
.../dragon/file/remote/RemoteFileService.java | 30 ++++
.../factory/RemoteFileFallbackFactory.java | 34 ++++
...ot.autoconfigure.AutoConfiguration.imports | 2 +
dragon-file-server/pom.xml | 92 ++++++++++
.../file/server/DragonFileApplication.java | 19 +++
.../file/server/config/MinioConfig.java | 72 ++++++++
.../file/server/config/ResourcesConfig.java | 48 ++++++
.../server/controller/SysFileController.java | 43 +++++
.../service/FastDfsSysFileServiceImpl.java | 45 +++++
.../file/server/service/ISysFileService.java | 19 +++
.../service/LocalSysFileServiceImpl.java | 48 ++++++
.../service/MinioSysFileServiceImpl.java | 48 ++++++
.../file/server/utils/FileUploadUtils.java | 157 ++++++++++++++++++
.../src/main/resources/banner.txt | 2 +
.../src/main/resources/bootstrap.yml | 25 +++
.../src/main/resources/logback.xml | 74 +++++++++
pom.xml | 45 +++++
21 files changed, 946 insertions(+)
create mode 100644 .gitignore
create mode 100644 dragon-file-common/pom.xml
create mode 100644 dragon-file-common/src/main/java/com/dragon/file/common/domain/SysFile.java
create mode 100644 dragon-file-remote/pom.xml
create mode 100644 dragon-file-remote/src/main/java/com/dragon/file/remote/RemoteFileService.java
create mode 100644 dragon-file-remote/src/main/java/com/dragon/file/remote/factory/RemoteFileFallbackFactory.java
create mode 100644 dragon-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
create mode 100644 dragon-file-server/pom.xml
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/DragonFileApplication.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/config/MinioConfig.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/config/ResourcesConfig.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/controller/SysFileController.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/service/FastDfsSysFileServiceImpl.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/service/ISysFileService.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/service/LocalSysFileServiceImpl.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/service/MinioSysFileServiceImpl.java
create mode 100644 dragon-file-server/src/main/java/com/dragon/file/server/utils/FileUploadUtils.java
create mode 100644 dragon-file-server/src/main/resources/banner.txt
create mode 100644 dragon-file-server/src/main/resources/bootstrap.yml
create mode 100644 dragon-file-server/src/main/resources/logback.xml
create mode 100644 pom.xml
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..09bdfea
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,46 @@
+######################################################################
+# Build Tools
+
+.gradle
+/build/
+!gradle/wrapper/gradle-wrapper.jar
+
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+######################################################################
+# IDE
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### JRebel ###
+rebel.xml
+### NetBeans ###
+nbproject/private/
+build/*
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
+
+######################################################################
+# Others
+*.log
+*.xml.versionsBackup
+*.swp
+
+!*/build/*.java
+!*/build/*.html
+!*/build/*.xml
\ No newline at end of file
diff --git a/dragon-file-common/pom.xml b/dragon-file-common/pom.xml
new file mode 100644
index 0000000..d5e7fff
--- /dev/null
+++ b/dragon-file-common/pom.xml
@@ -0,0 +1,26 @@
+
+
+ 4.0.0
+
+ com.dragon
+ dragon-modules-file
+ 3.6.3
+
+ 3.6.3
+ dragon-file-common
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+ com.dragon
+ dragon-common-core
+
+
+
diff --git a/dragon-file-common/src/main/java/com/dragon/file/common/domain/SysFile.java b/dragon-file-common/src/main/java/com/dragon/file/common/domain/SysFile.java
new file mode 100644
index 0000000..e422984
--- /dev/null
+++ b/dragon-file-common/src/main/java/com/dragon/file/common/domain/SysFile.java
@@ -0,0 +1,45 @@
+package com.dragon.file.common.domain;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+/**
+ * 文件信息
+ *
+ * @author dragon
+ */
+public class SysFile {
+ /**
+ * 文件名称
+ */
+ private String name;
+
+ /**
+ * 文件地址
+ */
+ private String url;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ @Override
+ public String toString() {
+ return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
+ .append("name", getName())
+ .append("url", getUrl())
+ .toString();
+ }
+}
diff --git a/dragon-file-remote/pom.xml b/dragon-file-remote/pom.xml
new file mode 100644
index 0000000..8f6b506
--- /dev/null
+++ b/dragon-file-remote/pom.xml
@@ -0,0 +1,26 @@
+
+
+ 4.0.0
+
+ com.dragon
+ dragon-modules-file
+ 3.6.3
+
+ 3.6.3
+ dragon-file-remote
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+
+ com.dragon
+ dragon-file-common
+
+
+
diff --git a/dragon-file-remote/src/main/java/com/dragon/file/remote/RemoteFileService.java b/dragon-file-remote/src/main/java/com/dragon/file/remote/RemoteFileService.java
new file mode 100644
index 0000000..a5fbb6b
--- /dev/null
+++ b/dragon-file-remote/src/main/java/com/dragon/file/remote/RemoteFileService.java
@@ -0,0 +1,30 @@
+package com.dragon.file.remote;
+
+import com.dragon.common.core.constant.ServiceNameConstants;
+import com.dragon.common.core.domain.Result;
+import com.dragon.file.common.domain.SysFile;
+import com.dragon.file.remote.factory.RemoteFileFallbackFactory;
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.multipart.MultipartFile;
+
+;
+
+/**
+ * 文件服务
+ *
+ * @author dragon
+ */
+@FeignClient(contextId = "remoteFileService", value = ServiceNameConstants.FILE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
+public interface RemoteFileService {
+ /**
+ * 上传文件
+ *
+ * @param file 文件信息
+ * @return 结果
+ */
+ @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+ public Result upload(@RequestPart(value = "file") MultipartFile file);
+}
diff --git a/dragon-file-remote/src/main/java/com/dragon/file/remote/factory/RemoteFileFallbackFactory.java b/dragon-file-remote/src/main/java/com/dragon/file/remote/factory/RemoteFileFallbackFactory.java
new file mode 100644
index 0000000..a8ec01e
--- /dev/null
+++ b/dragon-file-remote/src/main/java/com/dragon/file/remote/factory/RemoteFileFallbackFactory.java
@@ -0,0 +1,34 @@
+package com.dragon.file.remote.factory;
+
+import com.dragon.common.core.domain.Result;
+import com.dragon.file.common.domain.SysFile;
+
+import com.dragon.file.remote.RemoteFileService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cloud.openfeign.FallbackFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+;
+
+/**
+ * 文件服务降级处理
+ *
+ * @author dragon
+ */
+@Component
+public class RemoteFileFallbackFactory implements FallbackFactory {
+ private static final Logger log = LoggerFactory.getLogger(RemoteFileFallbackFactory.class);
+
+ @Override
+ public RemoteFileService create(Throwable throwable) {
+ log.error("文件服务调用失败:{}", throwable.getMessage());
+ return new RemoteFileService() {
+ @Override
+ public Result upload(MultipartFile file) {
+ return Result.error("上传文件失败:" + throwable.getMessage());
+ }
+ };
+ }
+}
diff --git a/dragon-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/dragon-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 0000000..27762ce
--- /dev/null
+++ b/dragon-file-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1,2 @@
+com.dragon.file.remote.factory.RemoteFileFallbackFactory
+
diff --git a/dragon-file-server/pom.xml b/dragon-file-server/pom.xml
new file mode 100644
index 0000000..2b6eeed
--- /dev/null
+++ b/dragon-file-server/pom.xml
@@ -0,0 +1,92 @@
+
+
+ 4.0.0
+
+ com.dragon
+ dragon-modules-file
+ 3.6.3
+
+ 3.6.3
+ dragon-file-server
+
+
+ 17
+ 17
+ UTF-8
+
+
+
+ com.dragon
+ dragon-file-common
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-config
+
+
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-sentinel
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+
+ com.github.tobato
+ fastdfs-client
+
+
+
+
+ io.minio
+ minio
+ ${minio.version}
+
+
+
+
+
+ com.dragon
+ dragon-common-swagger
+
+
+
+ ${project.artifactId}
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+ repackage
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-deploy-plugin
+
+ true
+
+
+
+
+
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/DragonFileApplication.java b/dragon-file-server/src/main/java/com/dragon/file/server/DragonFileApplication.java
new file mode 100644
index 0000000..bd23ee6
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/DragonFileApplication.java
@@ -0,0 +1,19 @@
+package com.dragon.file.server;
+
+import com.dragon.common.swagger.annotation.EnableCustomSwagger2;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+
+/**
+ * 文件服务
+ *
+ * @author dragon
+ */
+@EnableCustomSwagger2
+@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
+public class DragonFileApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(DragonFileApplication.class, args);
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/config/MinioConfig.java b/dragon-file-server/src/main/java/com/dragon/file/server/config/MinioConfig.java
new file mode 100644
index 0000000..49261f3
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/config/MinioConfig.java
@@ -0,0 +1,72 @@
+package com.dragon.file.server.config;
+
+import io.minio.MinioClient;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Minio 配置信息
+ *
+ * @author dragon
+ */
+@Configuration
+@ConfigurationProperties(prefix = "minio")
+public class MinioConfig {
+ /**
+ * 服务地址
+ */
+ private String url;
+
+ /**
+ * 用户名
+ */
+ private String accessKey;
+
+ /**
+ * 密码
+ */
+ private String secretKey;
+
+ /**
+ * 存储桶名称
+ */
+ private String bucketName;
+
+ public String getUrl() {
+ return url;
+ }
+
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getAccessKey() {
+ return accessKey;
+ }
+
+ public void setAccessKey(String accessKey) {
+ this.accessKey = accessKey;
+ }
+
+ public String getSecretKey() {
+ return secretKey;
+ }
+
+ public void setSecretKey(String secretKey) {
+ this.secretKey = secretKey;
+ }
+
+ public String getBucketName() {
+ return bucketName;
+ }
+
+ public void setBucketName(String bucketName) {
+ this.bucketName = bucketName;
+ }
+
+ @Bean
+ public MinioClient getMinioClient() {
+ return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/config/ResourcesConfig.java b/dragon-file-server/src/main/java/com/dragon/file/server/config/ResourcesConfig.java
new file mode 100644
index 0000000..427fe5f
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/config/ResourcesConfig.java
@@ -0,0 +1,48 @@
+package com.dragon.file.server.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.io.File;
+
+/**
+ * 通用映射配置
+ *
+ * @author dragon
+ */
+@Configuration
+public class ResourcesConfig implements WebMvcConfigurer {
+ /**
+ * 资源映射路径 前缀
+ */
+ @Value("${file.prefix}")
+ public String localFilePrefix;
+ /**
+ * 上传文件存储在本地的根路径
+ */
+ @Value("${file.path}")
+ private String localFilePath;
+
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ /** 本地文件上传路径 */
+ registry.addResourceHandler(localFilePrefix + "/**")
+ .addResourceLocations("file:" + localFilePath + File.separator);
+ }
+
+ /**
+ * 开启跨域
+ */
+ @Override
+ public void addCorsMappings(CorsRegistry registry) {
+ // 设置允许跨域的路由
+ registry.addMapping(localFilePrefix + "/**")
+ // 设置允许跨域请求的域名
+ .allowedOrigins("*")
+ // 设置允许的方法
+ .allowedMethods("GET");
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/controller/SysFileController.java b/dragon-file-server/src/main/java/com/dragon/file/server/controller/SysFileController.java
new file mode 100644
index 0000000..ffee5d2
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/controller/SysFileController.java
@@ -0,0 +1,43 @@
+package com.dragon.file.server.controller;
+
+import com.dragon.common.core.domain.Result;;
+import com.dragon.common.core.utils.file.FileUtils;
+import com.dragon.file.server.service.ISysFileService;
+import com.dragon.file.common.domain.SysFile;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 文件请求处理
+ *
+ * @author dragon
+ */
+@RestController
+public class SysFileController {
+ private static final Logger log = LoggerFactory.getLogger(SysFileController.class);
+
+ @Autowired
+ private ISysFileService sysFileService;
+
+ /**
+ * 文件上传请求
+ */
+ @PostMapping("upload")
+ public Result upload(MultipartFile file) {
+ try {
+ // 上传并返回访问地址
+ String url = sysFileService.uploadFile(file);
+ SysFile sysFile = new SysFile();
+ sysFile.setName(FileUtils.getName(url));
+ sysFile.setUrl(url);
+ return Result.success(sysFile);
+ } catch (Exception e) {
+ log.error("上传文件失败", e);
+ return Result.error(e.getMessage());
+ }
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/service/FastDfsSysFileServiceImpl.java b/dragon-file-server/src/main/java/com/dragon/file/server/service/FastDfsSysFileServiceImpl.java
new file mode 100644
index 0000000..d8911ba
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/service/FastDfsSysFileServiceImpl.java
@@ -0,0 +1,45 @@
+package com.dragon.file.server.service;
+
+import com.alibaba.nacos.common.utils.IoUtils;
+import com.dragon.common.core.utils.file.FileTypeUtils;
+import com.github.tobato.fastdfs.domain.fdfs.StorePath;
+import com.github.tobato.fastdfs.service.FastFileStorageClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.InputStream;
+
+/**
+ * FastDFS 文件存储
+ *
+ * @author dragon
+ */
+@Service
+public class FastDfsSysFileServiceImpl implements ISysFileService {
+ /**
+ * 域名或本机访问地址
+ */
+ @Value("${fdfs.domain}")
+ public String domain;
+
+ @Autowired
+ private FastFileStorageClient storageClient;
+
+ /**
+ * FastDfs文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ @Override
+ public String uploadFile(MultipartFile file) throws Exception {
+ InputStream inputStream = file.getInputStream();
+ StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
+ FileTypeUtils.getExtension(file), null);
+ IoUtils.closeQuietly(inputStream);
+ return domain + "/" + storePath.getFullPath();
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/service/ISysFileService.java b/dragon-file-server/src/main/java/com/dragon/file/server/service/ISysFileService.java
new file mode 100644
index 0000000..d2f8fa2
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/service/ISysFileService.java
@@ -0,0 +1,19 @@
+package com.dragon.file.server.service;
+
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 文件上传接口
+ *
+ * @author dragon
+ */
+public interface ISysFileService {
+ /**
+ * 文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ public String uploadFile(MultipartFile file) throws Exception;
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/service/LocalSysFileServiceImpl.java b/dragon-file-server/src/main/java/com/dragon/file/server/service/LocalSysFileServiceImpl.java
new file mode 100644
index 0000000..881cc0c
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/service/LocalSysFileServiceImpl.java
@@ -0,0 +1,48 @@
+package com.dragon.file.server.service;
+
+import com.dragon.file.server.utils.FileUploadUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * 本地文件存储
+ *
+ * @author dragon
+ */
+@Primary
+@Service
+public class LocalSysFileServiceImpl implements ISysFileService {
+ /**
+ * 资源映射路径 前缀
+ */
+ @Value("${file.prefix}")
+ public String localFilePrefix;
+
+ /**
+ * 域名或本机访问地址
+ */
+ @Value("${file.domain}")
+ public String domain;
+
+ /**
+ * 上传文件存储在本地的根路径
+ */
+ @Value("${file.path}")
+ private String localFilePath;
+
+ /**
+ * 本地文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ @Override
+ public String uploadFile(MultipartFile file) throws Exception {
+ String name = FileUploadUtils.upload(localFilePath, file);
+ String url = domain + localFilePrefix + name;
+ return url;
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/service/MinioSysFileServiceImpl.java b/dragon-file-server/src/main/java/com/dragon/file/server/service/MinioSysFileServiceImpl.java
new file mode 100644
index 0000000..da24c66
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/service/MinioSysFileServiceImpl.java
@@ -0,0 +1,48 @@
+package com.dragon.file.server.service;
+
+import com.alibaba.nacos.common.utils.IoUtils;
+import com.dragon.file.server.config.MinioConfig;
+import com.dragon.file.server.utils.FileUploadUtils;
+import io.minio.MinioClient;
+import io.minio.PutObjectArgs;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.InputStream;
+
+/**
+ * Minio 文件存储
+ *
+ * @author dragon
+ */
+@Service
+public class MinioSysFileServiceImpl implements ISysFileService {
+ @Autowired
+ private MinioConfig minioConfig;
+
+ @Autowired
+ private MinioClient client;
+
+ /**
+ * Minio文件上传接口
+ *
+ * @param file 上传的文件
+ * @return 访问地址
+ * @throws Exception
+ */
+ @Override
+ public String uploadFile(MultipartFile file) throws Exception {
+ String fileName = FileUploadUtils.extractFilename(file);
+ InputStream inputStream = file.getInputStream();
+ PutObjectArgs args = PutObjectArgs.builder()
+ .bucket(minioConfig.getBucketName())
+ .object(fileName)
+ .stream(inputStream, file.getSize(), -1)
+ .contentType(file.getContentType())
+ .build();
+ client.putObject(args);
+ IoUtils.closeQuietly(inputStream);
+ return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
+ }
+}
diff --git a/dragon-file-server/src/main/java/com/dragon/file/server/utils/FileUploadUtils.java b/dragon-file-server/src/main/java/com/dragon/file/server/utils/FileUploadUtils.java
new file mode 100644
index 0000000..783e35c
--- /dev/null
+++ b/dragon-file-server/src/main/java/com/dragon/file/server/utils/FileUploadUtils.java
@@ -0,0 +1,157 @@
+package com.dragon.file.server.utils;
+
+import com.dragon.common.core.exception.file.FileException;
+import com.dragon.common.core.exception.file.FileNameLengthLimitExceededException;
+import com.dragon.common.core.exception.file.FileSizeLimitExceededException;
+import com.dragon.common.core.exception.file.InvalidExtensionException;
+import com.dragon.common.core.utils.DateUtils;
+import com.dragon.common.core.utils.StringUtils;
+import com.dragon.common.core.utils.file.FileTypeUtils;
+import com.dragon.common.core.utils.file.MimeTypeUtils;
+import com.dragon.common.core.utils.uuid.Seq;
+import org.apache.commons.io.FilenameUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.Objects;
+
+/**
+ * 文件上传工具类
+ *
+ * @author dragon
+ */
+public class FileUploadUtils {
+ /**
+ * 默认大小 50M
+ */
+ public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
+
+ /**
+ * 默认的文件名最大长度 100
+ */
+ public static final int DEFAULT_FILE_NAME_LENGTH = 100;
+
+ /**
+ * 根据文件路径上传
+ *
+ * @param baseDir 相对应用的基目录
+ * @param file 上传的文件
+ * @return 文件名称
+ * @throws IOException
+ */
+ public static final String upload(String baseDir, MultipartFile file) throws IOException {
+ try {
+ return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
+ } catch (FileException fe) {
+ throw new IOException(fe.getDefaultMessage(), fe);
+ } catch (Exception e) {
+ throw new IOException(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * 文件上传
+ *
+ * @param baseDir 相对应用的基目录
+ * @param file 上传的文件
+ * @param allowedExtension 上传文件类型
+ * @return 返回上传成功的文件名
+ * @throws FileSizeLimitExceededException 如果超出最大大小
+ * @throws FileNameLengthLimitExceededException 文件名太长
+ * @throws IOException 比如读写文件出错时
+ * @throws InvalidExtensionException 文件校验异常
+ */
+ public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
+ throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
+ InvalidExtensionException {
+ int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
+ if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
+ throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
+ }
+
+ assertAllowed(file, allowedExtension);
+
+ String fileName = extractFilename(file);
+
+ String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
+ file.transferTo(Paths.get(absPath));
+ return getPathFileName(fileName);
+ }
+
+ /**
+ * 编码文件名
+ */
+ public static final String extractFilename(MultipartFile file) {
+ return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
+ FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
+ }
+
+ private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
+ File desc = new File(uploadDir + File.separator + fileName);
+
+ if (!desc.exists()) {
+ if (!desc.getParentFile().exists()) {
+ desc.getParentFile().mkdirs();
+ }
+ }
+ return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
+ }
+
+ private static final String getPathFileName(String fileName) throws IOException {
+ String pathFileName = "/" + fileName;
+ return pathFileName;
+ }
+
+ /**
+ * 文件大小校验
+ *
+ * @param file 上传的文件
+ * @throws FileSizeLimitExceededException 如果超出最大大小
+ * @throws InvalidExtensionException 文件校验异常
+ */
+ public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
+ throws FileSizeLimitExceededException, InvalidExtensionException {
+ long size = file.getSize();
+ if (size > DEFAULT_MAX_SIZE) {
+ throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
+ }
+
+ String fileName = file.getOriginalFilename();
+ String extension = FileTypeUtils.getExtension(file);
+ if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
+ if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
+ throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
+ throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
+ throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
+ fileName);
+ } else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
+ throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
+ fileName);
+ } else {
+ throw new InvalidExtensionException(allowedExtension, extension, fileName);
+ }
+ }
+ }
+
+ /**
+ * 判断MIME类型是否是允许的MIME类型
+ *
+ * @param extension 上传文件类型
+ * @param allowedExtension 允许上传文件类型
+ * @return true/false
+ */
+ public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
+ for (String str : allowedExtension) {
+ if (str.equalsIgnoreCase(extension)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git a/dragon-file-server/src/main/resources/banner.txt b/dragon-file-server/src/main/resources/banner.txt
new file mode 100644
index 0000000..0dd5eee
--- /dev/null
+++ b/dragon-file-server/src/main/resources/banner.txt
@@ -0,0 +1,2 @@
+Spring Boot Version: ${spring-boot.version}
+Spring Application Name: ${spring.application.name}
diff --git a/dragon-file-server/src/main/resources/bootstrap.yml b/dragon-file-server/src/main/resources/bootstrap.yml
new file mode 100644
index 0000000..7e91f7d
--- /dev/null
+++ b/dragon-file-server/src/main/resources/bootstrap.yml
@@ -0,0 +1,25 @@
+# Tomcat
+server:
+ port: 9300
+
+# Spring
+spring:
+ application:
+ # 应用名称
+ name: dragon-file
+ profiles:
+ # 环境配置
+ active: dev
+ cloud:
+ nacos:
+ discovery:
+ # 服务注册地址
+ server-addr: 47.120.38.124:8848
+ config:
+ # 配置中心地址
+ server-addr: 47.120.38.124:8848
+ # 配置文件格式
+ file-extension: yml
+ # 共享配置
+ shared-configs:
+ - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
diff --git a/dragon-file-server/src/main/resources/logback.xml b/dragon-file-server/src/main/resources/logback.xml
new file mode 100644
index 0000000..b798a29
--- /dev/null
+++ b/dragon-file-server/src/main/resources/logback.xml
@@ -0,0 +1,74 @@
+
+
+
+
+
+
+
+
+
+
+ ${log.pattern}
+
+
+
+
+
+ ${log.path}/info.log
+
+
+
+ ${log.path}/info.%d{yyyy-MM-dd}.log
+
+ 60
+
+
+ ${log.pattern}
+
+
+
+ INFO
+
+ ACCEPT
+
+ DENY
+
+
+
+
+ ${log.path}/error.log
+
+
+
+ ${log.path}/error.%d{yyyy-MM-dd}.log
+
+ 60
+
+
+ ${log.pattern}
+
+
+
+ ERROR
+
+ ACCEPT
+
+ DENY
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..3c45411
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,45 @@
+
+
+
+ com.dragon
+ dragon-modules
+ 3.6.3
+
+ pom
+
+ dragon-file-common
+ dragon-file-remote
+ dragon-file-server
+
+ 4.0.0
+
+ dragon-modules-file
+
+
+ dragon-modules-file文件服务
+
+
+
+ dragon-release
+ dragon-releases
+ http://10.100.1.7:8081/repository/maven-releases/
+
+
+
+
+ dragon-public
+ dragon-maven
+ http://10.100.1.7:8081/repository/maven-public/
+
+
+ public
+ aliyun nexus
+ http://10.100.1.7:8081/repository/maven-releases/
+
+ true
+
+
+
+