mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
change favicon rest method to accept a subscription id
This commit is contained in:
@@ -36,29 +36,45 @@ public class FaviconFetcher {
|
|||||||
@Inject
|
@Inject
|
||||||
HttpGetter getter;
|
HttpGetter getter;
|
||||||
|
|
||||||
public byte[] fetch(String targetPath) {
|
public byte[] fetch(String url) {
|
||||||
byte[] icon = getIconAtRoot(targetPath);
|
|
||||||
|
if (url == null) {
|
||||||
|
log.info("url is null");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
int doubleSlash = url.indexOf("//");
|
||||||
|
if (doubleSlash == -1) {
|
||||||
|
doubleSlash = 0;
|
||||||
|
} else {
|
||||||
|
doubleSlash += 2;
|
||||||
|
}
|
||||||
|
int firstSlash = url.indexOf('/', doubleSlash);
|
||||||
|
if (firstSlash != -1) {
|
||||||
|
url = url.substring(0, firstSlash);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] icon = getIconAtRoot(url);
|
||||||
|
|
||||||
if (icon == null) {
|
if (icon == null) {
|
||||||
icon = getIconInPage(targetPath);
|
icon = getIconInPage(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
return icon;
|
return icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getIconAtRoot(String targetPath) {
|
private byte[] getIconAtRoot(String url) {
|
||||||
byte[] bytes = null;
|
byte[] bytes = null;
|
||||||
String contentType = null;
|
String contentType = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String url = FeedUtils.removeTrailingSlash(targetPath)
|
url = FeedUtils.removeTrailingSlash(url) + "/favicon.ico";
|
||||||
+ "/favicon.ico";
|
log.info("getting root icon at {}", url);
|
||||||
log.debug("getting root icon at {}", url);
|
|
||||||
HttpResult result = getter.getBinary(url);
|
HttpResult result = getter.getBinary(url);
|
||||||
bytes = result.getContent();
|
bytes = result.getContent();
|
||||||
contentType = result.getContentType();
|
contentType = result.getContentType();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Failed to retrieve iconAtRoot: " + e.getMessage(), e);
|
log.debug("Failed to retrieve iconAtRoot: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isValidIconResponse(bytes, contentType)) {
|
if (!isValidIconResponse(bytes, contentType)) {
|
||||||
@@ -74,7 +90,7 @@ public class FaviconFetcher {
|
|||||||
|
|
||||||
long length = content.length;
|
long length = content.length;
|
||||||
|
|
||||||
if (!contentType.isEmpty()) {
|
if (StringUtils.isNotBlank(contentType)) {
|
||||||
contentType = contentType.split(";")[0];
|
contentType = contentType.split(";")[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,13 +114,12 @@ public class FaviconFetcher {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getIconInPage(String targetPath) {
|
private byte[] getIconInPage(String url) {
|
||||||
log.info("iconInPage, trying " + targetPath);
|
|
||||||
|
|
||||||
Document doc;
|
Document doc = null;
|
||||||
try {
|
try {
|
||||||
HttpResult result = getter.getBinary(targetPath);
|
HttpResult result = getter.getBinary(url);
|
||||||
doc = Jsoup.parse(new String(result.getContent()), targetPath);
|
doc = Jsoup.parse(new String(result.getContent()), url);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("Failed to retrieve page to find icon");
|
log.info("Failed to retrieve page to find icon");
|
||||||
return null;
|
return null;
|
||||||
@@ -114,7 +129,7 @@ public class FaviconFetcher {
|
|||||||
.select("link[rel~=(?i)^(shortcut|icon|shortcut icon)$]");
|
.select("link[rel~=(?i)^(shortcut|icon|shortcut icon)$]");
|
||||||
|
|
||||||
if (icons.isEmpty()) {
|
if (icons.isEmpty()) {
|
||||||
log.info("No icon found in page");
|
log.info("No icon found in page {}", url);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.commafeed.backend.feeds;
|
package com.commafeed.backend.feeds;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -22,6 +20,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.commafeed.backend.model.Feed;
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.commafeed.backend.model.FeedEntry;
|
import com.commafeed.backend.model.FeedEntry;
|
||||||
|
import com.commafeed.backend.model.FeedSubscription;
|
||||||
import com.google.api.client.util.Lists;
|
import com.google.api.client.util.Lists;
|
||||||
import com.google.gwt.i18n.client.HasDirection.Direction;
|
import com.google.gwt.i18n.client.HasDirection.Direction;
|
||||||
import com.google.gwt.i18n.shared.BidiUtils;
|
import com.google.gwt.i18n.shared.BidiUtils;
|
||||||
@@ -263,25 +262,9 @@ public class FeedUtils {
|
|||||||
return baseUrl + url;
|
return baseUrl + url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getFaviconUrl(String url, String publicUrl) {
|
public static String getFaviconUrl(FeedSubscription subscription,
|
||||||
|
String publicUrl) {
|
||||||
String defaultIcon = removeTrailingSlash(publicUrl)
|
return removeTrailingSlash(publicUrl) + "/rest/feed/favicon?id="
|
||||||
+ "/images/default_favicon.gif";
|
+ subscription.getId();
|
||||||
if (StringUtils.isBlank(url)) {
|
|
||||||
return defaultIcon;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = Math.max(url.length(), url.lastIndexOf('?'));
|
|
||||||
|
|
||||||
StringBuilder iconUrl = new StringBuilder(
|
|
||||||
"https://getfavicon.appspot.com/");
|
|
||||||
try {
|
|
||||||
iconUrl.append(URLEncoder.encode(url.substring(0, index), "UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
// never happens
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
iconUrl.append("?defaulticon=none");
|
|
||||||
return iconUrl.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ public class Entry implements Serializable {
|
|||||||
entry.setFeedId(String.valueOf(status.getSubscription().getId()));
|
entry.setFeedId(String.valueOf(status.getSubscription().getId()));
|
||||||
entry.setFeedUrl(status.getSubscription().getFeed().getUrl());
|
entry.setFeedUrl(status.getSubscription().getFeed().getUrl());
|
||||||
entry.setFeedLink(status.getSubscription().getFeed().getLink());
|
entry.setFeedLink(status.getSubscription().getFeed().getLink());
|
||||||
entry.setIconUrl(FeedUtils.getFaviconUrl(status.getSubscription()
|
entry.setIconUrl(FeedUtils.getFaviconUrl(status.getSubscription(),
|
||||||
.getFeed().getLink(), publicUrl));
|
publicUrl));
|
||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class Subscription implements Serializable {
|
|||||||
sub.setErrorCount(feed.getErrorCount());
|
sub.setErrorCount(feed.getErrorCount());
|
||||||
sub.setFeedUrl(feed.getUrl());
|
sub.setFeedUrl(feed.getUrl());
|
||||||
sub.setFeedLink(feed.getLink());
|
sub.setFeedLink(feed.getLink());
|
||||||
sub.setIconUrl(FeedUtils.getFaviconUrl(feed.getLink(), publicUrl));
|
sub.setIconUrl(FeedUtils.getFaviconUrl(subscription, publicUrl));
|
||||||
sub.setLastRefresh(feed.getLastUpdated());
|
sub.setLastRefresh(feed.getLastUpdated());
|
||||||
sub.setNextRefresh((feed.getDisabledUntil() != null && feed
|
sub.setNextRefresh((feed.getDisabledUntil() != null && feed
|
||||||
.getDisabledUntil().before(now)) ? null : feed
|
.getDisabledUntil().before(now)) ? null : feed
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import javax.ws.rs.Produces;
|
|||||||
import javax.ws.rs.QueryParam;
|
import javax.ws.rs.QueryParam;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.CacheControl;
|
import javax.ws.rs.core.CacheControl;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.core.Response.ResponseBuilder;
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
@@ -31,13 +30,13 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang.ObjectUtils;
|
import org.apache.commons.lang.ObjectUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.http.impl.cookie.DateUtils;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.commafeed.backend.StartupBean;
|
import com.commafeed.backend.StartupBean;
|
||||||
import com.commafeed.backend.feeds.FeedUtils;
|
import com.commafeed.backend.feeds.FeedUtils;
|
||||||
import com.commafeed.backend.feeds.FetchedFeed;
|
import com.commafeed.backend.feeds.FetchedFeed;
|
||||||
|
import com.commafeed.backend.model.Feed;
|
||||||
import com.commafeed.backend.model.FeedCategory;
|
import com.commafeed.backend.model.FeedCategory;
|
||||||
import com.commafeed.backend.model.FeedEntryStatus;
|
import com.commafeed.backend.model.FeedEntryStatus;
|
||||||
import com.commafeed.backend.model.FeedSubscription;
|
import com.commafeed.backend.model.FeedSubscription;
|
||||||
@@ -253,8 +252,16 @@ public class FeedREST extends AbstractResourceREST {
|
|||||||
@GET
|
@GET
|
||||||
@Path("/favicon")
|
@Path("/favicon")
|
||||||
@ApiOperation(value = "Fetch a feed's icon", notes = "Fetch icon of a feed")
|
@ApiOperation(value = "Fetch a feed's icon", notes = "Fetch icon of a feed")
|
||||||
public Response getFavicon(@QueryParam("url") String url) {
|
public Response getFavicon(@QueryParam("id") Long id) {
|
||||||
|
|
||||||
|
Preconditions.checkNotNull(id);
|
||||||
|
FeedSubscription subscription = feedSubscriptionDAO.findById(getUser(),
|
||||||
|
id);
|
||||||
|
if (subscription == null) {
|
||||||
|
return Response.status(Status.NOT_FOUND).build();
|
||||||
|
}
|
||||||
|
Feed feed = subscription.getFeed();
|
||||||
|
String url = feed.getLink() != null ? feed.getLink() : feed.getUrl();
|
||||||
byte[] icon = faviconFetcher.fetch(url);
|
byte[] icon = faviconFetcher.fetch(url);
|
||||||
|
|
||||||
ResponseBuilder builder = null;
|
ResponseBuilder builder = null;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ module
|
|||||||
url : '='
|
url : '='
|
||||||
},
|
},
|
||||||
replace : true,
|
replace : true,
|
||||||
template : '<img ng-src="{{url}}" class="favicon" onError="this.src=\'images/default_favicon.gif\'"></img>'
|
template : '<img ng-src="{{url}}" class="favicon"></img>'
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user