private CoverArtImage FetchImage(string entity, Guid mbid, string id, CoverArtImageSize size)
        {
            var suffix = string.Empty;

            if (size != CoverArtImageSize.Original)
            {
                suffix = "-" + ((int)size).ToString(CultureInfo.InvariantCulture);
            }
            var uri = new UriBuilder("http", this.WebSite, this.Port, $"{entity}/{mbid:D}/{id}{suffix}").Uri;

            using var response = this.PerformRequest(uri);
            Debug.Print($"[{DateTime.UtcNow}] => RESPONSE ({response.ContentType}): {response.ContentLength} bytes");
            if (response.ContentLength > CoverArt.MaxImageSize)
            {
                throw new ArgumentException($"The requested image is too large ({response.ContentLength} > {CoverArt.MaxImageSize}).");
            }
            using var stream = response.GetResponseStream();
            if (stream == null)
            {
                throw new WebException("No data received.", WebExceptionStatus.ReceiveFailure);
            }
            var data = new MemoryStream();

            try {
                stream.CopyTo(data);
            }
            catch {
                data.Dispose();
                throw;
            }
            return(new CoverArtImage(id, size, response.ContentType, data));
        }
Esempio n. 2
0
 internal CoverArtImage(string id, CoverArtImageSize size, string type, Stream data)
 {
     this.Id          = id;
     this.Size        = size;
     this.ContentType = type;
     this.Data        = data;
 }
 /// <summary>Fetch the specified image for the specified release, in the specified size.</summary>
 /// <param name="mbid">The MusicBrainz release ID for which the image is requested.</param>
 /// <param name="id">
 /// The ID of the requested image (as found via <see cref="Image.Id"/>, or "front"/"back" as special case).
 /// </param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>An asynchronous operation returning the requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release and/or the specified image do not exist;
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public Task <CoverArtImage> FetchImageAsync(Guid mbid, string id, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImageAsync("release", mbid, id, size);
 /// <summary>Fetch the specified image for the specified release, in the specified size.</summary>
 /// <param name="mbid">The MusicBrainz release ID for which the image is requested.</param>
 /// <param name="id">
 /// The ID of the requested image (as found via <see cref="Image.Id"/>, or "front"/"back" as special case).
 /// </param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>The requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release and/or the specified image do not exist;
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public CoverArtImage FetchImage(Guid mbid, string id, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImage("release", mbid, id, size);
 /// <summary>Fetch the main "front" image for the specified release group, in the specified size.</summary>
 /// <param name="mbid">The MusicBrainz release group ID for which the image is requested.</param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>An asynchronous operation returning the requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release group does not exist (or has no "front" image set);
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public Task <CoverArtImage> FetchGroupFrontAsync(Guid mbid, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImageAsync("release-group", mbid, "front", size);
 /// <summary>Fetch the main "front" image for the specified release group, in the specified size.</summary>
 /// <param name="mbid">The MusicBrainz release group ID for which the image is requested.</param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>The requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release group does not exist (or has no "front" image set);
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public CoverArtImage FetchGroupFront(Guid mbid, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImage("release-group", mbid, "front", size);
 /// <summary>Fetch the main "back" image for the specified release.</summary>
 /// <param name="mbid">The MusicBrainz release ID for which the image is requested.</param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>An asynchronous operation returning the requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release does not exist (or has no "back" image set);
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public Task <CoverArtImage> FetchBackAsync(Guid mbid, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImageAsync("release", mbid, "back", size);
 /// <summary>Fetch the main "back" image for the specified release.</summary>
 /// <param name="mbid">The MusicBrainz release ID for which the image is requested.</param>
 /// <param name="size">
 /// The requested image size; <see cref="CoverArtImageSize.Original"/> can be any content type, but the thumbnails are always
 /// JPEG.
 /// </param>
 /// <returns>The requested image data.</returns>
 /// <exception cref="WebException">
 /// When something went wrong with the request.
 /// More details can be found in the exception's <see cref="WebException.Response"/> property.<br/>
 /// Possible status codes for the response are:
 /// <ul><li>
 ///   404 (<see cref="HttpStatusCode.NotFound"/>) when the release does not exist (or has no "back" image set);
 /// </li><li>
 ///   503 (<see cref="HttpStatusCode.ServiceUnavailable"/>) when the server is unavailable, or rate limiting is in effect.
 /// </li></ul>
 /// </exception>
 public CoverArtImage FetchBack(Guid mbid, CoverArtImageSize size = CoverArtImageSize.Original)
 => this.FetchImage("release", mbid, "back", size);