* 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,44 +1,46 @@
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);
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) {
try {
ByteBuffer buffer = ByteBuffer.allocate(attributeView.size(key.toString()));
attributeView.read(key.toString(), buffer);
buffer.flip();
return encoding.decode(buffer).toString();
} catch (IOException e) {
return null;
}
}
@Override
public String put(String key, String value) {
try {
@ -52,8 +54,7 @@ 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 {
@ -66,8 +67,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
throw new IllegalStateException(e);
}
}
@Override
public void clear() {
try {
@ -78,8 +78,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
throw new IllegalStateException(e);
}
}
@Override
public int size() {
try {
@ -88,8 +87,7 @@ public class MetaAttributeView extends AbstractMap<String, String> {
throw new IllegalStateException(e);
}
}
@Override
public boolean isEmpty() {
try {
@ -98,40 +96,34 @@ public class MetaAttributeView extends AbstractMap<String, String> {
throw new IllegalStateException(e);
}
}
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,35 +1252,38 @@ 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()) {
MetaAttributes xattr = new MetaAttributes(file);
// set creation date to episode / movie release date
try {
if (model instanceof Episode) {
Episode episode = (Episode) model;
if (episode.getAirdate() != null) {
xattr.setCreationDate(episode.getAirdate().getTimeStamp());
}
} else if (model instanceof Movie) {
Movie movie = (Movie) model;
if (movie.getYear() > 0) {
xattr.setCreationDate(new Date(movie.getYear(), 1, 1).getTimeStamp());
}
}
} catch (Exception e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning("Failed to set creation date: " + e.getMessage());
}
MetaAttributes xattr = new MetaAttributes(file);
// store original name and model as xattr
try {
xattr.setObject(model);
if (xattr.getOriginalName() == null) {
xattr.setOriginalName(file.getName());
// set creation date to episode / movie release date
try {
if (model instanceof Episode) {
Episode episode = (Episode) model;
if (episode.getAirdate() != null) {
xattr.setCreationDate(episode.getAirdate().getTimeStamp());
}
} else if (model instanceof Movie) {
Movie movie = (Movie) model;
if (movie.getYear() > 0) {
xattr.setCreationDate(new Date(movie.getYear(), 1, 1).getTimeStamp());
}
}
} catch (Exception e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning("Failed to set creation date: " + e.getMessage());
}
} catch (Exception e) {
Logger.getLogger(MediaDetection.class.getClass().getName()).warning("Failed to set xattr: " + e.getMessage());
}
// store original name and model as xattr
try {
xattr.setObject(model);
if (xattr.getOriginalName() == null) {
xattr.setOriginalName(file.getName());
}
} 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,27 +11,23 @@ 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";
private static final String METADATA_KEY = "metadata";
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();
@ -41,31 +35,26 @@ public class MetaAttributes {
return 0;
}
}
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);
}
}