From 7ab9bb779cb72048f672b5b3be82b7400412966a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 4 Aug 2018 17:48:41 +0700 Subject: [PATCH] Refactor local datasources (exif, xattr, file) --- source/net/filebot/format/MediaBindingBean.java | 13 +++++++++---- source/net/filebot/media/ImageMetadata.java | 5 +++++ source/net/filebot/ui/rename/FileNameFormat.java | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 44d0713e..28c3ba84 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -223,7 +223,7 @@ public class MediaBindingBean { // try EXIF Date-Taken for image files or File Last-Modified for generic files try { - return new ImageMetadata(f).getDateTaken().get(); + return getPhoto().getDateTaken().get(); } catch (Exception e) { // ignore and default to file creation date } @@ -895,17 +895,17 @@ public class MediaBindingBean { @Define("exif") public AssociativeScriptObject getImageMetadata() throws Exception { - return new AssociativeScriptObject(new ImageMetadata(getMediaFile()).snapshot()); + return new AssociativeScriptObject(getPhoto().snapshot()); } @Define("camera") public AssociativeEnumObject getCamera() throws Exception { - return new ImageMetadata(getMediaFile()).getCameraModel().map(AssociativeEnumObject::new).orElse(null); + return getPhoto().getCameraModel().map(AssociativeEnumObject::new).orElse(null); } @Define("location") public AssociativeEnumObject getLocation() throws Exception { - return new ImageMetadata(getMediaFile()).getLocationTaken().map(AssociativeEnumObject::new).orElse(null); + return getPhoto().getLocationTaken().map(AssociativeEnumObject::new).orElse(null); } @Define("artist") @@ -943,6 +943,11 @@ public class MediaBindingBean { return (AudioTrack) infoObject; } + @Define("photo") + public ImageMetadata getPhoto() throws Exception { + return new ImageMetadata((File) infoObject); + } + @Define("pi") public Integer getPart() { if (infoObject instanceof AudioTrack) diff --git a/source/net/filebot/media/ImageMetadata.java b/source/net/filebot/media/ImageMetadata.java index d1f93526..9ac6c332 100644 --- a/source/net/filebot/media/ImageMetadata.java +++ b/source/net/filebot/media/ImageMetadata.java @@ -27,6 +27,7 @@ import com.drew.metadata.Tag; import com.drew.metadata.exif.ExifIFD0Directory; import com.drew.metadata.exif.ExifSubIFDDirectory; import com.drew.metadata.exif.GpsDirectory; +import com.drew.metadata.file.FileSystemDirectory; import net.filebot.Cache; import net.filebot.CacheType; @@ -69,6 +70,10 @@ public class ImageMetadata { return values; } + public Optional getName() { + return extract(m -> m.getFirstDirectoryOfType(FileSystemDirectory.class)).map(d -> d.getString(FileSystemDirectory.TAG_FILE_NAME)); + } + public Optional getDateTaken() { return extract(m -> m.getFirstDirectoryOfType(ExifIFD0Directory.class)).map(d -> d.getDate(ExifSubIFDDirectory.TAG_DATETIME)).map(d -> { return d.toInstant().atZone(ZoneOffset.UTC); diff --git a/source/net/filebot/ui/rename/FileNameFormat.java b/source/net/filebot/ui/rename/FileNameFormat.java index 9b017151..4253d491 100644 --- a/source/net/filebot/ui/rename/FileNameFormat.java +++ b/source/net/filebot/ui/rename/FileNameFormat.java @@ -10,8 +10,8 @@ import net.filebot.util.FileUtilities; public class FileNameFormat extends Format { @Override - public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { - return toAppendTo.append(FileUtilities.getName((File) obj)); + public StringBuffer format(Object obj, StringBuffer sb, FieldPosition pos) { + return sb.append(FileUtilities.getName((File) obj)); } @Override