Exemplo n.º 1
0
        public async Task <IActionResult> UploadFiles(Guid galleryId, int FamilyImageId, List <IFormFile> files)
        {
            try
            {
                var cancellationToken = GetCancellationToken();

                var gallery = await _readModel.Load(galleryId, cancellationToken);

                if (gallery == null)
                {
                    return(BadRequest("Ïnvalid Gallery Id"));
                }

                foreach (var formFile in files)
                {
                    var checkextension = Path.GetExtension(formFile.FileName).ToLower();

                    if (!_AllowedExtensions.Contains(checkextension))
                    {
                        return(BadRequest($"Invalid file format {checkextension} on file {formFile.FileName}"));
                    }

                    var img = new ImageReferenceModel()
                    {
                        MimeType         = formFile.ContentType,
                        FileName         = formFile.FileName,
                        Id               = Guid.NewGuid(),
                        GalleryReference = galleryId,
                        PersonId         = FamilyImageId
                    };

                    if (formFile.Length > 0)
                    {
                        using (var stream = new MemoryStream())
                        {
                            await formFile.CopyToAsync(stream);

                            var rotatedImage = RotateImage(stream);
                            using (var rotatedStream = new MemoryStream())
                            {
                                rotatedImage.Save(rotatedStream, GetRawFormat(formFile.FileName));
                                await _fileService.SaveFile(img.Id, galleryId.ToString(), rotatedStream, formFile.FileName, formFile.ContentType, cancellationToken);

                                rotatedStream.Position = 0;
                                var resized = FileResizer.GetFileResized(ReadFully(rotatedStream), 150);
                                await _fileService.SaveFile(img.Id, $"{galleryId}/thumbnail", resized, formFile.FileName, formFile.ContentType, cancellationToken);

                                await _imageReferenceWriteModel.Save(img.MapToEntity(), cancellationToken);
                            }
                        }
                    }
                }
                return(await Task.FromResult(RedirectToAction("EditGalleryImages", new { id = gallery.ID }).WithSuccess("Image/s saved")));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to upload the image/s");
                return(RedirectToAction("EditGalleryImages", new { id = galleryId }).WithError("Sorry - there has been an error saving the files. The error has been logged"));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EventImage(FamilyEventImage imageWriteModel)
        {
            var cancellationToken = GetCancellationToken();
            var gallery           = await _galleryReadRepository.FindAllByFamilyEvent(imageWriteModel.FamilyEventId, GetCancellationToken());

            if (!(gallery.Any() || gallery.Any(t => t.DefaultFamilyEventGallery)))
            {
                // is there an imagegallery for this event?
                var eventGallery = new Repo.Core.Model.Gallery()
                {
                    ID          = imageWriteModel.FamilyEventGalleryId,
                    DateCreated = DateTime.Now,
                    FamilyEvent = imageWriteModel.FamilyEventId,
                    DefaultFamilyEventGallery = true,
                    Name  = string.Empty,
                    Owner = _contextService.LoggedInPerson.Id
                };
                await _galleryWriteRepository.Save(eventGallery, cancellationToken);

                imageWriteModel.FamilyEventGalleryId = eventGallery.ID;
            }
            else
            {
                imageWriteModel.FamilyEventGalleryId = gallery.First(g => g.DefaultFamilyEventGallery).ID;
                var imageReferences = await _imageReferenceReadRepository.LoadByGallery(imageWriteModel.FamilyEventGalleryId, cancellationToken);

                var currentDefaultImage = imageReferences.FirstOrDefault(t => t.DefaultGalleryImage);
                if (currentDefaultImage != null)
                {
                    currentDefaultImage.DefaultGalleryImage = false;
                    await _imageReferenceWriteModel.Save(currentDefaultImage, cancellationToken);
                }
            }

            var allowedExtensions = new[] { ".png", ".gif", ".jpg" };

            var checkextension = Path.GetExtension(imageWriteModel.DefaultImage.FileName).ToLower();

            if (!allowedExtensions.Contains(checkextension))
            {
                return(BadRequest($"Invalid file format {checkextension} on file {imageWriteModel.DefaultImage.FileName}"));
            }

            var img = new ImageReferenceModel()
            {
                MimeType            = imageWriteModel.DefaultImage.ContentType,
                Title               = imageWriteModel.DefaultImage.FileName,
                Id                  = Guid.NewGuid(),
                GalleryReference    = imageWriteModel.FamilyEventGalleryId,
                DefaultGalleryImage = true
            };

            if (imageWriteModel.DefaultImage.Length > 0)
            {
                using (var stream = new MemoryStream())
                {
                    await imageWriteModel.DefaultImage.CopyToAsync(stream);

                    stream.Position = 0;
                    var resized = FileResizer.GetFileResized(ReadFully(stream), 150);
                    await _fileService.SaveFile(img.Id, $"{imageWriteModel.FamilyEventGalleryId}/thumbnail", resized, imageWriteModel.DefaultImage.FileName, imageWriteModel.DefaultImage.ContentType, cancellationToken);

                    await _imageReferenceWriteModel.Save(img.MapToEntity(), cancellationToken);
                }
            }

            return(Json(new { path = img.Id + "/thumbnail/" + img.Title, id = img.Id }));
        }