When mime type is octet-stream, guess from the file name

This commit is contained in:
ByteHamster 2022-01-25 21:55:38 +01:00
parent 2524fa0f76
commit c376be44dd
4 changed files with 33 additions and 30 deletions

View File

@ -92,14 +92,11 @@ public class Atom extends Namespace {
Log.d(TAG, "Length attribute could not be parsed.");
}
String type = attributes.getValue(LINK_TYPE);
if (type == null) {
type = SyndTypeUtils.getMimeTypeFromUrl(href);
}
String mimeType = SyndTypeUtils.getMimeType(type, href);
FeedItem currItem = state.getCurrentItem();
if (SyndTypeUtils.enclosureTypeValid(type) && currItem != null && !currItem.hasMedia()) {
currItem.setMedia(new FeedMedia(currItem, href, size, type));
if (SyndTypeUtils.isMediaFile(mimeType) && currItem != null && !currItem.hasMedia()) {
currItem.setMedia(new FeedMedia(currItem, href, size, mimeType));
}
} else if (LINK_REL_PAYMENT.equals(rel)) {
state.getCurrentItem().setPaymentLink(href);

View File

@ -49,27 +49,23 @@ public class Media extends Namespace {
boolean validTypeMedia = false;
boolean validTypeImage = false;
boolean isDefault = "true".equals(defaultStr);
String guessedType = SyndTypeUtils.getMimeTypeFromUrl(url);
String mimeType = SyndTypeUtils.getMimeType(type, url);
if (MEDIUM_AUDIO.equals(medium)) {
validTypeMedia = true;
type = "audio/*";
mimeType = "audio/*";
} else if (MEDIUM_VIDEO.equals(medium)) {
validTypeMedia = true;
type = "video/*";
} else if (MEDIUM_IMAGE.equals(medium) && (guessedType == null
|| (!guessedType.startsWith("audio/") && !guessedType.startsWith("video/")))) {
mimeType = "video/*";
} else if (MEDIUM_IMAGE.equals(medium) && (mimeType == null
|| (!mimeType.startsWith("audio/") && !mimeType.startsWith("video/")))) {
// Apparently, some publishers explicitly specify the audio file as an image
validTypeImage = true;
type = "image/*";
mimeType = "image/*";
} else {
if (type == null) {
type = guessedType;
}
if (SyndTypeUtils.enclosureTypeValid(type)) {
if (SyndTypeUtils.isMediaFile(mimeType)) {
validTypeMedia = true;
} else if (SyndTypeUtils.imageTypeValid(type)) {
} else if (SyndTypeUtils.isImageFile(mimeType)) {
validTypeImage = true;
}
}
@ -94,7 +90,7 @@ public class Media extends Namespace {
Log.e(TAG, "Duration \"" + durationStr + "\" could not be parsed");
}
}
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, mimeType);
if (durationMs > 0) {
media.setDuration(durationMs);
}

View File

@ -48,16 +48,11 @@ public class Rss20 extends Namespace {
} else if (ENCLOSURE.equals(localName) && ITEM.equals(state.getTagstack().peek().getName())) {
String type = attributes.getValue(ENC_TYPE);
String url = attributes.getValue(ENC_URL);
boolean validType = SyndTypeUtils.enclosureTypeValid(type);
if (!validType) {
type = SyndTypeUtils.getMimeTypeFromUrl(url);
validType = SyndTypeUtils.enclosureTypeValid(type);
}
String mimeType = SyndTypeUtils.getMimeType(type, url);
boolean validUrl = !TextUtils.isEmpty(url);
if (state.getCurrentItem() != null && state.getCurrentItem().getMedia() == null
&& validType && validUrl) {
&& SyndTypeUtils.isMediaFile(mimeType) && validUrl) {
long size = 0;
try {
size = Long.parseLong(attributes.getValue(ENC_LEN));
@ -68,7 +63,7 @@ public class Rss20 extends Namespace {
} catch (NumberFormatException e) {
Log.d(TAG, "Length attribute could not be parsed.");
}
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, type);
FeedMedia media = new FeedMedia(state.getCurrentItem(), url, size, mimeType);
state.getCurrentItem().setMedia(media);
}
}

View File

@ -1,17 +1,32 @@
package de.danoeh.antennapod.parser.feed.util;
import android.webkit.MimeTypeMap;
import androidx.annotation.Nullable;
import org.apache.commons.io.FilenameUtils;
/**
* Utility class for handling MIME-Types of enclosures.
* */
public class SyndTypeUtils {
public static final String OCTET_STREAM = "application/octet-stream";
private SyndTypeUtils() {
}
public static boolean enclosureTypeValid(String type) {
@Nullable
public static String getMimeType(@Nullable String type, @Nullable String filename) {
if (isMediaFile(type) && !OCTET_STREAM.equals(type)) {
return type;
}
String filenameType = SyndTypeUtils.getMimeTypeFromUrl(filename);
if (isMediaFile(filenameType)) {
return filenameType;
}
return type;
}
public static boolean isMediaFile(String type) {
if (type == null) {
return false;
} else {
@ -22,7 +37,7 @@ public class SyndTypeUtils {
}
}
public static boolean imageTypeValid(String type) {
public static boolean isImageFile(String type) {
if (type == null) {
return false;
} else {
@ -34,7 +49,7 @@ public class SyndTypeUtils {
* Should be used if mime-type of enclosure tag is not supported. This
* method will return the mime-type of the file extension.
*/
public static String getMimeTypeFromUrl(String url) {
private static String getMimeTypeFromUrl(String url) {
if (url == null) {
return null;
}