1 package org
.argeo
.cms
.gcr
.fs
;
3 import java
.io
.IOException
;
4 import java
.nio
.charset
.StandardCharsets
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.nio
.file
.attribute
.FileTime
;
8 import java
.nio
.file
.attribute
.UserDefinedFileAttributeView
;
9 import java
.time
.Instant
;
10 import java
.util
.Arrays
;
11 import java
.util
.Collections
;
12 import java
.util
.HashSet
;
13 import java
.util
.Iterator
;
16 import org
.argeo
.api
.gcr
.AbstractContent
;
17 import org
.argeo
.api
.gcr
.Content
;
18 import org
.argeo
.api
.gcr
.ContentResourceException
;
19 import org
.argeo
.api
.gcr
.ContentSession
;
21 public class FsContent
extends AbstractContent
implements Content
{
22 private static final Set
<String
> BASIC_KEYS
= new HashSet
<>(
23 Arrays
.asList("basic:creationTime", "basic:lastModifiedTime", "basic:size", "basic:fileKey"));
24 private static final Set
<String
> POSIX_KEYS
;
26 POSIX_KEYS
= new HashSet
<>(BASIC_KEYS
);
27 POSIX_KEYS
.add("owner:owner");
28 POSIX_KEYS
.add("posix:group");
29 POSIX_KEYS
.add("posix:permissions");
32 private FsContentSession contentSession
;
33 private final Path path
;
35 public FsContent(FsContentSession contentSession
, Path path
) {
37 this.contentSession
= contentSession
;
41 private boolean isPosix() {
42 return path
.getFileSystem().supportedFileAttributeViews().contains("posix");
46 public Iterator
<Content
> iterator() {
47 if (Files
.isDirectory(path
)) {
49 return Files
.list(path
).map((p
) -> (Content
) new FsContent(contentSession
, p
)).iterator();
50 } catch (IOException e
) {
51 throw new ContentResourceException("Cannot list " + path
, e
);
54 return Collections
.emptyIterator();
59 public String
getName() {
60 return path
.getFileName().toString();
64 public <A
> A
get(String key
, Class
<A
> clss
) {
67 value
= Files
.getAttribute(path
, key
);
68 } catch (IOException e
) {
69 throw new ContentResourceException("Cannot retrieve attribute " + key
+ " for " + path
, e
);
71 if (value
instanceof FileTime
) {
72 if (clss
.isAssignableFrom(FileTime
.class))
74 Instant instant
= ((FileTime
) value
).toInstant();
75 if (Object
.class.isAssignableFrom(clss
)) {// plain object requested
78 // TODO perform trivial file conversion to other formats
80 if (value
instanceof byte[]) {
81 return (A
) new String((byte[]) value
, StandardCharsets
.UTF_8
);
87 public ContentSession
getSession() {
88 return contentSession
;
92 protected Iterable
<String
> keys() {
93 Set
<String
> result
= new HashSet
<>(isPosix() ? POSIX_KEYS
: BASIC_KEYS
);
94 UserDefinedFileAttributeView udfav
= Files
.getFileAttributeView(path
, UserDefinedFileAttributeView
.class);
97 for (String name
: udfav
.list()) {
98 result
.add("user:" + name
);
100 } catch (IOException e
) {
101 throw new ContentResourceException("Cannot liast attributes for " + path
, e
);