mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
return the correct media type for favicons (fix #736)
This commit is contained in:
@@ -3,6 +3,8 @@ package com.commafeed.backend.favicon;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -18,7 +20,7 @@ public abstract class AbstractFaviconFetcher {
|
||||
|
||||
protected static int TIMEOUT = 4000;
|
||||
|
||||
public abstract byte[] fetch(Feed feed);
|
||||
public abstract Favicon fetch(Feed feed);
|
||||
|
||||
protected boolean isValidIconResponse(byte[] content, String contentType) {
|
||||
if (content == null) {
|
||||
@@ -48,4 +50,11 @@ public abstract class AbstractFaviconFetcher {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public static class Favicon {
|
||||
private final byte[] icon;
|
||||
private final String mediaType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,15 @@ public class DefaultFaviconFetcher extends AbstractFaviconFetcher {
|
||||
private final HttpGetter getter;
|
||||
|
||||
@Override
|
||||
public byte[] fetch(Feed feed) {
|
||||
byte[] icon = fetch(feed.getLink());
|
||||
public Favicon fetch(Feed feed) {
|
||||
Favicon icon = fetch(feed.getLink());
|
||||
if (icon == null) {
|
||||
icon = fetch(feed.getUrl());
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
private byte[] fetch(String url) {
|
||||
private Favicon fetch(String url) {
|
||||
if (url == null) {
|
||||
log.debug("url is null");
|
||||
return null;
|
||||
@@ -53,7 +53,7 @@ public class DefaultFaviconFetcher extends AbstractFaviconFetcher {
|
||||
url = url.substring(0, firstSlash);
|
||||
}
|
||||
|
||||
byte[] icon = getIconAtRoot(url);
|
||||
Favicon icon = getIconAtRoot(url);
|
||||
|
||||
if (icon == null) {
|
||||
icon = getIconInPage(url);
|
||||
@@ -62,7 +62,7 @@ public class DefaultFaviconFetcher extends AbstractFaviconFetcher {
|
||||
return icon;
|
||||
}
|
||||
|
||||
private byte[] getIconAtRoot(String url) {
|
||||
private Favicon getIconAtRoot(String url) {
|
||||
byte[] bytes = null;
|
||||
String contentType = null;
|
||||
|
||||
@@ -78,12 +78,12 @@ public class DefaultFaviconFetcher extends AbstractFaviconFetcher {
|
||||
}
|
||||
|
||||
if (!isValidIconResponse(bytes, contentType)) {
|
||||
bytes = null;
|
||||
return null;
|
||||
}
|
||||
return bytes;
|
||||
return new Favicon(bytes, contentType);
|
||||
}
|
||||
|
||||
private byte[] getIconInPage(String url) {
|
||||
private Favicon getIconInPage(String url) {
|
||||
|
||||
Document doc = null;
|
||||
try {
|
||||
@@ -127,6 +127,6 @@ public class DefaultFaviconFetcher extends AbstractFaviconFetcher {
|
||||
return null;
|
||||
}
|
||||
|
||||
return bytes;
|
||||
return new Favicon(bytes, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class FacebookFaviconFetcher extends AbstractFaviconFetcher {
|
||||
private final HttpGetter getter;
|
||||
|
||||
@Override
|
||||
public byte[] fetch(Feed feed) {
|
||||
public Favicon fetch(Feed feed) {
|
||||
String url = feed.getUrl();
|
||||
|
||||
if (!url.toLowerCase().contains("www.facebook.com")) {
|
||||
@@ -54,9 +54,9 @@ public class FacebookFaviconFetcher extends AbstractFaviconFetcher {
|
||||
}
|
||||
|
||||
if (!isValidIconResponse(bytes, contentType)) {
|
||||
bytes = null;
|
||||
return null;
|
||||
}
|
||||
return bytes;
|
||||
return new Favicon(bytes, contentType);
|
||||
}
|
||||
|
||||
private String extractUserName(String url) {
|
||||
|
||||
@@ -36,7 +36,7 @@ public class YoutubeFaviconFetcher extends AbstractFaviconFetcher {
|
||||
private final CommaFeedConfiguration config;
|
||||
|
||||
@Override
|
||||
public byte[] fetch(Feed feed) {
|
||||
public Favicon fetch(Feed feed) {
|
||||
String url = feed.getUrl();
|
||||
|
||||
if (!url.toLowerCase().contains("youtube.com/feeds/videos.xml")) {
|
||||
@@ -94,8 +94,8 @@ public class YoutubeFaviconFetcher extends AbstractFaviconFetcher {
|
||||
}
|
||||
|
||||
if (!isValidIconResponse(bytes, contentType)) {
|
||||
bytes = null;
|
||||
return null;
|
||||
}
|
||||
return bytes;
|
||||
return new Favicon(bytes, contentType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user