private async Task <IEnumerable <Image> > ConvertToImages(GetByEarthDateResponse resposne)
        {
            var convertToImageTasks = resposne.photos.Select(async photo => new Image
            {
                Data      = await _imageDownloader.FetchImage(new Uri(photo.img_src)),
                Name      = photo.id.ToString(),
                Extention = ImageFileExtension.From(photo.img_src.Split('.').Last())
            });

            return(await Task.WhenAll(convertToImageTasks));
        }
Exemple #2
0
        public static bool IsValidImageFile(byte[] bytFile, String fileContentType)
        {
            bool isvalid = false;

            byte[] chkBytejpg = { 255, 216, 255, 224 };
            byte[] chkBytebmp = { 66, 77 };
            byte[] chkBytegif = { 71, 73, 70, 56 };
            byte[] chkBytepng = { 137, 80, 78, 71 };


            ImageFileExtension imgfileExtn = ImageFileExtension.none;

            if (fileContentType.Contains("jpg") | fileContentType.Contains("jpeg"))
            {
                imgfileExtn = ImageFileExtension.jpg;
            }
            else if (fileContentType.Contains("png"))
            {
                imgfileExtn = ImageFileExtension.png;
            }
            else if (fileContentType.Contains("bmp"))
            {
                imgfileExtn = ImageFileExtension.bmp;
            }
            else if (fileContentType.Contains("gif"))
            {
                imgfileExtn = ImageFileExtension.gif;
            }

            if (imgfileExtn == ImageFileExtension.jpg || imgfileExtn == ImageFileExtension.jpeg)
            {
                if (bytFile.Length >= 4)
                {
                    int j = 0;
                    for (Int32 i = 0; i <= 3; i++)
                    {
                        if (bytFile[i] == chkBytejpg[i])
                        {
                            j = j + 1;
                            if (j == 3)
                            {
                                isvalid = true;
                            }
                        }
                    }
                }
            }


            if (imgfileExtn == ImageFileExtension.png)
            {
                if (bytFile.Length >= 4)
                {
                    int j = 0;
                    for (Int32 i = 0; i <= 3; i++)
                    {
                        if (bytFile[i] == chkBytepng[i])
                        {
                            j = j + 1;
                            if (j == 3)
                            {
                                isvalid = true;
                            }
                        }
                    }
                }
            }


            if (imgfileExtn == ImageFileExtension.bmp)
            {
                if (bytFile.Length >= 4)
                {
                    int j = 0;
                    for (Int32 i = 0; i <= 1; i++)
                    {
                        if (bytFile[i] == chkBytebmp[i])
                        {
                            j = j + 1;
                            if (j == 2)
                            {
                                isvalid = true;
                            }
                        }
                    }
                }
            }

            if (imgfileExtn == ImageFileExtension.gif)
            {
                if (bytFile.Length >= 4)
                {
                    int j = 0;
                    for (Int32 i = 0; i <= 1; i++)
                    {
                        if (bytFile[i] == chkBytegif[i])
                        {
                            j = j + 1;
                            if (j == 3)
                            {
                                isvalid = true;
                            }
                        }
                    }
                }
            }

            return(isvalid);
        }
 public void ImageFileExtension_GivenInvalidImageExtension_ShouldThrowException()
 {
     Assert.Throws <ImageFileExtensionInvalidException>(() => ImageFileExtension.From("invalidExtension"));
 }
        public void ImageFileExtension_GivenValidImageExtension_ShouldNotThrowException(string imageExtension)
        {
            var exception = Record.Exception(() => ImageFileExtension.From(imageExtension));

            Assert.Null(exception);
        }