Browse Source

Merge branch 'release/10.0' of https://code.fineres.com/scm/~dylan.liu/plugin-repository-s3 into release/10.0

release/10.0^2
Dylan.Liu 4 years ago
parent
commit
0402aa3a9e
  1. 3
      plugin.xml
  2. 84
      src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

3
plugin.xml

@ -5,12 +5,13 @@
<main-package>com.fanruan.fs</main-package>
<active>yes</active>
<hidden>no</hidden>
<version>1.2.3</version>
<version>1.2.4</version>
<env-version>10.0</env-version>
<jartime>2020-01-27</jartime>
<vendor>richie</vendor>
<description><![CDATA[使用支持S3协议的云存储文件系统作为文件服务器。]]></description>
<change-notes><![CDATA[
[2021-01-24]修复定时调度结果文件预览失败。<br/>
[2021-01-20]正式作为官方插件维护。<br/>
[2020-12-16]修复远程设计新增、修改模板问题。<br/>
[2020-10-29]修复连接不释放问题。<br/>

84
src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java

@ -19,6 +19,7 @@ import com.fanruan.api.util.StringKit;
import com.fr.io.repository.FineFileEntry;
import com.fr.io.repository.base.BaseResourceRepository;
import com.fr.stable.Filter;
import com.fr.stable.StringUtils;
import com.fr.workspace.resource.ResourceIOException;
import java.io.ByteArrayInputStream;
@ -110,6 +111,17 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public void write(String path, byte[] data) {
List<String> paths = getAllNecessaryPath(path);
for (int i = paths.size() - 1; i > 0; i--) {
String parent = paths.get(i);
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try {
s3.putObject(req);
} catch (Exception e) {
LogKit.error("[S3] Failed to create parent path {}", parent);
}
}
ObjectMetadata metadata;
try {
metadata = s3.getObjectMetadata(bucket, path);
@ -130,12 +142,17 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean createFile(String path) {
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
List<String> paths = getAllNecessaryPath(path);
for (int i = paths.size() - 1; i >= 0; i--) {
String parent = paths.get(i);
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try {
s3.putObject(request);
s3.putObject(req);
} catch (Exception e) {
LogKit.error("[S3] Failed to create parent path {}", parent);
return false;
}
}
return true;
}
@ -145,23 +162,45 @@ public class S3ResourceRepository extends BaseResourceRepository {
if (!path.endsWith(DELIMITER)) {
path += DELIMITER;
}
PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
List<String> paths = getAllNecessaryPath(path);
for (int i = paths.size() - 1; i >= 0; i--) {
String parent = paths.get(i);
PutObjectRequest req = new PutObjectRequest(bucket, parent, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata());
try {
s3.putObject(request);
s3.putObject(req);
} catch (Exception e) {
LogKit.error("[S3] Failed to create parent path {}", parent);
return false;
}
}
return true;
}
@Override
public boolean delete(String path) {
try {
s3.deleteObject(bucket, path);
String prefix = path;
if (!path.endsWith(DELIMITER)) {
prefix = path + DELIMITER;
}
try {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket)
.withPrefix(prefix).withDelimiter(DELIMITER);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
String key = summary.getKey();
if (!key.endsWith(DELIMITER)) {
s3.deleteObject(bucket, key);
}
}
for (String pre : objectListing.getCommonPrefixes()) {
delete(pre);
}
} catch (Exception e) {
LogKit.error(e.getMessage(), e);
return false;
}
s3.deleteObject(bucket, prefix);
return true;
}
@ -184,7 +223,12 @@ public class S3ResourceRepository extends BaseResourceRepository {
.withPrefix(dir).withDelimiter(DELIMITER);
ObjectListing objectListing = s3.listObjects(listObjectsRequest);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
String[] arr = objectSummary.getKey().split(DELIMITER);
String key = objectSummary.getKey();
if (StringUtils.equals(key, dir)) {
continue;
}
String[] arr = key.split(DELIMITER);
String name = arr[arr.length - 1];
if (filter == null) {
result.add(name);
@ -210,7 +254,8 @@ public class S3ResourceRepository extends BaseResourceRepository {
@Override
public boolean isDirectory(String path) {
if (path.endsWith(DELIMITER)) {
if (path.endsWith(DELIMITER) && exist(path)) {
return true;
}
ObjectListing listing = s3.listObjects(bucket, path);
@ -311,4 +356,27 @@ public class S3ResourceRepository extends BaseResourceRepository {
return entry;
}
/**
* 递归创建父目录的metadata. 比如path是 {@code WEB-INF/reportlets/test/1.cpt}
* 则返回 {@code [WEB-INF/reportlets/test/1.cpt, WEB-INF/reportlets/test/, WEB-INF/reportlets/, WEB-INF/]}
*/
private List<String> getAllNecessaryPath(String path) {
// 获取所有path路径及其所有父路径
List<String> allPath = new ArrayList<>();
allPath.add(path);
int lastIdxOfDelimiter = path.lastIndexOf(DELIMITER);
// 以/结尾的先把末尾的/去掉
if (lastIdxOfDelimiter == path.length() - 1) {
path = path.substring(0, lastIdxOfDelimiter);
lastIdxOfDelimiter = path.lastIndexOf(DELIMITER);
}
while (lastIdxOfDelimiter > 0) {
allPath.add(path.substring(0, lastIdxOfDelimiter + 1));
path = path.substring(0, lastIdxOfDelimiter);
lastIdxOfDelimiter = path.lastIndexOf(DELIMITER);
}
return allPath;
}
}

Loading…
Cancel
Save