1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.ifs2;
20
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.StandardCopyOption;
25 import java.nio.file.attribute.BasicFileAttributes;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.jdom2.Element;
31 import org.mycore.common.content.MCRContent;
32 import org.mycore.common.content.streams.MCRDevNull;
33 import org.mycore.datamodel.ifs.MCRContentInputStream;
34 import org.mycore.datamodel.niofs.MCRFileAttributes;
35
36
37
38
39
40
41
42
43 public class MCRFile extends MCRStoredNode {
44
45 private static final MCRDevNull DEV_NULL = new MCRDevNull();
46
47 private static final Logger LOGGER = LogManager.getLogger(MCRFile.class);
48
49
50
51
52 public static final String MD5_OF_EMPTY_FILE = "d41d8cd98f00b204e9800998ecf8427e";
53
54
55
56
57
58
59
60
61
62
63 protected MCRFile(MCRDirectory parent, Path fo, Element data) {
64 super(parent, fo, data);
65 }
66
67
68
69
70
71
72
73
74
75 protected MCRFile(MCRDirectory parent, String name) throws IOException {
76 super(parent, name, "file");
77 Files.createFile(path);
78 writeData(e -> e.setAttribute("md5", MCRFile.MD5_OF_EMPTY_FILE));
79 getRoot().saveAdditionalData();
80 }
81
82
83
84
85
86 @Override
87 protected MCRVirtualNode buildChildNode(Path fo) {
88 return null;
89 }
90
91
92
93
94
95
96 public String getMD5() {
97 return readData(e -> e.getAttributeValue("md5"));
98 }
99
100
101
102
103
104
105
106
107 public String getExtension() {
108 String name = getName();
109 int pos = name.lastIndexOf(".");
110 return pos == -1 ? "" : name.substring(pos + 1);
111 }
112
113
114
115
116
117
118
119
120 public String setContent(MCRContent source) throws IOException {
121 try (MCRContentInputStream cis = source.getContentInputStream()) {
122 source.sendTo(path, StandardCopyOption.REPLACE_EXISTING);
123 String md5 = cis.getMD5String();
124 writeData(e -> e.setAttribute("md5", md5));
125 getRoot().saveAdditionalData();
126 return md5;
127 }
128 }
129
130
131
132
133
134
135
136
137 public void setMD5(String md5) throws IOException {
138 writeData(e -> e.setAttribute("md5", md5));
139 getRoot().saveAdditionalData();
140 }
141
142
143
144
145 @Override
146 void repairMetadata() throws IOException {
147 MCRContentInputStream cis = getContent().getContentInputStream();
148 IOUtils.copy(cis, DEV_NULL);
149 cis.close();
150 String path = getPath();
151 writeData(e -> {
152 e.setName("file");
153 e.setAttribute("name", getName());
154 e.removeChildren("file");
155 e.removeChildren("directory");
156 String md5 = cis.getMD5String();
157 if (!md5.equals(e.getAttributeValue("md5"))) {
158 LOGGER.warn("Fixed MD5 of {} to {}", path, md5);
159 e.setAttribute("md5", md5);
160 }
161 });
162 }
163
164 @Override
165 public MCRFileAttributes<String> getBasicFileAttributes() throws IOException {
166 BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
167 return MCRFileAttributes.fromAttributes(attrs, getMD5());
168 }
169 }