diff --git a/plugin.xml b/plugin.xml index 63de31a..1bff9bd 100644 --- a/plugin.xml +++ b/plugin.xml @@ -5,12 +5,13 @@ com.fanruan.fs yes no - 1.2.3 + 1.2.4 10.0 2020-01-27 richie [2021-01-20]正式作为官方插件维护。
[2020-12-16]修复远程设计新增、修改模板问题。
[2020-10-29]修复连接不释放问题。
diff --git a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java b/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java index 4ea1913..9c18ead 100644 --- a/src/main/java/com/fanruan/fs/s3/repository/core/S3ResourceRepository.java +++ b/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 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,11 +142,16 @@ public class S3ResourceRepository extends BaseResourceRepository { @Override public boolean createFile(String path) { - PutObjectRequest request = new PutObjectRequest(bucket, path, new ByteArrayInputStream(new byte[0]), buildEmptyMetadata()); - try { - s3.putObject(request); - } catch (Exception e) { - return false; + List 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); + 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()); - try { - s3.putObject(request); - } catch (Exception e) { - return false; + List 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); + return false; + } } return true; } @Override public boolean delete(String path) { + + s3.deleteObject(bucket, path); + String prefix = path; + if (!path.endsWith(DELIMITER)) { + prefix = path + DELIMITER; + } try { - s3.deleteObject(bucket, path); + 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 getAllNecessaryPath(String path) { + + // 获取所有path路径及其所有父路径 + List 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; + } + }