* follow symlinks implicitly

This commit is contained in:
Reinhard Pointner 2014-01-08 18:43:27 +00:00
parent 4354dc2fd6
commit d63fca6787
3 changed files with 66 additions and 82 deletions

View File

@ -1,29 +1,32 @@
package net.sourceforge.filebot;
import static java.nio.file.Files.*;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.AbstractMap;
import java.util.LinkedHashSet;
import java.util.Set;
public class MetaAttributeView extends AbstractMap<String, String> {
private final UserDefinedFileAttributeView attributeView;
private final Charset encoding;
public MetaAttributeView(File file) {
attributeView = Files.getFileAttributeView(file.toPath(), UserDefinedFileAttributeView.class);
encoding = Charset.forName("UTF-8");
public MetaAttributeView(File file) throws IOException {
Path path = file.toPath();
while (isSymbolicLink(path)) {
path = readSymbolicLink(path);
}
attributeView = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
encoding = Charset.forName("UTF-8");
}
@Override
public String get(Object key) {
@ -38,7 +41,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
@Override
public String put(String key, String value) {
try {
@ -53,7 +55,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
return null; // since we don't know the old value
}
@Override
public Set<Entry<String, String>> entrySet() {
try {
@ -67,7 +68,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
@Override
public void clear() {
try {
@ -79,7 +79,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
@Override
public int size() {
try {
@ -89,7 +88,6 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
@Override
public boolean isEmpty() {
try {
@ -99,35 +97,29 @@ public class MetaAttributeView extends AbstractMap<String, String> {
}
}
private class AttributeEntry implements Entry<String, String> {
private final String name;
public AttributeEntry(String name) {
this.name = name;
}
@Override
public String getKey() {
return name;
}
@Override
public String getValue() {
return get(name);
}
@Override
public String setValue(String value) {
return put(name, value);
}
@Override
public String toString() {
return getKey() + "=" + getValue();

View File

@ -1252,6 +1252,7 @@ public class MediaDetection {
public static void storeMetaInfo(File file, Object model) {
// only for Episode / Movie objects
if ((model instanceof Episode || model instanceof Movie) && file.exists()) {
try {
MetaAttributes xattr = new MetaAttributes(file);
// set creation date to episode / movie release date
@ -1280,7 +1281,9 @@ public class MediaDetection {
} catch (Exception e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning("Failed to set xattr: " + e.getMessage());
}
} catch (Throwable t) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning(t.toString());
}
}
}
}

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot.media;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@ -13,7 +11,6 @@ import net.sourceforge.filebot.MetaAttributeView;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
public class MetaAttributes {
private static final String FILENAME_KEY = "filename";
@ -22,18 +19,15 @@ public class MetaAttributes {
private final BasicFileAttributeView fileAttributeView;
private final MetaAttributeView metaAttributeView;
public MetaAttributes(File file) {
public MetaAttributes(File file) throws IOException {
this.metaAttributeView = new MetaAttributeView(file);
this.fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class);
}
public void setCreationDate(long millis) throws IOException {
fileAttributeView.setTimes(null, null, FileTime.fromMillis(millis));
}
public long getCreationDate(long time) {
try {
return fileAttributeView.readAttributes().creationTime().toMillis();
@ -42,27 +36,22 @@ public class MetaAttributes {
}
}
public void setOriginalName(String name) {
metaAttributeView.put(FILENAME_KEY, name);
}
public String getOriginalName() {
return metaAttributeView.get(FILENAME_KEY);
}
public void setObject(Object object) {
metaAttributeView.put(METADATA_KEY, JsonWriter.toJson(object));
}
public Object getObject() {
return JsonReader.toJava(metaAttributeView.get(METADATA_KEY));
}
public void clear() {
metaAttributeView.remove(FILENAME_KEY);
metaAttributeView.remove(METADATA_KEY);