Ejemplo n.º 1
0
        public EncodedPhoto GetEncodedPhotoFromFileSystem(string Fullpath)
        {
            EncodedPhoto encodedPhoto = new EncodedPhoto();

            byte[] photoByteArray = System.IO.File.ReadAllBytes(Fullpath);
            string imgBase64      = Convert.ToBase64String(photoByteArray);
            string imgSrcString   = string.Empty;

            if (Fullpath.EndsWith(".png"))
            {
                imgSrcString         = string.Format("data:image/png;base64,{0}", imgBase64);
                encodedPhoto.IsVideo = false;
            }
            else if (Fullpath.EndsWith(".jpg") || Fullpath.EndsWith(".jpeg"))
            {
                imgSrcString         = string.Format("data:image/jpg;base64,{0}", imgBase64);
                encodedPhoto.IsVideo = false;
            }
            else if (Fullpath.EndsWith(".mp4"))
            {
                encodedPhoto.IsVideo = true;
            }

            encodedPhoto.PhotoBase64 = imgSrcString;
            encodedPhoto.PhotoName   = Fullpath.Split('\\').Last();

            return(encodedPhoto);
        }
Ejemplo n.º 2
0
        public IActionResult GetPhotos(string path, int skip)
        {
            string FullPath = _pathHelper.GetFullPathToFile(path);
            List <EncodedPhoto> encodedPhotos = new List <EncodedPhoto>();
            List <string>       Files         = _fsService.GetAllFilesInFolder(FullPath);
            List <FileInfo>     fileInfos     = new List <FileInfo>();

            for (int i = 0; i < Files.Count; i++)
            {
                fileInfos.Add(new FileInfo(Files[i]));
            }
            fileInfos = fileInfos.OrderByDescending(f => f.CreationTime).Skip(skip * 10).Take(10).ToList();

            using (FamilyArchiveContext db = new FamilyArchiveContext())
            {
                for (int i = 0; i < fileInfos.Count; i++)
                {
                    EncodedPhoto encodedPhoto = _dbService.GetPhotoFromDb(path, fileInfos[i].Name, db);
                    if (encodedPhoto == null)
                    {
                        encodedPhoto = _fsService.GetEncodedPhotoFromFileSystem(Path.Combine(FullPath, fileInfos[i].Name));
                    }

                    encodedPhotos.Add(encodedPhoto);
                }
            }

            return(Json(encodedPhotos));
        }
Ejemplo n.º 3
0
        public EncodedPhoto GetPhotoFromDb(string partPath, string filename, FamilyArchiveContext context)
        {
            string searchString = string.Empty;

            if (string.IsNullOrEmpty(partPath))
            {
                searchString = filename;
            }
            else
            {
                searchString = Path.Combine(partPath, filename);
            }

            Photos       photo = context.Photos.Where(p => p.Name == searchString).FirstOrDefault();
            EncodedPhoto encodedPhoto;

            if (photo != null)
            {
                encodedPhoto = new EncodedPhoto();
                string Base64PhotoString = string.Empty;

                if (photo.Name.EndsWith(".png") && !photo.IsThumbnail)
                {
                    Base64PhotoString    = "data:image/png;base64," + Encoding.ASCII.GetString(photo.PhotoBase64);
                    encodedPhoto.IsVideo = false;
                }
                else if ((photo.Name.EndsWith(".jpg") || photo.Name.EndsWith(".jpeg")) && !photo.IsThumbnail)
                {
                    Base64PhotoString    = "data:image/jpg;base64," + Encoding.ASCII.GetString(photo.PhotoBase64);
                    encodedPhoto.IsVideo = false;
                }
                else if (photo.Name.EndsWith(".mp4") && photo.IsThumbnail)
                {
                    Base64PhotoString    = "data:image/png;base64," + Encoding.ASCII.GetString(photo.PhotoBase64);
                    encodedPhoto.IsVideo = true;
                }

                encodedPhoto.PhotoName        = photo.Name.Split('\\').Last();
                encodedPhoto.PhotoHeader      = photo.Header;
                encodedPhoto.PhotoDescription = photo.Description;
                encodedPhoto.PhotoBase64      = Base64PhotoString;
                return(encodedPhoto);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
        public static EncodedPhoto ToEncodedPhoto(this Photo photo)
        {
            var encodedPhoto = new EncodedPhoto()
            {
                Id    = photo.Id,
                Name  = photo.Name,
                Order = photo.Order
            };

            using (Image image = Image.FromFile(photo.PhysicalPath))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, image.RawFormat);
                    var imageBytes = m.ToArray();

                    encodedPhoto.Data = $"data:image/{photo.PhysicalPath.Split(".")[1]};base64,{Convert.ToBase64String(imageBytes)}";
                }
            }

            return(encodedPhoto);
        }