You've already forked FrameTour-BE
修改IDevice+IStor
This commit is contained in:
@@ -38,15 +38,15 @@ public class StorageFactory {
|
||||
return get(storageType);
|
||||
}
|
||||
|
||||
protected static Map<String, IStorageAdapter> definedName = new HashMap<>();
|
||||
protected static Map<String, IStorageAdapter> namedStorage = new HashMap<>();
|
||||
protected static IStorageAdapter defaultStorage = null;
|
||||
|
||||
public static void register(String name, IStorageAdapter adapter) {
|
||||
definedName.put(name, adapter);
|
||||
namedStorage.put(name, adapter);
|
||||
}
|
||||
|
||||
public static IStorageAdapter use(String name) {
|
||||
IStorageAdapter adapter = definedName.get(name);
|
||||
IStorageAdapter adapter = namedStorage.get(name);
|
||||
if (adapter == null) {
|
||||
throw new StorageUndefinedException("未定义的存储方式:"+name);
|
||||
}
|
||||
|
@@ -37,6 +37,11 @@ public abstract class AStorageAdapter implements IStorageAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForDownload(String... path) {
|
||||
return getUrlForUpload(new Date(System.currentTimeMillis() + 1000 * 60 * 60), path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForUpload(String... path) {
|
||||
return getUrlForUpload(new Date(System.currentTimeMillis() + 1000 * 60 * 60), path);
|
||||
|
@@ -5,18 +5,29 @@ import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.HttpMethod;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.model.DeleteObjectsRequest;
|
||||
import com.aliyun.oss.model.ListObjectsV2Request;
|
||||
import com.aliyun.oss.model.ListObjectsV2Result;
|
||||
import com.aliyun.oss.model.OSSObjectSummary;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.ycwl.basic.storage.entity.AliOssStorageConfig;
|
||||
import com.ycwl.basic.storage.entity.StorageConfig;
|
||||
import com.ycwl.basic.storage.entity.StorageFileObject;
|
||||
import com.ycwl.basic.storage.exceptions.StorageConfigException;
|
||||
import com.ycwl.basic.storage.exceptions.StorageException;
|
||||
import com.ycwl.basic.storage.exceptions.UploadFileFailedException;
|
||||
import com.ycwl.basic.storage.utils.StorageUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
final public class AliOssAdapter extends AStorageAdapter {
|
||||
private AliOssStorageConfig config;
|
||||
@@ -80,6 +91,13 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
return config.getUrl() + "/" + buildPath(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForDownload(Date expireDate, String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
URL url = ossClient.generatePresignedUrl(config.getBucketName(), buildPath(path), expireDate, HttpMethod.GET);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForUpload(Date expireDate, String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
@@ -87,6 +105,68 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageFileObject> listDir(String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(config.getBucketName());
|
||||
listObjectsV2Request.setPrefix(buildPath(path));
|
||||
listObjectsV2Request.setMaxKeys(1000);
|
||||
boolean isTruncated = true;
|
||||
String continuationToken = null;
|
||||
List<OSSObjectSummary> objectList = new ArrayList<>();
|
||||
try {
|
||||
while (isTruncated) {
|
||||
if (continuationToken != null) {
|
||||
listObjectsV2Request.setContinuationToken(continuationToken);
|
||||
}
|
||||
|
||||
// 列举文件。
|
||||
ListObjectsV2Result result = ossClient.listObjectsV2(listObjectsV2Request);
|
||||
|
||||
objectList.addAll(result.getObjectSummaries());
|
||||
|
||||
isTruncated = result.isTruncated();
|
||||
continuationToken = result.getNextContinuationToken();
|
||||
}
|
||||
return objectList.stream().map(item -> {
|
||||
StorageFileObject object = new StorageFileObject();
|
||||
object.setPath(item.getKey().substring(0, item.getKey().lastIndexOf("/")));
|
||||
object.setName(item.getKey().substring(item.getKey().lastIndexOf("/") + 1));
|
||||
object.setSize(item.getSize());
|
||||
object.setRawObject(item);
|
||||
return object;
|
||||
}).collect(Collectors.toList());
|
||||
} catch (OSSException e) {
|
||||
throw new StorageException("列举文件失败:" + e.getErrorMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteDir(String... path) {
|
||||
List<StorageFileObject> objectList = listDir(buildPath(path));
|
||||
OSS ossClient = getOssClient();
|
||||
if (objectList.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
int idx = 0;
|
||||
int batchSize = 999;
|
||||
while (objectList.size() > idx) {
|
||||
if (objectList.size() - idx < batchSize) {
|
||||
batchSize = objectList.size() - idx;
|
||||
}
|
||||
List<StorageFileObject> subList = objectList.subList(idx, idx + batchSize);
|
||||
idx += batchSize;
|
||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
|
||||
request.setKeys(subList.stream().map(StorageFileObject::getFullPath).collect(Collectors.toList()));
|
||||
try {
|
||||
ossClient.deleteObjects(request);
|
||||
} catch (OSSException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private OSS getOssClient() {
|
||||
OSS ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
|
||||
return ossClient;
|
||||
|
@@ -1,11 +1,13 @@
|
||||
package com.ycwl.basic.storage.adapters;
|
||||
|
||||
import com.ycwl.basic.storage.entity.StorageConfig;
|
||||
import com.ycwl.basic.storage.entity.StorageFileObject;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface IStorageAdapter {
|
||||
@@ -16,6 +18,10 @@ public interface IStorageAdapter {
|
||||
String uploadFile(MultipartFile file, String ...path);
|
||||
boolean deleteFile(String ...path);
|
||||
String getUrl(String ...path);
|
||||
String getUrlForDownload(String ...path);
|
||||
String getUrlForDownload(Date expireDate, String ...path);
|
||||
String getUrlForUpload(String ...path);
|
||||
String getUrlForUpload(Date expireDate, String ...path);
|
||||
List<StorageFileObject> listDir(String ...path);
|
||||
boolean deleteDir(String ...path);
|
||||
}
|
||||
|
@@ -1,9 +1,12 @@
|
||||
package com.ycwl.basic.storage.adapters;
|
||||
|
||||
import com.ycwl.basic.storage.entity.StorageConfig;
|
||||
import com.ycwl.basic.storage.entity.StorageFileObject;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class LocalStorageAdapter extends AStorageAdapter{
|
||||
@@ -32,8 +35,23 @@ public class LocalStorageAdapter extends AStorageAdapter{
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForDownload(Date expireDate, String... path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForUpload(Date expireDate, String... path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageFileObject> listDir(String... path) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteDir(String... path) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,20 @@
|
||||
package com.ycwl.basic.storage.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class StorageFileObject {
|
||||
private String path;
|
||||
private String name;
|
||||
private Long size;
|
||||
private Object rawObject;
|
||||
|
||||
public String getFullPath() {
|
||||
return path + "/" + name;
|
||||
}
|
||||
|
||||
public void setFullPath(String path) {
|
||||
this.path = path.substring(0, path.lastIndexOf("/"));
|
||||
this.name = path.substring(path.lastIndexOf("/") + 1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user