2018-12-27 06:51:24 -05:00
|
|
|
package net.filebot.util;
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.*;
|
|
|
|
import static java.util.Collections.*;
|
|
|
|
import static java.util.stream.Collectors.*;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.NoSuchFileException;
|
|
|
|
import java.nio.file.Path;
|
2018-12-31 01:02:51 -05:00
|
|
|
import java.nio.file.attribute.DosFileAttributeView;
|
2018-12-27 06:51:24 -05:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class PlainFileXattrView implements XattrView {
|
|
|
|
|
2018-12-27 07:00:42 -05:00
|
|
|
private static final String XATTR_FOLDER = System.getProperty("net.filebot.xattr.store", ".xattr");
|
2018-12-27 06:51:24 -05:00
|
|
|
|
|
|
|
private final Path folder;
|
|
|
|
|
|
|
|
public PlainFileXattrView(Path path) throws IOException {
|
|
|
|
folder = path.getParent().resolve(XATTR_FOLDER).resolve(path.getFileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<String> list() throws IOException {
|
|
|
|
if (Files.isDirectory(folder)) {
|
|
|
|
return Files.list(folder).map(Path::getFileName).map(Path::toString).collect(toList());
|
|
|
|
}
|
|
|
|
return emptyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String read(String key) throws IOException {
|
|
|
|
try {
|
|
|
|
return new String(Files.readAllBytes(folder.resolve(key)), UTF_8);
|
|
|
|
} catch (NoSuchFileException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(String key, String value) throws IOException {
|
2018-12-31 01:02:51 -05:00
|
|
|
if (!Files.isDirectory(folder)) {
|
|
|
|
Files.createDirectories(folder);
|
|
|
|
|
|
|
|
// set Hidden on Windows
|
|
|
|
if (Files.getFileStore(folder).supportsFileAttributeView(DosFileAttributeView.class)) {
|
|
|
|
Files.getFileAttributeView(folder, DosFileAttributeView.class).setHidden(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 06:51:24 -05:00
|
|
|
Files.write(folder.resolve(key), value.getBytes(UTF_8));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void delete(String key) throws IOException {
|
|
|
|
Files.deleteIfExists(folder.resolve(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|