Search in sources :

Example 1 with CoverArtArchiveResult

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;
}
Also used : DefaultCoverArtArchiveClient(fm.last.musicbrainz.coverart.impl.DefaultCoverArtArchiveClient) CoverArtImage(fm.last.musicbrainz.coverart.CoverArtImage) InputStream(java.io.InputStream) CoverArtArchiveResult(net.pms.database.TableCoverArtArchive.CoverArtArchiveResult) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) CoverArt(fm.last.musicbrainz.coverart.CoverArt) CoverArtException(fm.last.musicbrainz.coverart.CoverArtException)

Aggregations

CoverArt (fm.last.musicbrainz.coverart.CoverArt)1 CoverArtException (fm.last.musicbrainz.coverart.CoverArtException)1 CoverArtImage (fm.last.musicbrainz.coverart.CoverArtImage)1 DefaultCoverArtArchiveClient (fm.last.musicbrainz.coverart.impl.DefaultCoverArtArchiveClient)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CoverArtArchiveResult (net.pms.database.TableCoverArtArchive.CoverArtArchiveResult)1 HttpResponseException (org.apache.http.client.HttpResponseException)1