1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.common.content;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.channels.ReadableByteChannel;
26 import java.nio.channels.SeekableByteChannel;
27 import java.nio.file.CopyOption;
28 import java.nio.file.FileSystem;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.StandardCopyOption;
32 import java.nio.file.StandardOpenOption;
33 import java.nio.file.attribute.BasicFileAttributes;
34 import java.util.Objects;
35
36 import org.apache.logging.log4j.LogManager;
37 import org.apache.logging.log4j.Logger;
38 import org.mycore.datamodel.niofs.MCRFileAttributes;
39
40
41
42
43
44
45 public class MCRPathContent extends MCRContent implements MCRSeekableChannelContent {
46
47 private static final Logger LOGGER = LogManager.getLogger();
48
49 private Path path;
50
51 private BasicFileAttributes attrs;
52
53 private static int BUFFER_SIZE = 8192;
54
55 public MCRPathContent(Path path) {
56 this(path, null);
57 }
58
59 public MCRPathContent(Path path, BasicFileAttributes attrs) {
60 this.path = Objects.requireNonNull(path).toAbsolutePath().normalize();
61 this.attrs = attrs;
62 }
63
64
65
66
67 @Override
68 public SeekableByteChannel getSeekableByteChannel() throws IOException {
69 return Files.newByteChannel(path, StandardOpenOption.READ);
70 }
71
72 @Override
73 public InputStream getInputStream() throws IOException {
74 return Files.newInputStream(path, StandardOpenOption.READ);
75 }
76
77 @Override
78 public ReadableByteChannel getReadableByteChannel() throws IOException {
79 return getSeekableByteChannel();
80 }
81
82 @Override
83 public byte[] asByteArray() throws IOException {
84 return Files.readAllBytes(path);
85 }
86
87 @Override
88 public long length() throws IOException {
89 return attrs != null ? attrs.size() : Files.size(path);
90 }
91
92 @Override
93 public long lastModified() throws IOException {
94 return (attrs != null ? attrs.lastModifiedTime() : Files.getLastModifiedTime(path)).toMillis();
95 }
96
97 @Override
98 public String getETag() throws IOException {
99 if (attrs instanceof MCRFileAttributes) {
100 return ((MCRFileAttributes) attrs).md5sum();
101 }
102
103 if (Files.getFileStore(path).supportsFileAttributeView("md5")) {
104 Object fileKey = Files.getAttribute(path, "md5:md5");
105 if (fileKey instanceof String) {
106 return fileKey.toString();
107 }
108 }
109
110 return super.getETag();
111 }
112
113 @Override
114 public String getMimeType() throws IOException {
115 return mimeType == null ? Files.probeContentType(path) : mimeType;
116 }
117
118 @Override
119 public String getSystemId() {
120 return path.toUri().toString();
121 }
122
123 @Override
124 public String getName() {
125 return name == null ? path.getFileName().toString() : super.getName();
126 }
127
128 @Override
129 public void sendTo(OutputStream out) throws IOException {
130 Files.copy(path, out);
131 }
132
133 @Override
134 public void sendTo(File target) throws IOException {
135 Files.copy(path, target.toPath(), StandardCopyOption.REPLACE_EXISTING);
136 }
137
138 @Override
139 public void sendTo(Path target, CopyOption... options) throws IOException {
140 Files.copy(path, target, options);
141 }
142 }