Check file extension if mime-type is not supported

This commit is contained in:
daniel oeh 2012-10-13 11:44:55 +02:00
parent 1f214573e9
commit 0b22262186
3 changed files with 55 additions and 14 deletions

View File

@ -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);
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);
}

View File

@ -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)) {

View File

@ -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;
}
}