diff --git a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java b/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java index f9963133b..b97fd49b8 100644 --- a/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java +++ b/src/de/danoeh/antennapod/syndication/namespace/atom/NSAtom.java @@ -10,6 +10,7 @@ import de.danoeh.antennapod.syndication.namespace.Namespace; import de.danoeh.antennapod.syndication.namespace.SyndElement; import de.danoeh.antennapod.syndication.namespace.rss20.NSRSS20; import de.danoeh.antennapod.syndication.util.SyndDateUtils; +import de.danoeh.antennapod.syndication.util.SyndTypeUtils; public class NSAtom extends Namespace { private static final String TAG = "NSAtom"; @@ -72,10 +73,13 @@ public class NSAtom extends Namespace { if (strSize != null) size = Long.parseLong(strSize); String type = attributes.getValue(LINK_TYPE); - - state.getCurrentItem().setMedia( - new FeedMedia(state.getCurrentItem(), href, - size, type)); + if (SyndTypeUtils.typeValid(type) + || (type = SyndTypeUtils + .getValidMimeTypeFromUrl(href)) != null) { + state.getCurrentItem().setMedia( + new FeedMedia(state.getCurrentItem(), href, + size, type)); + } } else if (rel.equals(LINK_REL_PAYMENT)) { state.getCurrentItem().setPaymentLink(href); } diff --git a/src/de/danoeh/antennapod/syndication/namespace/rss20/NSRSS20.java b/src/de/danoeh/antennapod/syndication/namespace/rss20/NSRSS20.java index 86369acf2..46fe03bcf 100644 --- a/src/de/danoeh/antennapod/syndication/namespace/rss20/NSRSS20.java +++ b/src/de/danoeh/antennapod/syndication/namespace/rss20/NSRSS20.java @@ -11,6 +11,7 @@ import de.danoeh.antennapod.syndication.handler.HandlerState; import de.danoeh.antennapod.syndication.namespace.Namespace; import de.danoeh.antennapod.syndication.namespace.SyndElement; import de.danoeh.antennapod.syndication.util.SyndDateUtils; +import de.danoeh.antennapod.syndication.util.SyndTypeUtils; /** * SAX-Parser for reading RSS-Feeds @@ -39,8 +40,6 @@ public class NSRSS20 extends Namespace { public final static String ENC_LEN = "length"; public final static String ENC_TYPE = "type"; - public final static String VALID_MIMETYPE = "audio/.*" + "|" + "video/.*" + "|" + "application/ogg"; - @Override public SyndElement handleElementStart(String localName, HandlerState state, Attributes attributes) { @@ -51,19 +50,20 @@ public class NSRSS20 extends Namespace { } else if (localName.equals(ENCLOSURE)) { String type = attributes.getValue(ENC_TYPE); + String url = attributes.getValue(ENC_URL); if (state.getCurrentItem().getMedia() == null - && (type.matches(VALID_MIMETYPE))) { + && (SyndTypeUtils.typeValid(type) || ((type = SyndTypeUtils + .getValidMimeTypeFromUrl(url)) != null))) { + long size = 0; try { - size = Long.parseLong(attributes - .getValue(ENC_LEN)); + size = Long.parseLong(attributes.getValue(ENC_LEN)); } catch (NumberFormatException e) { - if (AppConfig.DEBUG) Log.d(TAG, "Length attribute could not be parsed."); + if (AppConfig.DEBUG) + Log.d(TAG, "Length attribute could not be parsed."); } state.getCurrentItem().setMedia( - new FeedMedia(state.getCurrentItem(), attributes - .getValue(ENC_URL), size, attributes - .getValue(ENC_TYPE))); + new FeedMedia(state.getCurrentItem(), url, size, type)); } } else if (localName.equals(IMAGE)) { @@ -83,7 +83,7 @@ public class NSRSS20 extends Namespace { String top = topElement.getName(); SyndElement secondElement = state.getSecondTag(); String second = secondElement.getName(); - + if (top.equals(GUID) && second.equals(ITEM)) { state.getCurrentItem().setItemIdentifier(content); } else if (top.equals(TITLE)) { diff --git a/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java b/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java new file mode 100644 index 000000000..5e9494c91 --- /dev/null +++ b/src/de/danoeh/antennapod/syndication/util/SyndTypeUtils.java @@ -0,0 +1,37 @@ +package de.danoeh.antennapod.syndication.util; + +import org.apache.commons.io.FilenameUtils; + +import android.webkit.MimeTypeMap; + +/** Utility class for handling MIME-Types of enclosures */ +public class SyndTypeUtils { + + private final static String VALID_MIMETYPE = "audio/.*" + "|" + "video/.*" + + "|" + "application/ogg"; + + private SyndTypeUtils() { + + } + + public static boolean typeValid(String type) { + return type.matches(VALID_MIMETYPE); + } + + /** + * Should be used if mime-type of enclosure tag is not supported. This + * method will check if the mime-type of the file extension is supported. If + * the type is not supported, this method will return null. + */ + public static String getValidMimeTypeFromUrl(String url) { + String extension = FilenameUtils.getExtension(url); + if (extension != null) { + String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension( + extension); + if (typeValid(type)) { + return type; + } + } + return null; + } +}