KeyValueResource.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package org.ssssssss.magicapi.adapter.resource;
  2. import org.ssssssss.magicapi.adapter.Resource;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.*;
  6. import java.util.function.Function;
  7. import java.util.stream.Collectors;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipOutputStream;
  10. public abstract class KeyValueResource implements Resource {
  11. protected String separator;
  12. protected String path;
  13. protected KeyValueResource parent;
  14. protected boolean readonly = false;
  15. public KeyValueResource(String separator, String path, KeyValueResource parent) {
  16. this.separator = separator;
  17. this.path = path;
  18. this.parent = parent;
  19. }
  20. public KeyValueResource(String separator, String path, boolean readonly, KeyValueResource parent) {
  21. this.separator = separator;
  22. this.path = path;
  23. this.parent = parent;
  24. this.readonly = readonly;
  25. }
  26. @Override
  27. public boolean isDirectory() {
  28. return this.path.endsWith(separator);
  29. }
  30. @Override
  31. public final boolean renameTo(Resource resource) {
  32. if(resource.name().equalsIgnoreCase(this.name())){
  33. return true;
  34. }
  35. if (!(resource instanceof KeyValueResource)) {
  36. throw new IllegalArgumentException("无法将" + this.getAbsolutePath() + "重命名为:" + resource.getAbsolutePath());
  37. }
  38. KeyValueResource targetResource = (KeyValueResource) resource;
  39. // 判断移动的是否是文件夹
  40. if (resource.isDirectory()) {
  41. Set<String> oldKeys = this.keys();
  42. Map<String, String> mappings = new HashMap<>(oldKeys.size());
  43. int keyLen = this.path.length();
  44. oldKeys.forEach(oldKey -> mappings.put(oldKey, targetResource.path + oldKey.substring(keyLen)));
  45. return renameTo(mappings);
  46. } else {
  47. return renameTo(Collections.singletonMap(this.path, targetResource.path));
  48. }
  49. }
  50. @Override
  51. public boolean delete() {
  52. return this.keys().stream().allMatch(this::deleteByKey);
  53. }
  54. protected boolean deleteByKey(String key) {
  55. return false;
  56. }
  57. /**
  58. * 需要做修改的key,原key: 新key
  59. */
  60. protected abstract boolean renameTo(Map<String, String> renameKeys);
  61. @Override
  62. public String name() {
  63. String name = this.path;
  64. if (isDirectory()) {
  65. name = this.path.substring(0, name.length() - 1);
  66. }
  67. int index = name.lastIndexOf(separator);
  68. return index > -1 ? name.substring(index + 1) : name;
  69. }
  70. @Override
  71. public Resource getResource(String name) {
  72. name = (isDirectory() ? this.path : this.path + separator) + name;
  73. return mappedFunction().apply(name);
  74. }
  75. @Override
  76. public Resource getDirectory(String name) {
  77. return getResource(name + separator);
  78. }
  79. @Override
  80. public boolean mkdir() {
  81. if (!isDirectory()) {
  82. this.path += separator;
  83. }
  84. return write("this is directory");
  85. }
  86. @Override
  87. public Resource parent() {
  88. return this.parent;
  89. }
  90. @Override
  91. public boolean write(byte[] bytes) {
  92. return write(new String(bytes, StandardCharsets.UTF_8));
  93. }
  94. @Override
  95. public List<Resource> resources() {
  96. return keys().stream().map(mappedFunction()).collect(Collectors.toList());
  97. }
  98. protected abstract Function<String, Resource> mappedFunction();
  99. protected abstract Set<String> keys();
  100. @Override
  101. public List<Resource> dirs() {
  102. return resources().stream().filter(Resource::isDirectory).collect(Collectors.toList());
  103. }
  104. @Override
  105. public List<Resource> files(String suffix) {
  106. return resources().stream().filter(it -> it.name().endsWith(suffix)).collect(Collectors.toList());
  107. }
  108. @Override
  109. public String getAbsolutePath() {
  110. return this.path;
  111. }
  112. @Override
  113. public void processExport(ZipOutputStream zos, String path, Resource directory, List<Resource> resources, List<String> excludes) throws IOException {
  114. for (Resource resource : resources) {
  115. String fullName = directory.getAbsolutePath();
  116. if (!fullName.endsWith(separator)) {
  117. fullName += separator;
  118. }
  119. fullName += resource.name();
  120. if (resource.isDirectory()) {
  121. fullName += separator;
  122. }
  123. if (fullName.equals(resource.getAbsolutePath()) && !excludes.contains(resource.name())) {
  124. if (resource.isDirectory()) {
  125. String newPath = path + resource.name() + "/";
  126. zos.putNextEntry(new ZipEntry(newPath));
  127. zos.closeEntry();
  128. processExport(zos, newPath, resource, resources, excludes);
  129. } else {
  130. zos.putNextEntry(new ZipEntry(path + resource.name()));
  131. zos.write(resource.read());
  132. zos.closeEntry();
  133. }
  134. }
  135. }
  136. }
  137. }