use of net.pms.database.TableCoverArtArchive.CoverArtArchiveResult in project UniversalMediaServer by UniversalMediaServer.
the class CoverArtArchiveUtil method doGetThumbnail.
@Override
protected byte[] doGetThumbnail(Tag tag, boolean externalNetwork) {
String mBID = getMBID(tag, externalNetwork);
if (mBID != null) {
// Secure exclusive access to search for this tag
CoverArtArchiveCoverLatch latch = reserveCoverLatch(mBID);
if (latch == null) {
// Couldn't reserve exclusive access, giving up
return null;
}
try {
// Check if it's cached first
CoverArtArchiveResult result = TableCoverArtArchive.findMBID(mBID);
if (result.found) {
if (result.cover != null) {
return result.cover;
} else if (System.currentTimeMillis() - result.modified.getTime() < EXPIRATION_TIME) {
// return null. Do another lookup after expireTime has passed
return null;
}
}
if (!externalNetwork) {
LOGGER.warn("Can't download cover from Cover Art Archive since external network is disabled");
LOGGER.info("Either enable external network or disable cover download");
return null;
}
DefaultCoverArtArchiveClient client = new DefaultCoverArtArchiveClient();
CoverArt coverArt;
try {
coverArt = client.getByMbid(UUID.fromString(mBID));
} catch (CoverArtException e) {
LOGGER.debug("Could not get cover with MBID \"{}\": {}", mBID, e.getMessage());
LOGGER.trace("", e);
return null;
}
if (coverArt == null || coverArt.getImages().isEmpty()) {
LOGGER.debug("MBID \"{}\" has no cover at CoverArtArchive", mBID);
TableCoverArtArchive.writeMBID(mBID, null);
return null;
}
CoverArtImage image = coverArt.getFrontImage();
if (image == null) {
image = coverArt.getImages().get(0);
}
byte[] cover = null;
try {
try (InputStream is = image.getLargeThumbnail()) {
cover = IOUtils.toByteArray(is);
} catch (HttpResponseException e) {
// Use the default image if the large thumbnail is not available
try (InputStream is = image.getImage()) {
cover = IOUtils.toByteArray(is);
}
}
TableCoverArtArchive.writeMBID(mBID, cover);
return cover;
} catch (HttpResponseException e) {
if (e.getStatusCode() == 404) {
LOGGER.debug("Cover for MBID \"{}\" was not found at CoverArtArchive", mBID);
TableCoverArtArchive.writeMBID(mBID, null);
return null;
}
LOGGER.warn("Got HTTP response {} while trying to download cover for MBID \"{}\" from CoverArtArchive: {}", e.getStatusCode(), mBID, e.getMessage());
} catch (IOException e) {
LOGGER.error("An error occurred while downloading cover for MBID \"{}\": {}", mBID, e.getMessage());
LOGGER.trace("", e);
return null;
}
} finally {
releaseCoverLatch(latch);
}
}
return null;
}
Aggregations