1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.mycore.restapi.v1.utils;
23
24 import static org.mycore.datamodel.niofs.MCRAbstractFileSystem.SEPARATOR;
25 import static org.mycore.datamodel.niofs.MCRAbstractFileSystem.SEPARATOR_STRING;
26
27 import java.io.IOException;
28 import java.nio.file.FileVisitResult;
29 import java.nio.file.Path;
30 import java.nio.file.SimpleFileVisitor;
31 import java.nio.file.attribute.BasicFileAttributes;
32
33 import org.mycore.datamodel.metadata.MCRObjectID;
34 import org.mycore.datamodel.niofs.MCRContentTypes;
35 import org.mycore.datamodel.niofs.MCRFileAttributes;
36 import org.mycore.datamodel.niofs.MCRPath;
37 import org.mycore.frontend.filter.MCRSecureTokenV2FilterConfig;
38 import org.mycore.frontend.jersey.MCRJerseyUtil;
39
40 import com.google.gson.stream.JsonWriter;
41
42 import jakarta.ws.rs.core.Application;
43 import jakarta.ws.rs.core.UriInfo;
44
45
46
47
48
49 public class MCRJSONFileVisitor extends SimpleFileVisitor<Path> {
50
51 private JsonWriter jw;
52
53 private String baseURL;
54
55 private String objId;
56
57 private String derId;
58
59 public MCRJSONFileVisitor(JsonWriter jw, MCRObjectID objId, MCRObjectID derId, UriInfo info, Application app) {
60 super();
61 this.jw = jw;
62 this.baseURL = MCRJerseyUtil.getBaseURL(info, app);
63 this.objId = objId.toString();
64 this.derId = derId.toString();
65 }
66
67 @Override
68 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
69 jw.beginObject();
70 writePathInfo(dir, attrs);
71 jw.name("children").beginArray();
72 return super.preVisitDirectory(dir, attrs);
73 }
74
75 private void writePathInfo(Path path, BasicFileAttributes attrs) throws IOException {
76 MCRPath mcrPath = MCRPath.toMCRPath(path);
77 MCRPath relativePath = mcrPath.getRoot().relativize(mcrPath);
78 boolean isRoot = mcrPath.getNameCount() == 0;
79 jw.name("type").value(attrs.isDirectory() ? "directory" : "file");
80 if (isRoot) {
81 jw.name("mycoreobject").value(objId);
82 jw.name("mycorederivate").value(mcrPath.getOwner());
83 }
84 jw.name("name").value(isRoot ? "" : mcrPath.getFileName().toString());
85 jw.name("path")
86 .value(attrs.isDirectory() ? toStringValue(relativePath) : SEPARATOR_STRING + relativePath);
87 if (!isRoot) {
88 jw.name("parentPath").value(toStringValue(relativePath.getParent()));
89 }
90 addBasicAttributes(path, attrs);
91 }
92
93 private void addBasicAttributes(Path path, BasicFileAttributes attrs) throws IOException {
94 jw.name("size").value(attrs.size());
95 jw.name("time").beginObject();
96 jw.name("created").value(attrs.creationTime().toString());
97 jw.name("modified").value(attrs.lastModifiedTime().toString());
98 jw.name("accessed").value(attrs.lastAccessTime().toString());
99 jw.endObject();
100 if (attrs.isRegularFile()) {
101 jw.name("contentType").value(MCRContentTypes.probeContentType(path));
102 if (attrs instanceof MCRFileAttributes) {
103 jw.name("md5").value(((MCRFileAttributes<?>) attrs).md5sum());
104 }
105 }
106 }
107
108 @Override
109 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
110 MCRPath mcrPath = MCRPath.toMCRPath(file);
111 MCRPath relativePath = mcrPath.getRoot().relativize(mcrPath);
112 jw.beginObject();
113 writePathInfo(file, attrs);
114 jw.name("extension").value(getFileExtension(file.getFileName().toString()));
115 jw.name("href").value(MCRSecureTokenV2FilterConfig.getFileNodeServletSecured(MCRObjectID.getInstance(derId),
116 relativePath.toString(), this.baseURL));
117 jw.endObject();
118 return super.visitFile(file, attrs);
119 }
120
121 private static String getFileExtension(String fileName) {
122 if (fileName.endsWith(".")) {
123 return "";
124 }
125 int pos = fileName.lastIndexOf(".");
126 return pos == -1 ? "" : fileName.substring(pos + 1);
127 }
128
129 @Override
130 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
131 jw.endArray();
132 jw.endObject();
133 return super.postVisitDirectory(dir, exc);
134 }
135
136 private static String toStringValue(MCRPath relativePath) {
137 if (relativePath == null) {
138 return SEPARATOR_STRING;
139 }
140 String pathString = relativePath.toString();
141 if (pathString.isEmpty()) {
142 return SEPARATOR_STRING;
143 }
144 if (pathString.equals(SEPARATOR_STRING)) {
145 return pathString;
146 }
147 return SEPARATOR + pathString + SEPARATOR;
148 }
149
150 }