コード例 #1
0
        private async Task <BookDownloadLinkModel> GetDownloadLink(string url)
        {
            HttpResponseMessage response;

            try
            {
                response = await _webClient.DoHeadAsync(url);
            }
            catch (Exception)
            {
                return(null);
            }

            if (response == null || response.Content.Headers.ContentType == null)
            {
                return(null);
            }

            var contentType = response.Content.Headers.ContentType.MediaType;

            if (contentType.Contains("application/epub"))
            {
                var type = contentType.EndsWith("zip") ? "application/epub+zip" : "application/epub";
                return(new BookDownloadLinkModel
                {
                    Type = type,
                    Url = url
                });
            }
            if (contentType.Contains("application/fb2"))
            {
                var type = contentType.EndsWith("zip") ? "application/fb2+zip" : "application/fb2";
                return(new BookDownloadLinkModel
                {
                    Type = type,
                    Url = url
                });
            }
            if (contentType.Contains("application/html"))
            {
                var type = contentType.EndsWith("zip") ? "application/html+zip" : "application/html";
                return(new BookDownloadLinkModel
                {
                    Type = type,
                    Url = url
                });
            }
            if (contentType.Contains("application/txt"))
            {
                var type = contentType.EndsWith("zip") ? "application/txt+zip" : "application/txt";
                return(new BookDownloadLinkModel
                {
                    Type = type,
                    Url = url
                });
            }
            if (contentType.Contains("application/zip"))
            {
                return(new BookDownloadLinkModel
                {
                    Type = "application/fb2+zip",
                    Url = url
                });
            }

            return(null);
        }