mirror of
https://github.com/zfile-dev/zfile.git
synced 2025-04-19 05:34:52 +00:00
🎉 初始化提交
This commit is contained in:
15
src/main/java/im/zhaojun/ZfileApplication.java
Normal file
15
src/main/java/im/zhaojun/ZfileApplication.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package im.zhaojun;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy(exposeProxy = true)
|
||||
public class ZfileApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ZfileApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
109
src/main/java/im/zhaojun/aliyun/service/AliyunService.java
Normal file
109
src/main/java/im/zhaojun/aliyun/service/AliyunService.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package im.zhaojun.aliyun.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.*;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class AliyunService implements FileService {
|
||||
|
||||
@Value("${zfile.cache.timeout}")
|
||||
private Long timeout;
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private static final String BUCKET_NAME_KEY = "bucket-name";
|
||||
|
||||
private static final String ACCESS_KEY = "accessKey";
|
||||
|
||||
private static final String SECRET_KEY = "secretKey";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private static final String ENDPOINT_KEY = "endPoint";
|
||||
|
||||
private OSS ossClient;
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String domain;
|
||||
|
||||
private boolean isPrivate;
|
||||
|
||||
@Override
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.ALIYUN);
|
||||
String accessKey = stringStorageConfigMap.get(ACCESS_KEY).getValue();
|
||||
String secretKey = stringStorageConfigMap.get(SECRET_KEY).getValue();
|
||||
String endPoint = stringStorageConfigMap.get(ENDPOINT_KEY).getValue();
|
||||
|
||||
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
ossClient = new OSSClientBuilder().build(endPoint, accessKey, secretKey);
|
||||
|
||||
AccessControlList bucketAcl = ossClient.getBucketAcl(bucketName);
|
||||
CannedAccessControlList cannedACL = bucketAcl.getCannedACL();
|
||||
isPrivate = "Private".equals(cannedACL.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
ObjectListing objectListing =
|
||||
ossClient.listObjects(new ListObjectsRequest(bucketName).withDelimiter("/").withPrefix(path));
|
||||
|
||||
for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(s.getKey().substring(path.length()));
|
||||
fileItem.setSize(s.getSize());
|
||||
fileItem.setTime(s.getLastModified());
|
||||
fileItem.setType(FileTypeEnum.FILE);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
for (String commonPrefix : objectListing.getCommonPrefixes()) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(commonPrefix.substring(path.length(), commonPrefix.length() - 1));
|
||||
fileItem.setType(FileTypeEnum.FOLDER);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) throws Exception {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
path = URLDecoder.decode(path, "utf8");
|
||||
|
||||
if (isPrivate) {
|
||||
Date expirationDate = new Date(new Date().getTime() + timeout * 1000);
|
||||
URL url = ossClient.generatePresignedUrl(bucketName, path, expirationDate);
|
||||
return URLUtil.complateUrl(domain, url.getFile());
|
||||
} else {
|
||||
return URLUtil.complateUrl(domain, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package im.zhaojun.common.config;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.cache.interceptor.KeyGenerator;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public class CaffeineConfiguration {
|
||||
|
||||
@Value("${zfile.cache.timeout}")
|
||||
private Long timeout;
|
||||
|
||||
public static final String CACHE_NAME = "zfile";
|
||||
|
||||
/**
|
||||
* 个性化配置缓存
|
||||
*/
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager manager = new SimpleCacheManager();
|
||||
ArrayList<CaffeineCache> caches = new ArrayList<>();
|
||||
caches.add(new CaffeineCache(CACHE_NAME,
|
||||
Caffeine.newBuilder().recordStats()
|
||||
.expireAfterWrite(timeout, TimeUnit.SECONDS)
|
||||
.build())
|
||||
);
|
||||
manager.setCaches(caches);
|
||||
return manager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public KeyGenerator keyGenerator() {
|
||||
return new KeyGenerator() {
|
||||
@Override
|
||||
public Object generate(Object target, Method method, Object... params) {
|
||||
char separator = ':';
|
||||
StringBuilder strBuilder = new StringBuilder();
|
||||
// 类名
|
||||
strBuilder.append(target.getClass().getSimpleName());
|
||||
strBuilder.append(separator);
|
||||
// 方法名
|
||||
strBuilder.append(method.getName());
|
||||
strBuilder.append(separator);
|
||||
// 参数值
|
||||
for (Object object : params) {
|
||||
strBuilder.append(object).append(",");
|
||||
}
|
||||
return strBuilder.toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
14
src/main/java/im/zhaojun/common/config/RestConfig.java
Normal file
14
src/main/java/im/zhaojun/common/config/RestConfig.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package im.zhaojun.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(){
|
||||
return new RestTemplate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package im.zhaojun.common.config;
|
||||
|
||||
import im.zhaojun.aliyun.service.AliyunService;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.ftp.service.FtpService;
|
||||
import im.zhaojun.huawei.service.HuaweiService;
|
||||
import im.zhaojun.local.service.LocalService;
|
||||
import im.zhaojun.qiniu.service.QiniuService;
|
||||
import im.zhaojun.tencent.TencentService;
|
||||
import im.zhaojun.upyun.service.UpYunService;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 存储类型工厂类
|
||||
*/
|
||||
@Component
|
||||
public class StorageTypeFactory implements ApplicationContextAware {
|
||||
|
||||
private static Map<String, FileService> storageTypeEnumFileServiceMap;
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext act) throws BeansException {
|
||||
applicationContext = act;
|
||||
storageTypeEnumFileServiceMap = act.getBeansOfType(FileService.class);
|
||||
}
|
||||
|
||||
public static <T extends FileService> T getTrafficMode(StorageTypeEnum type) {
|
||||
String beanName = "";
|
||||
switch (type) {
|
||||
case UPYUN: beanName = applicationContext.getBeanNamesForType(UpYunService.class)[0]; break;
|
||||
case QINIU: beanName = applicationContext.getBeanNamesForType(QiniuService.class)[0]; break;
|
||||
case HUAWEI: beanName = applicationContext.getBeanNamesForType(HuaweiService.class)[0]; break;
|
||||
case FTP: beanName = applicationContext.getBeanNamesForType(FtpService.class)[0]; break;
|
||||
case ALIYUN: beanName = applicationContext.getBeanNamesForType(AliyunService.class)[0]; break;
|
||||
case LOCAL: beanName = applicationContext.getBeanNamesForType(LocalService.class)[0]; break;
|
||||
case TENCENT: beanName = applicationContext.getBeanNamesForType(TencentService.class)[0]; break;
|
||||
}
|
||||
return (T) storageTypeEnumFileServiceMap.get(beanName);
|
||||
}
|
||||
|
||||
}
|
||||
120
src/main/java/im/zhaojun/common/controller/FileController.java
Normal file
120
src/main/java/im/zhaojun/common/controller/FileController.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package im.zhaojun.common.controller;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.common.config.StorageTypeFactory;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
import im.zhaojun.common.model.ResultBean;
|
||||
import im.zhaojun.common.model.SiteConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.SystemConfigService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
private FileService fileService;
|
||||
|
||||
@Resource
|
||||
private SystemConfigService configService;
|
||||
|
||||
@GetMapping("/filelist")
|
||||
public ResultBean list(String path, String sortBy, boolean descending) throws Exception {
|
||||
List<FileItem> fileItems = fileService.fileList(path);
|
||||
|
||||
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
|
||||
fileItems.sort((o1, o2) -> {
|
||||
FileTypeEnum o1Type = o1.getType();
|
||||
FileTypeEnum o2Type = o2.getType();
|
||||
|
||||
if (o1Type.equals(o2Type)) {
|
||||
switch (sortBy) {
|
||||
case "name": return o1.getName().compareTo(o2.getName());
|
||||
case "time": return o1.getTime().compareTo(o2.getTime());
|
||||
case "size": return o1.getSize().compareTo(o2.getSize());
|
||||
default: return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
}
|
||||
|
||||
if (o1Type.equals(FileTypeEnum.FOLDER)) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
if (descending) {
|
||||
Collections.reverse(fileItems);
|
||||
}
|
||||
|
||||
return ResultBean.successData(fileItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载链接
|
||||
* @param path 路径
|
||||
* @return 下载链接
|
||||
*/
|
||||
@GetMapping("/downloadUrl")
|
||||
public ResultBean getDownloadUrl(String path) throws Exception {
|
||||
return ResultBean.successData(fileService.getDownloadUrl(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类容, 仅限用于, txt, md, ini 等普通文本文件.
|
||||
* @param path 文件路径
|
||||
* @return 文件内容
|
||||
*/
|
||||
@GetMapping("/getContent")
|
||||
public ResultBean getContent(String path) throws Exception {
|
||||
return ResultBean.successData(fileService.getTextContent(path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统配置信息和当前页的标题, 文件头, 文件尾信息
|
||||
* @param path 路径
|
||||
*/
|
||||
@GetMapping("/getConfig")
|
||||
public ResultBean getConfig(String path) throws Exception {
|
||||
SiteConfig config = fileService.getConfig(path);
|
||||
config.setSystemConfig(configService.getSystemConfig());
|
||||
return ResultBean.successData(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新存储策略, 使用 @PostConstruct 注解, 以用于第一次启动时, 根据数据库的配置值, 读取默认的存储策略.
|
||||
*/
|
||||
@PostConstruct
|
||||
@GetMapping("/updateStorageStrategy")
|
||||
public ResultBean updateConfig() {
|
||||
StorageTypeEnum storageStrategy = configService.getSystemConfig().getStorageStrategy();
|
||||
fileService = StorageTypeFactory.getTrafficMode(storageStrategy);
|
||||
return ResultBean.success();
|
||||
}
|
||||
|
||||
@GetMapping("clearCache")
|
||||
public ResultBean clearCache() throws Exception {
|
||||
fileService.clearCache();
|
||||
return ResultBean.success();
|
||||
}
|
||||
|
||||
@GetMapping("/getImageInfo")
|
||||
public ResultBean getImageInfo(String url) throws Exception {
|
||||
return ResultBean.success(fileService.getImageInfo(url));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package im.zhaojun.common.controller;
|
||||
|
||||
import im.zhaojun.common.service.SystemConfigService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
@Resource
|
||||
private SystemConfigService systemConfigService;
|
||||
|
||||
@GetMapping("/")
|
||||
public ModelAndView index(ModelAndView modelAndView) {
|
||||
modelAndView.setViewName("index");
|
||||
modelAndView.addObject("systemConfig", systemConfigService.getSystemConfig());
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
@GetMapping("/admin")
|
||||
public ModelAndView admin(ModelAndView modelAndView) {
|
||||
modelAndView.setViewName("admin");
|
||||
modelAndView.addObject("systemConfig", systemConfigService.getSystemConfig());
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
19
src/main/java/im/zhaojun/common/enums/FileTypeEnum.java
Normal file
19
src/main/java/im/zhaojun/common/enums/FileTypeEnum.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package im.zhaojun.common.enums;
|
||||
|
||||
public enum FileTypeEnum {
|
||||
FILE("File"), FOLDER("Folder");
|
||||
|
||||
private String value;
|
||||
|
||||
FileTypeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
52
src/main/java/im/zhaojun/common/enums/StorageTypeEnum.java
Normal file
52
src/main/java/im/zhaojun/common/enums/StorageTypeEnum.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package im.zhaojun.common.enums;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum StorageTypeEnum {
|
||||
|
||||
UPYUN("upyun", "又拍云 USS"),
|
||||
QINIU("qiniu", "七牛云 KODO"),
|
||||
HUAWEI("huawei", "华为云 OBS"),
|
||||
ALIYUN("aliyun", "阿里云 OSS"),
|
||||
FTP("ftp", "FTP"),
|
||||
LOCAL("local", "本地存储"),
|
||||
TENCENT("tencent", "腾讯云 COS");;
|
||||
|
||||
private static Map<String, StorageTypeEnum> enumMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (StorageTypeEnum type : StorageTypeEnum.values()) {
|
||||
enumMap.put(type.getKey(), type);
|
||||
}
|
||||
}
|
||||
|
||||
StorageTypeEnum(String key, String description) {
|
||||
this.key = key;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
private String key;
|
||||
private String description;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public static StorageTypeEnum getEnum(String value) {
|
||||
return enumMap.get(value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package im.zhaojun.common.enums;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class StorageTypeEnumTypeHandler extends BaseTypeHandler<StorageTypeEnum>{
|
||||
|
||||
public StorageTypeEnumTypeHandler(Class<StorageTypeEnum> type) {
|
||||
if (type == null)
|
||||
throw new IllegalArgumentException("Type argument cannot be null");
|
||||
StorageTypeEnum[] enums = type.getEnumConstants();
|
||||
if (enums == null)
|
||||
throw new IllegalArgumentException(type.getSimpleName()
|
||||
+ " does not represent an enum type.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
String i = rs.getString(columnName);
|
||||
if (rs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return StorageTypeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
String i = rs.getString(columnIndex);
|
||||
if (rs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return StorageTypeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
String i = cs.getString(columnIndex);
|
||||
if (cs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return StorageTypeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, StorageTypeEnum parameter, JdbcType jdbcType)
|
||||
throws SQLException {
|
||||
ps.setString(i, parameter.getKey());
|
||||
}
|
||||
|
||||
}
|
||||
38
src/main/java/im/zhaojun/common/enums/ViewModeEnum.java
Normal file
38
src/main/java/im/zhaojun/common/enums/ViewModeEnum.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package im.zhaojun.common.enums;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum ViewModeEnum {
|
||||
|
||||
DETAILS("details"), ICONS("icons"), GRID("grid");
|
||||
|
||||
private static Map<String, ViewModeEnum> enumMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (ViewModeEnum type : ViewModeEnum.values()) {
|
||||
enumMap.put(type.getValue(), type);
|
||||
}
|
||||
}
|
||||
|
||||
String value;
|
||||
|
||||
ViewModeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ViewModeEnum getEnum(String value) {
|
||||
return enumMap.get(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package im.zhaojun.common.enums;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ViewModeEnumTypeHandler extends BaseTypeHandler<ViewModeEnum>{
|
||||
|
||||
public ViewModeEnumTypeHandler(Class<ViewModeEnum> type) {
|
||||
if (type == null)
|
||||
throw new IllegalArgumentException("Type argument cannot be null");
|
||||
ViewModeEnum[] enums = type.getEnumConstants();
|
||||
if (enums == null)
|
||||
throw new IllegalArgumentException(type.getSimpleName()
|
||||
+ " does not represent an enum type.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewModeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
String i = rs.getString(columnName);
|
||||
if (rs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return ViewModeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewModeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
String i = rs.getString(columnIndex);
|
||||
if (rs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return ViewModeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewModeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
String i = cs.getString(columnIndex);
|
||||
if (cs.wasNull()) {
|
||||
return null;
|
||||
} else {
|
||||
return ViewModeEnum.getEnum(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, ViewModeEnum parameter, JdbcType jdbcType)
|
||||
throws SQLException {
|
||||
ps.setString(i, parameter.getValue());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package im.zhaojun.common.mapper;
|
||||
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface StorageConfigMapper {
|
||||
int deleteByPrimaryKey(Integer id);
|
||||
|
||||
int insert(StorageConfig record);
|
||||
|
||||
int insertSelective(StorageConfig record);
|
||||
|
||||
StorageConfig selectByPrimaryKey(Integer id);
|
||||
|
||||
int updateByPrimaryKeySelective(StorageConfig record);
|
||||
|
||||
int updateByPrimaryKey(StorageConfig record);
|
||||
|
||||
List<StorageConfig> selectStorageConfigByType(StorageTypeEnum type);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package im.zhaojun.common.mapper;
|
||||
|
||||
import im.zhaojun.common.model.SystemConfig;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SystemConfigMapper {
|
||||
|
||||
int insert(SystemConfig record);
|
||||
|
||||
int updateByPrimaryKeySelective(SystemConfig record);
|
||||
|
||||
int updateByPrimaryKey(SystemConfig record);
|
||||
|
||||
SystemConfig selectFirstConfig();
|
||||
}
|
||||
46
src/main/java/im/zhaojun/common/model/FileItem.java
Normal file
46
src/main/java/im/zhaojun/common/model/FileItem.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class FileItem implements Serializable {
|
||||
|
||||
private String name;
|
||||
private Date time;
|
||||
private Long size;
|
||||
private FileTypeEnum type;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public Long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Long size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public FileTypeEnum getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(FileTypeEnum type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
14
src/main/java/im/zhaojun/common/model/FolderItem.java
Normal file
14
src/main/java/im/zhaojun/common/model/FolderItem.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
public class FolderItem {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
27
src/main/java/im/zhaojun/common/model/ImageInfo.java
Normal file
27
src/main/java/im/zhaojun/common/model/ImageInfo.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
public class ImageInfo {
|
||||
private Integer width;
|
||||
private Integer height;
|
||||
|
||||
public ImageInfo(Integer width, Integer height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public Integer getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(Integer width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public Integer getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(Integer height) {
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
79
src/main/java/im/zhaojun/common/model/ResultBean.java
Normal file
79
src/main/java/im/zhaojun/common/model/ResultBean.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ResultBean implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8276264968757808344L;
|
||||
|
||||
private static final int SUCCESS = 0;
|
||||
|
||||
private static final int FAIL = -1;
|
||||
|
||||
private String msg = "操作成功";
|
||||
|
||||
private int code = SUCCESS;
|
||||
|
||||
private Object data;
|
||||
|
||||
private ResultBean() {
|
||||
super();
|
||||
}
|
||||
|
||||
private ResultBean(String msg, Object data, int code) {
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static ResultBean success() {
|
||||
return success("操作成功");
|
||||
}
|
||||
|
||||
public static ResultBean success(String msg) {
|
||||
return success(msg, null);
|
||||
}
|
||||
|
||||
public static ResultBean successData(Object data) {
|
||||
return success("操作成功", data);
|
||||
}
|
||||
|
||||
public static ResultBean success(Object data) {
|
||||
return success("操作成功", data);
|
||||
}
|
||||
|
||||
public static ResultBean success(String msg, Object data) {
|
||||
return new ResultBean(msg, data, SUCCESS);
|
||||
}
|
||||
|
||||
public static ResultBean error(String msg) {
|
||||
ResultBean resultBean = new ResultBean();
|
||||
resultBean.setCode(FAIL);
|
||||
resultBean.setMsg(msg);
|
||||
return resultBean;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
38
src/main/java/im/zhaojun/common/model/SiteConfig.java
Normal file
38
src/main/java/im/zhaojun/common/model/SiteConfig.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SiteConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8811196207046121740L;
|
||||
|
||||
private String header;
|
||||
|
||||
private String footer;
|
||||
|
||||
private SystemConfig systemConfig;
|
||||
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(String header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public String getFooter() {
|
||||
return footer;
|
||||
}
|
||||
|
||||
public void setFooter(String footer) {
|
||||
this.footer = footer;
|
||||
}
|
||||
|
||||
public SystemConfig getSystemConfig() {
|
||||
return systemConfig;
|
||||
}
|
||||
|
||||
public void setSystemConfig(SystemConfig systemConfig) {
|
||||
this.systemConfig = systemConfig;
|
||||
}
|
||||
}
|
||||
56
src/main/java/im/zhaojun/common/model/StorageConfig.java
Normal file
56
src/main/java/im/zhaojun/common/model/StorageConfig.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
|
||||
public class StorageConfig {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private StorageTypeEnum type;
|
||||
|
||||
private String key;
|
||||
|
||||
private String title;
|
||||
|
||||
private String value;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public StorageTypeEnum getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(StorageTypeEnum type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
90
src/main/java/im/zhaojun/common/model/SystemConfig.java
Normal file
90
src/main/java/im/zhaojun/common/model/SystemConfig.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package im.zhaojun.common.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.enums.ViewModeEnum;
|
||||
|
||||
public class SystemConfig {
|
||||
|
||||
@JsonIgnore
|
||||
private Integer id;
|
||||
|
||||
private String siteName;
|
||||
|
||||
private ViewModeEnum mode;
|
||||
|
||||
private Boolean sidebarEnable;
|
||||
|
||||
private Boolean infoEnable;
|
||||
|
||||
private Boolean searchEnable;
|
||||
|
||||
private Boolean searchIgnoreCase;
|
||||
|
||||
@JsonIgnore
|
||||
private StorageTypeEnum storageStrategy;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSiteName() {
|
||||
return siteName;
|
||||
}
|
||||
|
||||
public void setSiteName(String siteName) {
|
||||
this.siteName = siteName;
|
||||
}
|
||||
|
||||
public ViewModeEnum getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
public void setMode(ViewModeEnum mode) {
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public Boolean getSidebarEnable() {
|
||||
return sidebarEnable;
|
||||
}
|
||||
|
||||
public void setSidebarEnable(Boolean sidebarEnable) {
|
||||
this.sidebarEnable = sidebarEnable;
|
||||
}
|
||||
|
||||
public Boolean getInfoEnable() {
|
||||
return infoEnable;
|
||||
}
|
||||
|
||||
public void setInfoEnable(Boolean infoEnable) {
|
||||
this.infoEnable = infoEnable;
|
||||
}
|
||||
|
||||
public Boolean getSearchEnable() {
|
||||
return searchEnable;
|
||||
}
|
||||
|
||||
public void setSearchEnable(Boolean searchEnable) {
|
||||
this.searchEnable = searchEnable;
|
||||
}
|
||||
|
||||
public Boolean getSearchIgnoreCase() {
|
||||
return searchIgnoreCase;
|
||||
}
|
||||
|
||||
public void setSearchIgnoreCase(Boolean searchIgnoreCase) {
|
||||
this.searchIgnoreCase = searchIgnoreCase;
|
||||
}
|
||||
|
||||
public StorageTypeEnum getStorageStrategy() {
|
||||
return storageStrategy;
|
||||
}
|
||||
|
||||
public void setStorageStrategy(StorageTypeEnum storageStrategy) {
|
||||
this.storageStrategy = storageStrategy;
|
||||
}
|
||||
}
|
||||
75
src/main/java/im/zhaojun/common/service/FileService.java
Normal file
75
src/main/java/im/zhaojun/common/service/FileService.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package im.zhaojun.common.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import im.zhaojun.common.config.CaffeineConfiguration;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
import im.zhaojun.common.model.SiteConfig;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.List;
|
||||
|
||||
@CacheConfig(cacheNames = CaffeineConfiguration.CACHE_NAME, keyGenerator = "keyGenerator")
|
||||
public interface FileService {
|
||||
|
||||
@Cacheable
|
||||
List<FileItem> fileList(String path) throws Exception;
|
||||
|
||||
@Cacheable
|
||||
String getDownloadUrl(String path) throws Exception;
|
||||
|
||||
/**
|
||||
* 构建指定路径下标题, 页头, 页尾
|
||||
* @param path 路径
|
||||
*/
|
||||
@Cacheable
|
||||
default SiteConfig getConfig(String path) throws Exception {
|
||||
path = StringUtils.removeLastSeparator(path);
|
||||
SiteConfig siteConfig = new SiteConfig();
|
||||
for (FileItem fileItem : fileList(path)) {
|
||||
if ("readme.md".equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setFooter(getTextContent(path + "/" + fileItem.getName()));
|
||||
} else if ("header.md".equalsIgnoreCase(fileItem.getName())) {
|
||||
siteConfig.setHeader(getTextContent(path + "/" + fileItem.getName()));
|
||||
}
|
||||
}
|
||||
return siteConfig;
|
||||
}
|
||||
|
||||
default String getTextContent(String path) throws Exception {
|
||||
return HttpUtil.get(URLDecoder.decode(getDownloadUrl(path), "utf8"));
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
default void initMethod() throws Exception {}
|
||||
|
||||
/**
|
||||
* 清除缓存.
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
default void clearCache() throws Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取图片信息
|
||||
* @param url 图片 URL
|
||||
* @return 图片的信息, 宽、高
|
||||
*/
|
||||
@Cacheable
|
||||
default ImageInfo getImageInfo(String url) throws Exception {
|
||||
url = URLUtil.decode(url);
|
||||
InputStream inputStream = new URL(url).openStream();
|
||||
BufferedImage sourceImg = ImageIO.read(inputStream);
|
||||
return new ImageInfo(sourceImg.getWidth(), sourceImg.getHeight());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package im.zhaojun.common.service;
|
||||
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.mapper.StorageConfigMapper;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class StorageConfigService {
|
||||
|
||||
@Resource
|
||||
private StorageConfigMapper storageConfigMapper;
|
||||
|
||||
public List<StorageConfig> selectStorageConfigByType(StorageTypeEnum storageTypeEnum) {
|
||||
return storageConfigMapper.selectStorageConfigByType(storageTypeEnum);
|
||||
}
|
||||
|
||||
public Map<String, StorageConfig> selectStorageConfigMapByKey(StorageTypeEnum storageTypeEnum) {
|
||||
Map<String, StorageConfig> map = new HashMap<>();
|
||||
for (StorageConfig storageConfig : selectStorageConfigByType(storageTypeEnum)) {
|
||||
map.put(storageConfig.getKey(), storageConfig);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package im.zhaojun.common.service;
|
||||
|
||||
import im.zhaojun.common.mapper.SystemConfigMapper;
|
||||
import im.zhaojun.common.model.SystemConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class SystemConfigService {
|
||||
|
||||
@Resource
|
||||
private SystemConfigMapper systemConfigMapper;
|
||||
|
||||
@Cacheable("zfile")
|
||||
public SystemConfig getSystemConfig() {
|
||||
return systemConfigMapper.selectFirstConfig();
|
||||
}
|
||||
}
|
||||
44
src/main/java/im/zhaojun/common/util/StringUtils.java
Normal file
44
src/main/java/im/zhaojun/common/util/StringUtils.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package im.zhaojun.common.util;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
* 移除 URL 中的第一个 '/'
|
||||
* @return 如 path = '/folder1/file1', 返回 'folder1/file1'
|
||||
*/
|
||||
public static String removeFirstSeparator(String path) {
|
||||
if (!"".equals(path) && path.charAt(0) == '/') {
|
||||
path = path.substring(1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除 URL 中的最后一个 '/'
|
||||
* @return 如 path = '/folder1/file1/', 返回 '/folder1/file1'
|
||||
*/
|
||||
public static String removeLastSeparator(String path) {
|
||||
if (!"".equals(path) && path.charAt(path.length() - 1) == '/') {
|
||||
path = path.substring(0, path.length() - 1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将域名和路径组装成 URL, 主要用来处理分隔符 '/'
|
||||
* @param domain 域名
|
||||
* @param path 路径
|
||||
* @return URL
|
||||
*/
|
||||
public static String concatDomainAndPath(String domain, String path) {
|
||||
if (path != null && path.length() > 1 && path.charAt(0) != '/') {
|
||||
path = '/' + path;
|
||||
}
|
||||
|
||||
if (domain.charAt(domain.length() - 1) == '/') {
|
||||
domain = domain.substring(0, domain.length() - 2);
|
||||
}
|
||||
|
||||
return domain + path;
|
||||
}
|
||||
}
|
||||
73
src/main/java/im/zhaojun/ftp/service/FtpService.java
Normal file
73
src/main/java/im/zhaojun/ftp/service/FtpService.java
Normal file
@@ -0,0 +1,73 @@
|
||||
package im.zhaojun.ftp.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.extra.ftp.Ftp;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class FtpService implements FileService {
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private static final String HOST_KEY = "host";
|
||||
|
||||
private static final String PORT_KEY = "port";
|
||||
|
||||
private static final String USERNAME_KEY = "username";
|
||||
|
||||
private static final String PASSWORD_KEY = "password";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private Ftp ftp;
|
||||
|
||||
private String domain;
|
||||
|
||||
@Override
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.FTP);
|
||||
String host = stringStorageConfigMap.get(HOST_KEY).getValue();
|
||||
String port = stringStorageConfigMap.get(PORT_KEY).getValue();
|
||||
String username = stringStorageConfigMap.get(USERNAME_KEY).getValue();
|
||||
String password = stringStorageConfigMap.get(PASSWORD_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
|
||||
ftp = new Ftp(host, Integer.parseInt(port), username, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) {
|
||||
FTPFile[] ftpFiles = ftp.lsFiles(path);
|
||||
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
|
||||
for (FTPFile ftpFile : ftpFiles) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(ftpFile.getName());
|
||||
fileItem.setSize(ftpFile.getSize());
|
||||
fileItem.setTime(ftpFile.getTimestamp().getTime());
|
||||
fileItem.setType(ftpFile.isDirectory() ? FileTypeEnum.FOLDER : FileTypeEnum.FILE);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return URLUtil.complateUrl(domain, path);
|
||||
}
|
||||
}
|
||||
107
src/main/java/im/zhaojun/huawei/service/HuaweiService.java
Normal file
107
src/main/java/im/zhaojun/huawei/service/HuaweiService.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package im.zhaojun.huawei.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.*;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class HuaweiService implements FileService {
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String domain;
|
||||
|
||||
@Value("${zfile.cache.timeout}")
|
||||
private Long timeout;
|
||||
|
||||
private static final String BUCKET_NAME_KEY = "bucket-name";
|
||||
|
||||
private static final String ACCESS_KEY = "accessKey";
|
||||
|
||||
private static final String SECRET_KEY = "secretKey";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private static final String ENDPOINT_KEY = "endPoint";
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private ObsClient obsClient;
|
||||
|
||||
@Override
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.HUAWEI);
|
||||
String accessKey = stringStorageConfigMap.get(ACCESS_KEY).getValue();
|
||||
String secretKey = stringStorageConfigMap.get(SECRET_KEY).getValue();
|
||||
String endPoint = stringStorageConfigMap.get(ENDPOINT_KEY).getValue();
|
||||
|
||||
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
obsClient = new ObsClient(accessKey, secretKey, endPoint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
|
||||
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
|
||||
listObjectsRequest.setBucketName(bucketName);
|
||||
listObjectsRequest.setDelimiter("/");
|
||||
listObjectsRequest.setPrefix(path);
|
||||
ObjectListing objectListing = obsClient.listObjects(listObjectsRequest);
|
||||
List<ObsObject> objects = objectListing.getObjects();
|
||||
|
||||
for (ObsObject object : objects) {
|
||||
String fileName = object.getObjectKey();
|
||||
ObjectMetadata metadata = object.getMetadata();
|
||||
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(fileName.substring(path.length()));
|
||||
fileItem.setSize(metadata.getContentLength());
|
||||
fileItem.setTime(metadata.getLastModified());
|
||||
fileItem.setType(FileTypeEnum.FILE);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
for (String commonPrefix : objectListing.getCommonPrefixes()) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(commonPrefix.substring(0, commonPrefix.length() - 1));
|
||||
fileItem.setType(FileTypeEnum.FOLDER);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) throws Exception {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
path = URLDecoder.decode(path, "utf8");
|
||||
TemporarySignatureRequest req = new TemporarySignatureRequest(HttpMethodEnum.GET, timeout);
|
||||
req.setBucketName(bucketName);
|
||||
req.setObjectKey(path);
|
||||
TemporarySignatureResponse res = obsClient.createTemporarySignature(req);
|
||||
URL url = new URL(res.getSignedUrl());
|
||||
return URLUtil.complateUrl(domain, url.getFile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package im.zhaojun.local.controller;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import im.zhaojun.local.service.LocalService;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Date;
|
||||
|
||||
@Controller
|
||||
public class LocalController {
|
||||
|
||||
@Resource
|
||||
private LocalService localService;
|
||||
|
||||
@GetMapping("/local-download")
|
||||
@ResponseBody
|
||||
public ResponseEntity<FileSystemResource> downAttachment(String fileName) throws IOException {
|
||||
return export(new File(StringUtils.concatDomainAndPath(localService.getFilePath(), URLUtil.decode(fileName))));
|
||||
}
|
||||
|
||||
private ResponseEntity<FileSystemResource> export(File file) throws IOException {
|
||||
// 获取文件 MIME 类型
|
||||
String fileMimeType = Files.probeContentType(file.toPath());
|
||||
|
||||
MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
// 如果
|
||||
if (fileMimeType == null || "".equals(fileMimeType)) {
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
||||
} else {
|
||||
mediaType = MediaType.parseMediaType(fileMimeType);
|
||||
}
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
headers.add("Last-Modified", new Date().toString());
|
||||
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.headers(headers)
|
||||
.contentLength(file.length())
|
||||
.contentType(mediaType)
|
||||
.body(new FileSystemResource(file));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
99
src/main/java/im/zhaojun/local/service/LocalService.java
Normal file
99
src/main/java/im/zhaojun/local/service/LocalService.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package im.zhaojun.local.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.ImageInfo;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class LocalService implements FileService {
|
||||
|
||||
private static final String FILE_PATH_KEY = "filePath";
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private String filePath;
|
||||
|
||||
@Override
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.LOCAL);
|
||||
filePath = stringStorageConfigMap.get(FILE_PATH_KEY).getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) throws Exception {
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
|
||||
String fullPath = StringUtils.concatDomainAndPath(filePath, path);
|
||||
|
||||
File file = new File(fullPath);
|
||||
File[] files = file.listFiles();
|
||||
|
||||
if (files == null) {
|
||||
return fileItemList;
|
||||
}
|
||||
for (File f : files) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setType(f.isDirectory() ? FileTypeEnum.FOLDER : FileTypeEnum.FILE);
|
||||
fileItem.setTime(new Date(f.lastModified()));
|
||||
fileItem.setSize(f.length());
|
||||
fileItem.setName(f.getName());
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) throws Exception {
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
// 网络协议
|
||||
String networkProtocol = request.getScheme();
|
||||
// 网络ip
|
||||
String ip = request.getServerName();
|
||||
// 端口号
|
||||
int port = request.getServerPort();
|
||||
// 项目发布名称
|
||||
String webApp = request.getContextPath();
|
||||
return StringUtils.concatDomainAndPath(networkProtocol + "://" + ip + ":" + port + webApp, "local-download?fileName=" + path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageInfo getImageInfo(String url) throws Exception {
|
||||
String query = new URL(URLUtil.decode(url)).getQuery();
|
||||
url = url.replace(query, URLUtil.encode(query));
|
||||
InputStream inputStream = new URL(url).openStream();
|
||||
BufferedImage sourceImg = ImageIO.read(inputStream);
|
||||
return new ImageInfo(sourceImg.getWidth(), sourceImg.getHeight());
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void setFilePath(String filePath) {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
}
|
||||
112
src/main/java/im/zhaojun/qiniu/service/QiniuService.java
Normal file
112
src/main/java/im/zhaojun/qiniu/service/QiniuService.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package im.zhaojun.qiniu.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.storage.model.FileListing;
|
||||
import com.qiniu.util.Auth;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class QiniuService implements FileService {
|
||||
|
||||
@Value("${zfile.cache.timeout}")
|
||||
private Long timeout;
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private static final String BUCKET_NAME_KEY = "bucket-name";
|
||||
|
||||
private static final String ACCESS_KEY = "accessKey";
|
||||
|
||||
private static final String SECRET_KEY = "secretKey";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private BucketManager bucketManager;
|
||||
|
||||
private Auth auth;
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String domain;
|
||||
|
||||
private boolean isPrivate;
|
||||
|
||||
public void initMethod() throws QiniuException {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.QINIU);
|
||||
String accessKey = stringStorageConfigMap.get(ACCESS_KEY).getValue();
|
||||
String secretKey = stringStorageConfigMap.get(SECRET_KEY).getValue();
|
||||
|
||||
Configuration cfg = new Configuration(Zone.autoZone());
|
||||
auth = Auth.create(accessKey, secretKey);
|
||||
bucketManager = new BucketManager(auth, cfg);
|
||||
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
|
||||
isPrivate = bucketManager.getBucketInfo(bucketName).getPrivate() == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) throws Exception {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
|
||||
// 每次迭代的长度限制, 最大1000, 推荐值 1000
|
||||
int limit = 1000;
|
||||
// 指定目录分隔符, 列出所有公共前缀(模拟列出目录效果). 缺省值为空字符串
|
||||
String delimiter = "/";
|
||||
// 列举空间文件列表
|
||||
FileListing fileListing = bucketManager.listFilesV2(bucketName, path, "", limit, delimiter);
|
||||
for (FileInfo item : fileListing.items) {
|
||||
String fileKey = item.key;
|
||||
String fileName = fileKey.substring(path.length());
|
||||
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(fileName);
|
||||
fileItem.setSize(item.fsize);
|
||||
fileItem.setTime(new Date(item.putTime / 1000));
|
||||
fileItem.setType(FileTypeEnum.FILE);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
String[] commonPrefixes = fileListing.commonPrefixes;
|
||||
|
||||
for (String commonPrefix : commonPrefixes) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(commonPrefix.substring(0, commonPrefix.length() - 1));
|
||||
fileItem.setType(FileTypeEnum.FOLDER);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
String url = URLUtil.complateUrl(domain, path);
|
||||
if (isPrivate) {
|
||||
url = auth.privateDownloadUrl(url, timeout);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
103
src/main/java/im/zhaojun/tencent/TencentService.java
Normal file
103
src/main/java/im/zhaojun/tencent/TencentService.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package im.zhaojun.tencent;
|
||||
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.ClientConfig;
|
||||
import com.qcloud.cos.auth.BasicCOSCredentials;
|
||||
import com.qcloud.cos.auth.COSCredentials;
|
||||
import com.qcloud.cos.model.COSObjectSummary;
|
||||
import com.qcloud.cos.model.ListObjectsRequest;
|
||||
import com.qcloud.cos.model.ObjectListing;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import im.zhaojun.common.util.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class TencentService implements FileService {
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private static final String BUCKET_NAME_KEY = "bucket-name";
|
||||
|
||||
private static final String SECRET_ID_KEY = "secretId";
|
||||
|
||||
private static final String SECRET_KEY = "secretKey";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private static final String ENDPOINT_KEY = "endPoint";
|
||||
|
||||
@Value("${zfile.cache.timeout}")
|
||||
private Long timeout;
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String domain;
|
||||
|
||||
private COSClient cosClient;
|
||||
|
||||
@Override
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.TENCENT);
|
||||
String secretId = stringStorageConfigMap.get(SECRET_ID_KEY).getValue();
|
||||
String secretKey = stringStorageConfigMap.get(SECRET_KEY).getValue();
|
||||
String endPoint = stringStorageConfigMap.get(ENDPOINT_KEY).getValue();
|
||||
bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
|
||||
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
|
||||
Region region = new Region("ap-shanghai");
|
||||
ClientConfig clientConfig = new ClientConfig(region);
|
||||
// clientConfig.setSignExpired();
|
||||
cosClient = new COSClient(cred, clientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FileItem> fileList(String path) {
|
||||
path = StringUtils.removeFirstSeparator(path);
|
||||
|
||||
List<FileItem> fileItemList = new ArrayList<>();
|
||||
ObjectListing objectListing = cosClient.listObjects(new ListObjectsRequest().withBucketName(bucketName).withDelimiter("/").withPrefix(path));
|
||||
for (COSObjectSummary s : objectListing.getObjectSummaries()) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(s.getKey().substring(path.length()));
|
||||
fileItem.setSize(s.getSize());
|
||||
fileItem.setTime(s.getLastModified());
|
||||
fileItem.setType(FileTypeEnum.FILE);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
for (String commonPrefix : objectListing.getCommonPrefixes()) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(commonPrefix.substring(path.length(), commonPrefix.length() - 1));
|
||||
fileItem.setType(FileTypeEnum.FOLDER);
|
||||
fileItemList.add(fileItem);
|
||||
}
|
||||
|
||||
return fileItemList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
Date expirationDate = new Date(new Date().getTime() + timeout * 1000);
|
||||
URL url = cosClient.generatePresignedUrl(bucketName, path, expirationDate);
|
||||
return URLUtil.complateUrl(domain, url.getFile());
|
||||
}
|
||||
}
|
||||
76
src/main/java/im/zhaojun/upyun/service/UpYunService.java
Normal file
76
src/main/java/im/zhaojun/upyun/service/UpYunService.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package im.zhaojun.upyun.service;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import com.UpYun;
|
||||
import com.upyun.UpException;
|
||||
import im.zhaojun.common.enums.FileTypeEnum;
|
||||
import im.zhaojun.common.enums.StorageTypeEnum;
|
||||
import im.zhaojun.common.model.FileItem;
|
||||
import im.zhaojun.common.model.StorageConfig;
|
||||
import im.zhaojun.common.service.FileService;
|
||||
import im.zhaojun.common.service.StorageConfigService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class UpYunService implements FileService {
|
||||
|
||||
@Resource
|
||||
private StorageConfigService storageConfigService;
|
||||
|
||||
private static final String BUCKET_NAME_KEY = "bucket-name";
|
||||
|
||||
private static final String USERNAME_KEY = "username";
|
||||
|
||||
private static final String PASSWORD_KEY = "password";
|
||||
|
||||
private static final String DOMAIN_KEY = "domain";
|
||||
|
||||
private String domain;
|
||||
|
||||
private UpYun upYun;
|
||||
|
||||
public void initMethod() {
|
||||
Map<String, StorageConfig> stringStorageConfigMap =
|
||||
storageConfigService.selectStorageConfigMapByKey(StorageTypeEnum.UPYUN);
|
||||
String bucketName = stringStorageConfigMap.get(BUCKET_NAME_KEY).getValue();
|
||||
String username = stringStorageConfigMap.get(USERNAME_KEY).getValue();
|
||||
String password = stringStorageConfigMap.get(PASSWORD_KEY).getValue();
|
||||
domain = stringStorageConfigMap.get(DOMAIN_KEY).getValue();
|
||||
upYun = new UpYun(bucketName, username, password);
|
||||
}
|
||||
|
||||
public List<FileItem> fileList(String path) throws IOException, UpException {
|
||||
ArrayList<FileItem> fileItems = new ArrayList<>();
|
||||
|
||||
List<UpYun.FolderItem> folderItems = upYun.readDir(path, null);
|
||||
|
||||
if (folderItems != null) {
|
||||
for (UpYun.FolderItem folderItem : folderItems) {
|
||||
FileItem fileItem = new FileItem();
|
||||
fileItem.setName(folderItem.name);
|
||||
fileItem.setSize(folderItem.size);
|
||||
fileItem.setTime(folderItem.date);
|
||||
|
||||
if ("Folder".equals(folderItem.type)) {
|
||||
fileItem.setType(FileTypeEnum.FOLDER);
|
||||
} else if ("File".equals(folderItem.type)) {
|
||||
fileItem.setType(FileTypeEnum.FILE);
|
||||
}
|
||||
fileItems.add(fileItem);
|
||||
}
|
||||
}
|
||||
return fileItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return URLUtil.complateUrl(domain, path);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user