Beispiel #1
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));
        }
Beispiel #2
0
        public bool WriteHeaderDescription(EditFileInfo editFileInfo)
        {
            string searchString = string.Empty;

            if (string.IsNullOrEmpty(editFileInfo.Path))
            {
                searchString = editFileInfo.FileName;
            }
            else
            {
                searchString = Path.Combine(editFileInfo.Path, editFileInfo.FileName);
            }

            using (FamilyArchiveContext db = new FamilyArchiveContext())
            {
                Photos photo = db.Photos.Where(p => p.Name == searchString).FirstOrDefault();
                if (photo != null)
                {
                    photo.Header      = editFileInfo.NewHeader;
                    photo.Description = editFileInfo.NewDescription;
                    db.SaveChanges();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #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);
            }
        }
Beispiel #4
0
        public void DeletePhotoFromDb(DeleteFileInfo deleteFileInfo)
        {
            using (FamilyArchiveContext db = new FamilyArchiveContext())
            {
                string searchString = string.Empty;
                if (string.IsNullOrEmpty(deleteFileInfo.Path))
                {
                    searchString = deleteFileInfo.PhotoName;
                }
                else
                {
                    searchString = Path.Combine(deleteFileInfo.Path, deleteFileInfo.PhotoName);
                }

                List <Photos> photos = db.Photos.Where(p => p.Name == searchString).ToList();
                db.Photos.RemoveRange(photos);
                db.SaveChanges();
            }
        }
Beispiel #5
0
        public void AddPhotoToDb(UploadedPhoto uploadedPhoto, FamilyArchiveContext context)
        {
            if (context.Photos.Where(p => p.Name == uploadedPhoto.filename && !p.IsThumbnail).FirstOrDefault() == null)
            {
                byte[] Base64Photo;
                if (uploadedPhoto.fileContent.Length > 1000000)
                {
                    MemoryStream memoryStream = new MemoryStream(uploadedPhoto.fileContent);

                    ImageOptimizer imageOptimizer = new ImageOptimizer();
                    imageOptimizer.LosslessCompress(memoryStream);

                    using (MagickImage sprite = new MagickImage(memoryStream))
                    {
                        sprite.Quality = 100;

                        sprite.AutoOrient();

                        if (sprite.Width > sprite.Height)
                        {
                            sprite.Resize(1280, 960);
                        }
                        else
                        {
                            sprite.Resize(960, 1280);
                        }

                        Base64Photo = Encoding.ASCII.GetBytes(sprite.ToBase64());
                    }
                }
                else
                {
                    Base64Photo = Encoding.ASCII.GetBytes(Convert.ToBase64String(uploadedPhoto.fileContent));
                }

                Photos DbPhotoToAdd = new Photos();
                DbPhotoToAdd.Name        = uploadedPhoto.filename;
                DbPhotoToAdd.IsThumbnail = false;
                DbPhotoToAdd.PhotoBase64 = Base64Photo;
                context.Photos.Add(DbPhotoToAdd);
            }
        }
Beispiel #6
0
        public async Task <IActionResult> UploadPhotos(string path, [FromForm] IList <IFormFile> photos)
        {
            List <UploadedPhoto> uploadedPhotos = new List <UploadedPhoto>();

            for (int i = 0; i < photos.Count; i++)
            {
                string fileName = string.Empty;
                if (string.IsNullOrEmpty(path))
                {
                    fileName = photos[i].FileName;
                }
                else
                {
                    fileName = Path.Combine(path, photos[i].FileName);
                }

                MemoryStream memoryStream = new MemoryStream();
                photos[i].CopyTo(memoryStream);

                bool binaryCheck = false;

                if (photos[i].FileName.EndsWith(".mp4"))
                {
                    binaryCheck = memoryStream.IsMp4();
                }
                else if (photos[i].FileName.EndsWith(".jpg") || photos[i].FileName.EndsWith(".jpeg"))
                {
                    binaryCheck = memoryStream.IsJpg();
                }
                else if (photos[i].FileName.EndsWith(".png"))
                {
                    binaryCheck = memoryStream.IsPng();
                }

                if (!binaryCheck)
                {
                    continue;
                }

                UploadedPhoto uploadedPhoto = new UploadedPhoto {
                    filename = fileName, fileContent = memoryStream.ToArray()
                };
                uploadedPhotos.Add(uploadedPhoto);

                string FullPath = _pathHelper.GetFullPathToFile(fileName);
                if (!System.IO.File.Exists(FullPath))
                {
                    await System.IO.File.WriteAllBytesAsync(FullPath, uploadedPhoto.fileContent);
                }
                else
                {
                    uploadedPhotos.Remove(uploadedPhoto);
                }
            }

            Task task = Task.Factory.StartNew(() =>
            {
                using (FamilyArchiveContext db = new FamilyArchiveContext())
                {
                    for (int i = 0; i < uploadedPhotos.Count; i++)
                    {
                        if (uploadedPhotos[i].filename.EndsWith(".png") || uploadedPhotos[i].filename.EndsWith(".jpg") || uploadedPhotos[i].filename.EndsWith(".jpeg"))
                        {
                            _dbService.AddPhotoToDb(uploadedPhotos[i], db);
                        }
                        else if (uploadedPhotos[i].filename.EndsWith(".mp4"))
                        {
                            string FullPathToVideo = _pathHelper.GetFullPathToFile(uploadedPhotos[i].filename);
                            _dbService.GenerateThumbnailForVideo(uploadedPhotos[i].filename, FullPathToVideo, db);
                        }
                    }

                    db.SaveChanges();
                }
            });

            return(Ok());
        }
Beispiel #7
0
        public void GenerateThumbnailForVideo(string filename, string fullPathToVideo, FamilyArchiveContext context)
        {
            string ReplacingPart       = fullPathToVideo.Split('\\').Last();
            string fullPathToThumbnail = fullPathToVideo.Replace(ReplacingPart, "temp_thumbnail.png");

            if (File.Exists(fullPathToThumbnail))
            {
                File.Delete(fullPathToThumbnail);
            }

            string ThumbnailGenerationString = string.Format(@"-ss 3 -i ""{0}"" -vframes 1 -s 1280x960 ""{1}""", fullPathToVideo, fullPathToThumbnail);

            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                WindowStyle    = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                FileName       = Path.Combine(Directory.GetCurrentDirectory(), "ffmpeg\\bin\\ffmpeg.exe"),
                Arguments      = ThumbnailGenerationString
            };

            Process process = new Process()
            {
                StartInfo = startInfo
            };

            process.Start();
            process.WaitForExit(5000);

            byte[] ThumbnailImage = System.IO.File.ReadAllBytes(fullPathToThumbnail);
            System.IO.File.Delete(fullPathToThumbnail);

            Photos photo = new Photos();

            photo.Name        = filename;
            photo.IsThumbnail = true;
            photo.PhotoBase64 = Encoding.ASCII.GetBytes(Convert.ToBase64String(ThumbnailImage));
            context.Photos.Add(photo);
        }