Merge pull request #4306 from ByteHamster/mime-type-media-content

Fixed mime type in media:content
This commit is contained in:
H. Lehmann 2020-07-22 17:55:04 +02:00 committed by GitHub
commit 03190dfa20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 97 deletions

View File

@ -2,6 +2,8 @@ package de.test.antennapod.handler;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.syndication.namespace.NSMedia;
import de.test.antennapod.util.syndication.feedgenerator.Rss2Generator; import de.test.antennapod.util.syndication.feedgenerator.Rss2Generator;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
@ -39,4 +41,23 @@ public class RssParserTest extends FeedParserTestBase {
}, "UTF-8", 0); }, "UTF-8", 0);
assertEquals(image, f2.getImageUrl()); assertEquals(image, f2.getImageUrl());
} }
@Test
public void testMediaContentMime() throws Exception {
Feed f1 = createTestFeed(0, false);
f1.setImageUrl(null);
Feed f2 = runFeedTest(f1, new Rss2Generator() {
@Override
protected void writeAdditionalAttributes(XmlSerializer xml) throws IOException {
xml.setPrefix(NSMedia.NSTAG, NSMedia.NSURI);
xml.startTag(null, "item");
xml.startTag(NSMedia.NSURI, "content");
xml.attribute(null, "url", "https://www.example.com/file.mp4");
xml.attribute(null, "medium", "video");
xml.endTag(NSMedia.NSURI, "content");
xml.endTag(null, "item");
}
}, "UTF-8", 0);
assertEquals(MediaType.VIDEO, f2.getItems().get(0).getMedia().getMediaType());
}
} }

View File

@ -14,114 +14,118 @@ import de.danoeh.antennapod.core.syndication.util.SyndTypeUtils;
/** Processes tags from the http://search.yahoo.com/mrss/ namespace. */ /** Processes tags from the http://search.yahoo.com/mrss/ namespace. */
public class NSMedia extends Namespace { public class NSMedia extends Namespace {
private static final String TAG = "NSMedia"; private static final String TAG = "NSMedia";
public static final String NSTAG = "media"; public static final String NSTAG = "media";
public static final String NSURI = "http://search.yahoo.com/mrss/"; public static final String NSURI = "http://search.yahoo.com/mrss/";
private static final String CONTENT = "content"; private static final String CONTENT = "content";
private static final String DOWNLOAD_URL = "url"; private static final String DOWNLOAD_URL = "url";
private static final String SIZE = "fileSize"; private static final String SIZE = "fileSize";
private static final String MIME_TYPE = "type"; private static final String MIME_TYPE = "type";
private static final String DURATION = "duration"; private static final String DURATION = "duration";
private static final String DEFAULT = "isDefault"; private static final String DEFAULT = "isDefault";
private static final String MEDIUM = "medium"; private static final String MEDIUM = "medium";
private static final String MEDIUM_IMAGE = "image"; private static final String MEDIUM_IMAGE = "image";
private static final String MEDIUM_AUDIO = "audio"; private static final String MEDIUM_AUDIO = "audio";
private static final String MEDIUM_VIDEO = "video"; private static final String MEDIUM_VIDEO = "video";
private static final String IMAGE = "thumbnail"; private static final String IMAGE = "thumbnail";
private static final String IMAGE_URL = "url"; private static final String IMAGE_URL = "url";
private static final String DESCRIPTION = "description"; private static final String DESCRIPTION = "description";
private static final String DESCRIPTION_TYPE = "type"; private static final String DESCRIPTION_TYPE = "type";
@Override @Override
public SyndElement handleElementStart(String localName, HandlerState state, public SyndElement handleElementStart(String localName, HandlerState state,
Attributes attributes) { Attributes attributes) {
if (CONTENT.equals(localName)) { if (CONTENT.equals(localName)) {
String url = attributes.getValue(DOWNLOAD_URL); String url = attributes.getValue(DOWNLOAD_URL);
String type = attributes.getValue(MIME_TYPE); String type = attributes.getValue(MIME_TYPE);
String defaultStr = attributes.getValue(DEFAULT); String defaultStr = attributes.getValue(DEFAULT);
String medium = attributes.getValue(MEDIUM); String medium = attributes.getValue(MEDIUM);
boolean validTypeMedia = false; boolean validTypeMedia = false;
boolean validTypeImage = false; boolean validTypeImage = false;
boolean isDefault = "true".equals(defaultStr); boolean isDefault = "true".equals(defaultStr);
if (MEDIUM_AUDIO.equals(medium) || MEDIUM_VIDEO.equals(medium)) { if (MEDIUM_AUDIO.equals(medium)) {
validTypeMedia = true; validTypeMedia = true;
} else if (MEDIUM_IMAGE.equals(medium)) { type = "audio/*";
validTypeImage = true; } else if (MEDIUM_VIDEO.equals(medium)) {
} else { validTypeMedia = true;
if (type == null) { type = "video/*";
type = SyndTypeUtils.getMimeTypeFromUrl(url); } else if (MEDIUM_IMAGE.equals(medium)) {
} validTypeImage = true;
type = "image/*";
} else {
if (type == null) {
type = SyndTypeUtils.getMimeTypeFromUrl(url);
}
if (SyndTypeUtils.enclosureTypeValid(type)) { if (SyndTypeUtils.enclosureTypeValid(type)) {
validTypeMedia = true; validTypeMedia = true;
} else if (SyndTypeUtils.imageTypeValid(type)) { } else if (SyndTypeUtils.imageTypeValid(type)) {
validTypeImage = true; validTypeImage = true;
} }
} }
if (state.getCurrentItem() != null && if (state.getCurrentItem() != null && (state.getCurrentItem().getMedia() == null || isDefault)
(state.getCurrentItem().getMedia() == null || isDefault) && && url != null && validTypeMedia) {
url != null && validTypeMedia) { long size = 0;
long size = 0; String sizeStr = attributes.getValue(SIZE);
String sizeStr = attributes.getValue(SIZE); try {
try { size = Long.parseLong(sizeStr);
size = Long.parseLong(sizeStr); } catch (NumberFormatException e) {
} catch (NumberFormatException e) { Log.e(TAG, "Size \"" + sizeStr + "\" could not be parsed.");
Log.e(TAG, "Size \"" + sizeStr + "\" could not be parsed."); }
}
int durationMs = 0; int durationMs = 0;
String durationStr = attributes.getValue(DURATION); String durationStr = attributes.getValue(DURATION);
if (!TextUtils.isEmpty(durationStr)) { if (!TextUtils.isEmpty(durationStr)) {
try { try {
long duration = Long.parseLong(durationStr); long duration = Long.parseLong(durationStr);
durationMs = (int) TimeUnit.MILLISECONDS.convert(duration, TimeUnit.SECONDS); durationMs = (int) TimeUnit.MILLISECONDS.convert(duration, TimeUnit.SECONDS);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(TAG, "Duration \"" + durationStr + "\" could not be parsed"); 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, type);
if (durationMs > 0) { if (durationMs > 0) {
media.setDuration(durationMs); media.setDuration(durationMs);
} }
state.getCurrentItem().setMedia(media); state.getCurrentItem().setMedia(media);
} else if (state.getCurrentItem() != null && url != null && validTypeImage) { } else if (state.getCurrentItem() != null && url != null && validTypeImage) {
state.getCurrentItem().setImageUrl(url); state.getCurrentItem().setImageUrl(url);
} }
} else if (IMAGE.equals(localName)) { } else if (IMAGE.equals(localName)) {
String url = attributes.getValue(IMAGE_URL); String url = attributes.getValue(IMAGE_URL);
if (url != null) { if (url != null) {
if (state.getCurrentItem() != null) { if (state.getCurrentItem() != null) {
state.getCurrentItem().setImageUrl(url); state.getCurrentItem().setImageUrl(url);
} else { } else {
if (state.getFeed().getImageUrl() == null) { if (state.getFeed().getImageUrl() == null) {
state.getFeed().setImageUrl(url); state.getFeed().setImageUrl(url);
} }
} }
} }
} else if (DESCRIPTION.equals(localName)) { } else if (DESCRIPTION.equals(localName)) {
String type = attributes.getValue(DESCRIPTION_TYPE); String type = attributes.getValue(DESCRIPTION_TYPE);
return new AtomText(localName, this, type); return new AtomText(localName, this, type);
} }
return new SyndElement(localName, this); return new SyndElement(localName, this);
} }
@Override @Override
public void handleElementEnd(String localName, HandlerState state) { public void handleElementEnd(String localName, HandlerState state) {
if (DESCRIPTION.equals(localName)) { if (DESCRIPTION.equals(localName)) {
String content = state.getContentBuf().toString(); String content = state.getContentBuf().toString();
if (state.getCurrentItem() != null && content != null && if (state.getCurrentItem() != null && content != null
state.getCurrentItem().getDescription() == null) { && state.getCurrentItem().getDescription() == null) {
state.getCurrentItem().setDescription(content); state.getCurrentItem().setDescription(content);
} }
} }
} }
} }