完成 yudao-spring-boot-starter-file 组件,支持 S3 对接云存储、local、ftp、sftp、db 等协议
parent
3d40fc81dd
commit
05d4aae65d
@ -0,0 +1,21 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.FileClientFactoryImpl;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件配置类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class YudaoFileAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FileClientFactory fileClientFactory() {
|
||||||
|
return new FileClientFactoryImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl;
|
package cn.iocoder.yudao.framework.file.core.client;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.core.client;
|
||||||
|
|
||||||
|
public interface FileClientFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得文件客户端
|
||||||
|
*
|
||||||
|
* @param channelId 渠道编号
|
||||||
|
* @return 文件客户端
|
||||||
|
*/
|
||||||
|
FileClient getFileClient(Long channelId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建文件客户端
|
||||||
|
*
|
||||||
|
* @param configId 配置编号
|
||||||
|
* @param storage 存储器的枚举 {@link cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum}
|
||||||
|
* @param config 文件配置
|
||||||
|
*/
|
||||||
|
<Config extends FileClientConfig> void createOrUpdateFileClient(Long configId, Integer storage, Config config);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.core.client.db;
|
||||||
|
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于 DB 存储的文件客户端的配置类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class DBFileClient extends AbstractFileClient<DBFileClientConfig> {
|
||||||
|
|
||||||
|
private DBFileContentFrameworkDAO dao;
|
||||||
|
|
||||||
|
public DBFileClient(Long id, DBFileClientConfig config) {
|
||||||
|
super(id, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doInit() {
|
||||||
|
dao = SpringUtil.getBean(DBFileContentFrameworkDAO.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String upload(byte[] content, String path) {
|
||||||
|
dao.insert(getId(), path, content);
|
||||||
|
// 拼接返回路径
|
||||||
|
return super.formatFileUrl(config.getDomain(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String path) {
|
||||||
|
dao.delete(getId(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getContent(String path) {
|
||||||
|
return dao.selectContent(getId(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.core.client.db;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.hibernate.validator.constraints.URL;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于 DB 存储的文件客户端的配置类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DBFileClientConfig implements FileClientConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义域名
|
||||||
|
*/
|
||||||
|
@NotEmpty(message = "domain 不能为空")
|
||||||
|
@URL(message = "domain 必须是 URL 格式")
|
||||||
|
private String domain;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.core.client.db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件内容 Framework DAO 接口
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public interface DBFileContentFrameworkDAO {
|
||||||
|
|
||||||
|
void insert(Long configId, String path, byte[] content);
|
||||||
|
|
||||||
|
void delete(Long configId, String path);
|
||||||
|
|
||||||
|
byte[] selectContent(Long configId, String path);
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.ftp;
|
package cn.iocoder.yudao.framework.file.core.client.ftp;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.local;
|
package cn.iocoder.yudao.framework.file.core.client.local;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.iocoder.yudao.framework.file.core.client.impl.AbstractFileClient;
|
import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.local;
|
package cn.iocoder.yudao.framework.file.core.client.local;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.s3;
|
package cn.iocoder.yudao.framework.file.core.client.s3;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.s3;
|
package cn.iocoder.yudao.framework.file.core.client.s3;
|
||||||
|
|
||||||
import software.amazon.awssdk.core.interceptor.Context;
|
import software.amazon.awssdk.core.interceptor.Context;
|
||||||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
|
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
|
||||||
@ -1,9 +1,9 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.sftp;
|
package cn.iocoder.yudao.framework.file.core.client.sftp;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.extra.ssh.Sftp;
|
import cn.hutool.extra.ssh.Sftp;
|
||||||
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
|
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
|
||||||
import cn.iocoder.yudao.framework.file.core.client.impl.AbstractFileClient;
|
import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package cn.iocoder.yudao.framework.file.core.client.impl.sftp;
|
package cn.iocoder.yudao.framework.file.core.client.sftp;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
package cn.iocoder.yudao.framework.file.core.enums;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.db.DBFileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.db.DBFileClientConfig;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.ftp.FtpFileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.ftp.FtpFileClientConfig;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.s3.S3FileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.s3.S3FileClientConfig;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.sftp.SftpFileClient;
|
||||||
|
import cn.iocoder.yudao.framework.file.core.client.sftp.SftpFileClientConfig;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件存储器枚举
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum FileStorageEnum {
|
||||||
|
|
||||||
|
DB(1, DBFileClientConfig.class, DBFileClient.class),
|
||||||
|
|
||||||
|
LOCAL(10, LocalFileClientConfig.class, LocalFileClient.class),
|
||||||
|
FTP(11, FtpFileClientConfig.class, FtpFileClient.class),
|
||||||
|
SFTP(12, SftpFileClientConfig.class, SftpFileClient.class),
|
||||||
|
|
||||||
|
S3(20, S3FileClientConfig.class, S3FileClient.class),
|
||||||
|
;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 存储器
|
||||||
|
*/
|
||||||
|
private final Integer storage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置类
|
||||||
|
*/
|
||||||
|
private final Class<? extends FileClientConfig> configClass;
|
||||||
|
/**
|
||||||
|
* 客户端类
|
||||||
|
*/
|
||||||
|
private final Class<? extends FileClient> clientClass;
|
||||||
|
|
||||||
|
public static FileStorageEnum getByStorage(Integer storage) {
|
||||||
|
return ArrayUtil.firstMatch(o -> o.getStorage().equals(storage), values());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
cn.iocoder.yudao.framework.file.config.YudaoFileAutoConfiguration
|
||||||
Loading…
Reference in New Issue